Describe the bug
After migrating a React project to Vite+ using vp migrate, tests that use @testing-library/jest-dom/vitest fail with:
Error: Invalid Chai property: toBeInTheDocument
Root cause
@testing-library/jest-dom/vitest internally calls require('vitest').expect.extend(matchers) to register custom matchers. However, in Vite+, vitest is aliased to @voidzero-dev/vite-plus-test via npm overrides. The expect instance obtained by jest-dom through require('vitest') appears to be a different instance from the one actually used during test execution, so the matchers are never registered on the real expect.
Workaround
Manually importing matchers and extending expect from vite-plus/test works:
// setupTests.ts
// import '@testing-library/jest-dom/vitest'; // ← does NOT work
import * as matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vite-plus/test';
expect.extend(matchers);
Reproduction
- Create a React project that uses
@testing-library/jest-dom with import '@testing-library/jest-dom/vitest' in setupTests.ts
- Run
vp migrate
- Run
vp test run
- All tests using jest-dom matchers (
toBeInTheDocument, toHaveTextContent, etc.) fail with "Invalid Chai property"
Steps to reproduce
vp migrate on an existing Vite + Vitest + React project
vp test run
System Info
vp v0.1.11
Local vite-plus:
vite-plus v0.1.11
Tools:
vite v8.0.0
rolldown v1.0.0-rc.9
vitest v4.1.0
oxfmt v0.40.0
oxlint v1.55.0
oxlint-tsgolint v0.16.0
tsdown v0.21.2
Environment:
Version 24.14.0
Source lts
Logs
Error: Invalid Chai property: toBeInTheDocument
❯ Object.proxyGetter [as get] node_modules/@voidzero-dev/vite-plus-test/dist/vendor/chai.mjs:1203:15
Describe the bug
After migrating a React project to Vite+ using
vp migrate, tests that use@testing-library/jest-dom/vitestfail with:Root cause
@testing-library/jest-dom/vitestinternally callsrequire('vitest').expect.extend(matchers)to register custom matchers. However, in Vite+,vitestis aliased to@voidzero-dev/vite-plus-testvia npm overrides. Theexpectinstance obtained by jest-dom throughrequire('vitest')appears to be a different instance from the one actually used during test execution, so the matchers are never registered on the realexpect.Workaround
Manually importing matchers and extending
expectfromvite-plus/testworks:Reproduction
@testing-library/jest-domwithimport '@testing-library/jest-dom/vitest'insetupTests.tsvp migratevp test runtoBeInTheDocument,toHaveTextContent, etc.) fail with "Invalid Chai property"Steps to reproduce
vp migrateon an existing Vite + Vitest + React projectvp test runSystem Info
Logs