-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[code-infra] vitest
browser & jsdom modes
#14508
base: master
Are you sure you want to change the base?
Changes from 104 commits
9708607
dd7d81f
a3340d9
d591723
a2123a0
12a33cc
8b70d36
ec169bb
0f417c8
b2ee3a1
dd9b531
28343a2
d51f71f
b89d56d
944a5f4
5ca3e01
de75e5f
491002e
c46e176
2c226c7
27e034f
ab99b4b
d796f8b
9c88ffc
e7de8e5
8b9a9c9
26e1354
5d64485
2913400
3dc36ee
1a653db
f0da929
371d8e4
82b4a8a
f1762c2
3e885a8
8e37734
169e333
ad29b23
3325f0b
36ed5f5
8ae56fa
0263c16
3ebee86
a1dd2e7
3bfbb80
3834dda
172c424
7be271e
32bcb6e
426819e
0bd6c3e
78856fc
f403c45
3a4b451
beeb6e6
6e643eb
6df7b9d
4033d6f
c13114e
57ea710
588475a
ecc2eca
bd96dd3
fab6556
b8534f4
fe31823
e67608e
2afd4c9
7b794ff
c3f4db9
b567b88
6b38c59
22f2cbf
8e22aaf
a04cad1
acb092f
8d657d0
ca21a6e
1ae2dc3
a1c3e41
af8e372
23e55bd
e2ec21b
661bf0b
434e2d2
d33772a
85cb1fe
46bcb69
c9fa5e1
6f9e916
d1ac98d
642e4ef
ca4eaba
0f8c8cf
e9853e0
11fa02b
a352883
1f80763
fe0b0d6
edd2907
abcb953
da00ea5
001e933
dda8ecd
e17895d
f87f521
66dc80b
36664df
7b7c193
45c6a1a
533665b
701bd9c
d34359c
aaa574f
9038bed
f6257e4
6d30ab1
522819b
2f04cd7
bd37ddf
3d25a4b
3b7044a
106cf06
5c1c485
7ba5dbd
12a557f
44b7a0a
bf5ff4b
3df9fb5
c47e28e
c0ee4a5
445a754
4ffa9f1
74d97ef
c6484db
332a58e
c2a78ff
d24941b
f7af0bb
bd28140
b62b902
1de5e03
42de77d
0d66e4e
67a8d5a
db7ede4
a3145c1
2a7508a
657a5ea
71729a9
2acfb89
775a4cd
288eb22
00da37e
a28374e
eed8451
cc32291
b255641
34feadf
d1e04a2
fab8abb
3f9e03b
6d10490
950d9f0
b27c461
2e1a05a
c5fccba
67c8dd7
f951e15
6603ffc
0579e1a
c564086
fa52763
f766204
be3a31f
6484f59
3aa733b
0758884
cf9d282
472e323
90a37c6
06c8e47
a2f15b7
088dbc8
9782168
5f001e3
4c71514
6900304
f0465b5
9cadb47
0f9a1e1
0e5d7d4
6881ecf
1014ffe
bf2d395
bf91c4d
66827f1
8b97b4f
4a94b0f
94f5179
046d9a5
ff4839f
694ff3c
cabde14
dbd7618
655dc9c
2c2cffe
e6bf33d
72469d5
5798225
d4f7602
f2e674e
818eb96
1c823e7
cf44d28
60b754f
2bfe2fa
6651e15
cb69c03
8b49daa
90d399d
c653d01
7ccff0e
21effe6
b118a0c
9442953
63bebdf
e6a59b1
09d6c38
e16d6af
9a85e68
c87bf05
013aa2f
f95e878
9e9a147
eace5e8
32f8f68
20dc412
5b5923e
c761c7e
1e6e65b
bc599b7
91fb565
eae931a
1611caa
ae3804a
3011fe3
31bb49e
48f6df6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Vitest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CircleCI please: https://www.notion.so/mui-org/code-infra-Migrate-out-of-CircleCI-42350363b7344380a9961cf9731aae31 for the final version of this effort. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is just for ensuring the changes work while working on them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the GH workflow given that CircleCI is already setup? 🤔 |
||
|
||
on: | ||
push: | ||
branches: | ||
- 'master' | ||
- 'next' | ||
pull_request: | ||
branches: | ||
- 'master' | ||
- 'next' | ||
|
||
permissions: {} | ||
|
||
jobs: | ||
vitest-jsdom: | ||
name: Vitest Tests (jsdom) | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | ||
- uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0 | ||
with: | ||
run_install: false | ||
- name: Use Node.js 20.x | ||
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 | ||
with: | ||
node-version: 20 | ||
cache: 'pnpm' # https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#caching-packages-dependencies | ||
- run: pnpm install --frozen-lockfile | ||
- name: Install Playwright Browsers | ||
run: pnpm playwright install --with-deps | ||
- name: Run Tests | ||
run: pnpm vitest --project "jsdom/*" | ||
vitest-browser: | ||
name: Vitest Tests (browser) | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | ||
- uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0 | ||
with: | ||
run_install: false | ||
- name: Use Node.js 20.x | ||
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 | ||
with: | ||
node-version: 20 | ||
cache: 'pnpm' # https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#caching-packages-dependencies | ||
- run: pnpm install --frozen-lockfile | ||
- name: Install Playwright Browsers | ||
run: pnpm playwright install --with-deps | ||
- name: Run Tests | ||
run: pnpm vitest --project "browser/*" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,17 +28,30 @@ const isJSDOM = /jsdom/.test(window.navigator.userAgent); | |
describe('BarChart - click event', () => { | ||
const { render } = createRenderer(); | ||
|
||
beforeEach(() => { | ||
if (window?.document?.body?.style) { | ||
window.document.body.style.margin = '0'; | ||
} | ||
}); | ||
|
||
afterEach(() => { | ||
if (window?.document?.body?.style) { | ||
window.document.body.style.margin = '8px'; | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some charts tests are reliant screen position, and margins are different for karma and vitest, so we force one. 🙃 |
||
|
||
describe('onAxisClick', () => { | ||
it('should provide the right context as second argument', function test() { | ||
it('should provide the right context as second argument', function test(t = {}) { | ||
if (isJSDOM) { | ||
// can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 | ||
this.skip(); | ||
// @ts-expect-error to support mocha and vitest | ||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
this?.skip?.() || t?.skip(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will see this pattern across multiple files, Without this mocha will fail because it waits for the This can then be migrated to just a .skip, but vitest offers many ways of skipping test. function test(t) {
if (isJSDOM) {
t.skip();
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a shim in The same couldn't be done for Don't necessarily have to use it, but it can keep the amount of code changes in tests down. |
||
} | ||
const onAxisClick = spy(); | ||
render( | ||
<div | ||
style={{ | ||
margin: -8, // Removes the body default margins | ||
width: 400, | ||
height: 400, | ||
}} | ||
|
@@ -81,16 +94,17 @@ describe('BarChart - click event', () => { | |
}); | ||
}); | ||
|
||
it('should provide the right context as second argument with layout="horizontal"', function test() { | ||
it('should provide the right context as second argument with layout="horizontal"', function test(t = {}) { | ||
if (isJSDOM) { | ||
// can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 | ||
this.skip(); | ||
// @ts-expect-error to support mocha and vitest | ||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
this?.skip?.() || t?.skip(); | ||
} | ||
const onAxisClick = spy(); | ||
render( | ||
<div | ||
style={{ | ||
margin: -8, // Removes the body default margins | ||
width: 400, | ||
height: 400, | ||
}} | ||
|
@@ -155,16 +169,17 @@ describe('BarChart - click event', () => { | |
).to.deep.equal(['pointer', 'pointer', 'pointer', 'pointer']); | ||
}); | ||
|
||
it('should provide the right context as second argument', function test() { | ||
it('should provide the right context as second argument', function test(t = {}) { | ||
if (isJSDOM) { | ||
// can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 | ||
this.skip(); | ||
// @ts-expect-error to support mocha and vitest | ||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
this?.skip?.() || t?.skip(); | ||
} | ||
const onItemClick = spy(); | ||
render( | ||
<div | ||
style={{ | ||
margin: -8, // No idea why, but that make the SVG coordinates match the HTML coordinates | ||
width: 400, | ||
height: 400, | ||
}} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this regex, it seems hard to search in the codebase for, e.g.
.tsx
, I suspect that listing all the permutations would be clearer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why you would need to search for that though. As you will have to sweep over all configuration files when doing a change that would require updating that anyways 😅
Replacing it to list all the permutations is quite unnecessary in my view:
{.ts,.tsx,.js,.cjs,.cjsx,.mjs,.mjsx,.cts,.ctsx,.mts,.mtsx}
We could cleanup some unlikely exts like
{.ts,.tsx,.js,.cjs,.mjs,.mts}
which would be simpler, but less complete.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it actually be great? I assume we will never need those other extensions, so if someone create them, he might be more likely to realize those are wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite the contrary, we sometimes need those extensions:
tsx
), we need a way to signal node which module system is being used.next/document
in ESM.esbuild
,tsx
andvitest
rely on the extension to determine whether JSX needs to be transformed or not. This is currently giving problems as we have a bunch of jsx in.js
files. See Enable JSX in.js
files privatenumber/tsx#644 and Allow JSX in .js files vitest-dev/vitest#1564. For the latter I have a workaround but it also forces our.ts
files to adhere to the JSX rules, which required me to refactor some code.We shouldn't restrict extensions, they're not for cosmetic reasons. We should just prefer
.ts
/.tsx
unless specific runtime requirements demand otherwise.Besides that, I think the logic of "so if someone create them, he might be more likely to realize those are wrong?" is flawed. They likely won't realize anything, starting with not realizing eslint is not running on their code 😄. Better is to apply the rules to any file that could contain javascript/typescript and if we need to enforce a specific extension we should use a specific rule for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Janpot If a developer can't run the tests he/she try to write in dev mode, I think he/she will notice that something is off.
For eslint specifically, yeah, agree, the ideal is to lint the file and have an eslint rule that error because the extension is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case, why have them waste time reverse engineering the tests to find out they're using the wrong extension if the eslint plugin can tell them in context in the editor? 🤔 Not that there would be anything wrong with using .jsx instead of .js for JSX files, contrary, it would kind of make things easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 https://www.npmjs.com/package/eslint-plugin-filename-rules sounds great.
I think that the value of only having
.js
is about not having to think about extensions. Having to rename a file after refactoring its content and moving the code around is friction to change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can't get around this friction in typescript. Finishing the typescript migration means imposing this friction across the codebase anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙂 Coincidentally, I'm just running into a problem that illustrates why this is harmful. Just trying to figure out why I have a failing test locally in vitest that doesn't seem to break in mocha. The following produces a failing test:
But when you try running the global command:
Whoops, no test is running. What happened? Somewhere along the way we started running the build script natively in node. The file was renamed to .mjs. The test runner only looks for .js, .ts, and .tsx. The author didn't see any tests break after running them and assumed they were good. They didn't realize the test stopped running altogether. If the test framework was configured to run on any encountered javascript file, nothing would have been broken.
It took me a while to realize the test wasn't running, my first instinct was "something fails in vitest that breaks in mocha".