Repository navigation
[test] Use the Vitest global expect instead of importing chai - #48996
Merged
Merged
Conversation
Every test file imported `expect` from `chai` while the rest of the suite
already ran on Vitest. Vitest's `expect` is built on the very same chai
singleton that `setupVitest` calls `chai.use()` on, so the chai-style chains,
`chai-dom` methods and our custom matchers (`toErrorDev`, `toHaveFocus`, ...)
all keep working through the global. No assertion had to be rewritten.
- drop the `import { expect } from 'chai'` line from 415 files
- drop `chai` from the workspace `package.json` files, so editors stop
suggesting it on autocomplete; it stays available transitively for
`@mui/internal-test-utils`
- ban the `chai` import through `no-restricted-imports` to keep it that way
Removing the import made the `@vitest/eslint-plugin` rules see these
assertions for the first time. `vitest/valid-expect` caught four assertions
that silently asserted nothing, since chai reads the second argument as the
failure message:
- `expect(handleChange.args[0][1], '')` in BottomNavigation
- `expect(readContext.args[0][0].adornedStart, true)` in FormControl
- a leftover `expect(actual)` in base-use-named-exports
Those are fixed. `vitest/no-conditional-expect` is turned off with a TODO:
its remaining reports are deliberate environment branches, not assertions
hidden in a `catch`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0199TdThnjsRBKieSfmsuVC5
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
Nothing imports chai any more, and the tsconfig files pin types to [node, react, vitest/globals], so these were never auto-included. The Chai namespace reaches the program via vitest/globals -> @vitest/expect -> its own @types/chai dependency. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Follow-up to the discussion started from #48915, where a test file was found importing
expectfromvitestwhile every other file imported it fromchai. Rather than keeping two ways to write assertions, this consolidates on one.The concern raised in the discussion was that our custom matchers (
toErrorDev,toWarnDev,toHaveFocus, ...) would need to be ported to the Vitest matcher API, making the change enormous. That turns out not to be the case.@vitest/expectdepends onchai, and pnpm resolves a singlechai@6.2.2instance for the whole repo. That is the same instancesetupVitestfrom@mui/internal-test-utilscallschai.use()on. So the chai-style chains, thechai-dommethods and our custom matchers are all already reachable through Vitest's globalexpect.No assertion had to be rewritten. The change is the removal of an import.
Changes
import { expect } from 'chai'line from 415 files (plus one CommonJSrequireinmui-envinfo).chaifrom the 18 workspacepackage.jsonfiles. It stays available transitively for@mui/internal-test-utils, but editors no longer suggest it on autocomplete, which is what let it creep back in.chaiimport viano-restricted-imports, merged into all three override blocks so it applies indocs/andpackages/*/src/too.Bugs this exposed
While
expectwas an imported binding,@vitest/eslint-plugindid not recognise it as an assertion, so its rules never ran on our tests. Removing the import turned them on, andvitest/valid-expectimmediately caught four assertions that silently asserted nothing, because chai reads the second argument as the failure message rather than as an expected value:Plus a leftover no-op
expect(actual)inbase-use-named-exports.test.js. All of these are now real assertions and pass.vitest/no-conditional-expectis turned off with a TODO. Its 44 remaining reports are deliberate environment branches (isJsdom(), feature flags, optional fixtures), not assertions hidden inside acatch, so silencing it is the accurate call for now and cleaning it up is a separate job.Testing
pnpm eslint: cleanpnpm typescript: green across all 19 projectspnpm test:node: 6458 passed, 451 filespnpm test:browser: 6222 passed, 286 filesNote
test/integration/*is not matched by any Vitest project's include globs, so those three files are only typechecked and never actually run. Pre-existing, untouched here, but worth a look separately.