Skip to content

[test] Use the Vitest global expect instead of importing chai - #48996

Merged
JCQuintas merged 2 commits into
mui:masterfrom
JCQuintas:worktree-chai-to-vitest-expect
Aug 19, 2026
Merged

JCQuintas merged 2 commits into
mui:masterfrom
JCQuintas:worktree-chai-to-vitest-expect

Conversation

@JCQuintas

Copy link
Copy Markdown
Member

Motivation

Follow-up to the discussion started from #48915, where a test file was found importing expect from vitest while every other file imported it from chai. 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/expect depends on chai, and pnpm resolves a single chai@6.2.2 instance for the whole repo. That is the same instance setupVitest from @mui/internal-test-utils calls chai.use() on. So the chai-style chains, the chai-dom methods and our custom matchers are all already reachable through Vitest's global expect.

No assertion had to be rewritten. The change is the removal of an import.

Changes

  • Drop the import { expect } from 'chai' line from 415 files (plus one CommonJS require in mui-envinfo).
  • Drop chai from the 18 workspace package.json files. 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.
  • Ban the chai import via no-restricted-imports, merged into all three override blocks so it applies in docs/ and packages/*/src/ too.

Bugs this exposed

While expect was an imported binding, @vitest/eslint-plugin did not recognise it as an assertion, so its rules never ran on our tests. Removing the import turned them on, and vitest/valid-expect immediately caught four assertions that silently asserted nothing, because chai reads the second argument as the failure message rather than as an expected value:

// packages/mui-material/src/BottomNavigation/BottomNavigation.test.js
expect(handleChange.args[0][1], '');   // asserts nothing
expect(handleChange.args[1][1], 1);    // asserts nothing
expect(handleChange.args[2][1], '');   // asserts nothing

// packages/mui-material/src/FormControl/FormControl.test.js
expect(readContext.args[0][0].adornedStart, true);   // asserts nothing

Plus a leftover no-op expect(actual) in base-use-named-exports.test.js. All of these are now real assertions and pass.

vitest/no-conditional-expect is turned off with a TODO. Its 44 remaining reports are deliberate environment branches (isJsdom(), feature flags, optional fixtures), not assertions hidden inside a catch, so silencing it is the accurate call for now and cleaning it up is a separate job.

Testing

  • pnpm eslint: clean
  • pnpm typescript: green across all 19 projects
  • pnpm test:node: 6458 passed, 451 files
  • pnpm test:browser: 6222 passed, 286 files

Note

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.

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
@JCQuintas JCQuintas added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). labels Aug 19, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-48996--material-ui.netlify.app/
QR code for https://deploy-preview-48996--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


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>
@JCQuintas
JCQuintas requested a review from a team August 19, 2026 11:50
@JCQuintas JCQuintas self-assigned this Aug 19, 2026
@JCQuintas
JCQuintas marked this pull request as ready for review August 19, 2026 11:50

@michelengelen michelengelen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JCQuintas
JCQuintas merged commit 06b5c70 into mui:master Aug 19, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants