-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 all commits
eff3bca
bd11253
9a6cbf8
4a27d05
fb7c2de
118a532
33293ca
d14ad46
5a67c1b
d39f9e4
aa6b60d
1db9bbb
3e5caf5
a1234b6
9a6eb21
09de9d7
91c67c0
94bbb95
8d37f26
e47aa21
7f19f37
ad28046
d522dab
795d4af
18b7a5c
593e59c
bbeed7f
8e375a2
3eced20
2893eef
4645e72
a8464a9
8f540d7
4507e81
90754d6
8218f0e
a9af3e4
6fffa88
2b0754e
23925ac
5c85b9f
f1269fd
3e2fbe4
164ec72
e396545
5121079
570e939
8812678
a06bb50
f3da275
7ba380b
ca22dde
ff6c715
90d0af7
db2817d
b2b8628
8434b41
0f8b218
2d5d395
4d58fb9
493133d
f338a12
66c410e
94b4443
43ce018
b219263
f530667
4fd6d33
8d3601c
23bb8c2
4497dd8
6539759
b20b1bb
840c4b6
6db68b8
9f6f828
a353cb6
1885222
f985554
b869545
739c2d4
8b38fd9
f5493b8
820bbd5
8342499
163ed7e
953059d
dac3923
ddc252d
8d28a7b
1d47d30
0e13811
88378a6
d32bfe5
dd40b0e
0bba870
f06ff48
5c57167
c264163
a1feb9b
3615198
8757aa4
9799824
ed6dbb8
7c62bfc
4f6b9c4
5442abd
aba0b63
5a930f1
8066b23
3bc1fa7
98013ae
f3b6d1f
7cf9c69
0098dbc
fa96d91
0085b9e
121bde4
dd2621b
fd39e32
a154440
10523ec
96d1d12
d86d6cf
9d486d6
3bfdce8
73b10fe
be83e61
1ff3ea1
1da790d
6a5056e
ee14aa5
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 |
||
|
||
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/*" --coverage | ||
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// We can't import the `.mocharc.js` of the monorepo, otherwise we trigger its `setupBabel`. | ||
|
||
module.exports = { | ||
extension: ['js', 'ts', 'tsx'], | ||
ignore: [ | ||
|
@@ -7,6 +8,8 @@ module.exports = { | |
// Mocha seems to ignore .next anyway (maybe because dotfiles?). | ||
// We're leaving this to make sure. | ||
'docs/.next/**', | ||
'packages/**/*.browser.test.{js,ts,tsx,jsx}', | ||
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. Is this a new file extension we're introducing? Can't find any such file. |
||
'packages/**/*.jsdom.test.{js,ts,tsx,jsx}', | ||
], | ||
recursive: true, | ||
timeout: (process.env.CIRCLECI === 'true' ? 5 : 2) * 1000, // Circle CI has low-performance CPUs. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,17 +106,17 @@ module.exports = function getBabelConfig(api) { | |
plugins.push([ | ||
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. Let's add a TODO here to remove these plugins once fully migrated to |
||
'babel-plugin-replace-imports', | ||
{ | ||
test: /date-fns/i, | ||
replacer: 'date-fns-v4', | ||
test: /date-fns\//i, | ||
replacer: 'date-fns-v4/', | ||
// This option is provided by the `patches/[email protected]` patch | ||
filenameIncludes: 'src/AdapterDateFnsV3/', | ||
}, | ||
]); | ||
plugins.push([ | ||
'babel-plugin-replace-imports', | ||
{ | ||
test: /date-fns-jalali/i, | ||
replacer: 'date-fns-jalali-v3', | ||
test: /date-fns-jalali\//i, | ||
replacer: 'date-fns-jalali-v3/', | ||
// This option is provided by the `patches/[email protected]` patch | ||
filenameIncludes: 'src/AdapterDateFnsJalaliV3/', | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,8 @@ | |
"release:publish:dry-run": "pnpm publish --recursive --tag next --registry=\"http://localhost:4873/\"", | ||
"release:tag": "node scripts/releaseTag.mjs", | ||
"validate": "concurrently \"pnpm prettier && pnpm eslint\" \"pnpm proptypes\" \"pnpm docs:typescript:formatted\" \"pnpm docs:api\"", | ||
"clean:node_modules": "rimraf --glob \"**/node_modules\"" | ||
"clean:node_modules": "rimraf --glob \"**/node_modules\"", | ||
"vitest": "cross-env TZ=UTC vitest" | ||
}, | ||
"devDependencies": { | ||
"@actions/core": "^1.11.1", | ||
|
@@ -101,9 +102,9 @@ | |
"@octokit/plugin-retry": "^7.1.2", | ||
"@octokit/rest": "^21.0.2", | ||
"@playwright/test": "^1.44.1", | ||
"@testing-library/react": "^16.0.1", | ||
"@types/babel__core": "^7.20.5", | ||
"@types/babel__traverse": "^7.20.6", | ||
"@types/chai": "^4.3.20", | ||
"@types/chai-dom": "^1.11.3", | ||
"@types/fs-extra": "^11.0.4", | ||
"@types/karma": "^6.3.9", | ||
|
@@ -118,6 +119,9 @@ | |
"@types/yargs": "^17.0.33", | ||
"@typescript-eslint/eslint-plugin": "^7.18.0", | ||
"@typescript-eslint/parser": "^7.18.0", | ||
"@vitejs/plugin-react": "^4.3.2", | ||
"@vitest/browser": "^2.1.3", | ||
"@vitest/coverage-v8": "^2.1.3", | ||
"autoprefixer": "^10.4.20", | ||
"axe-core": "4.10.2", | ||
"babel-loader": "^9.2.1", | ||
|
@@ -192,14 +196,18 @@ | |
"typescript": "^5.6.3", | ||
"unist-util-visit": "^5.0.0", | ||
"util": "^0.12.5", | ||
"vitest": "2.1.3", | ||
"webpack": "^5.95.0", | ||
"webpack-bundle-analyzer": "^4.10.2", | ||
"webpack-cli": "^5.1.4", | ||
"yargs": "^17.7.2" | ||
}, | ||
"resolutions": { | ||
"react-is": "^18.3.1", | ||
"@types/node": "^20.17.3" | ||
"@types/node": "^20.17.3", | ||
"@playwright/test": "1.44.1", | ||
"playwright": "1.44.1", | ||
"@mui/internal-test-utils": "https://pkg.csb.dev/mui/material-ui/commit/92c23999/@mui/internal-test-utils" | ||
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. This needs to be changed when mui/material-ui#43625 is merged. I would still leave it as a 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. Personally, I would avoid all resolutions if we can, it tends to be misunderstood and overused and could result into setups that break on the user end but not on our end. (probably not for this dependency though) If the version ranges are equal, there should be no duplication. And it shouldn't break in the first place if there was duplication. |
||
}, | ||
"packageManager": "[email protected]", | ||
"engines": { | ||
|
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'; | ||
} | ||
}); | ||
Comment on lines
+31
to
+41
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(); | ||
Comment on lines
+44
to
+49
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".