Skip to content

@testing-library/jest-dom/vitest setup fails with "Invalid Chai property: toBeInTheDocument" #897

@koizuka

Description

@koizuka

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

  1. Create a React project that uses @testing-library/jest-dom with import '@testing-library/jest-dom/vitest' in setupTests.ts
  2. Run vp migrate
  3. Run vp test run
  4. All tests using jest-dom matchers (toBeInTheDocument, toHaveTextContent, etc.) fail with "Invalid Chai property"

Steps to reproduce

  1. vp migrate on an existing Vite + Vitest + React project
  2. 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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions