`typecheck`: Custom DOM matchers (`toHaveTextContent`, `toHaveAttribute`) missing from `Assertion` types in v5.0.0
Describe the bug
After upgrading to [email protected], tsc --noEmit fails with type errors on standard DOM matchers provided by @testing-library/jest-dom (such as toHaveTextContent and toHaveAttribute), even though tests execute and pass successfully at runtime.
The type error indicates that Assertion<void, HTMLElement> no longer includes these extended properties:
error TS2339: Property 'toHaveTextContent' does not exist on type 'Assertion<void, HTMLElement>'.
error TS2339: Property 'toHaveAttribute' does not exist on type 'Assertion<void, HTMLElement>'.
This appears to stem from a regression or breaking change in how vitest exports its internal assertion namespace or interface boundaries in v5, preventing ambient module declarations from properly augmenting Assertion<R, T> or Matchers<R, T>.
Reproduction
- Setup a minimal project with
[email protected]and@testing-library/jest-dom. Can check my failing action directly on this repo react-heading-manager - Add a setup file importing
@testing-library/jest-dom - Create a test asserting on a DOM element:```ts import { test, expect } from 'vitest';
test('DOM assertions', () => {
const el = document.createElement('div');
el.textContent = 'Hello World';
el.setAttribute('data-testid', 'custom-el');
expect(el).toHaveTextContent('Hello World');
expect(el).toHaveAttribute('data-testid', 'custom-el');
});
- Run
pnpm tsc --noEmit.
Expected: Type checking passes without errors when ambient matchers are imported.
Actual: TS2339: Property 'toHaveTextContent' does not exist on type 'Assertion<void, HTMLElement>'.
System Info
### **System Info**
System:
OS: Windows
CPU: x64
Binaries:
Node: v24.8.x
pnpm: 12.3.4
npmPackages:
vitest: 5.0.0 => 5.0.0
typescript: 6
@testing-library/jest-dom: ^7.0.0
Used Package Manager
pnpm
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
Please use import '@testing-library/jest-dom/vitest' https://github.com/testing-library/jest-dom#with-vitest
The import @testing-library/jest-dom doesn't give type anymore likely related to this breaking change about jest global types
https://vitest.dev/guide/migration/#assertion-types-expose-return-and-received-types
Vitest no longer reads custom matcher declarations from the global jest.Matchers interface. Libraries that support both Jest and Vitest should augment jest.Matchers and vitest.Matchers separately. This only affects TypeScript declarations; registering matchers with expect.extend works as before.
Thanks for clarifying! Updating the import to @testing-library/jest-dom/vitest resolved the type augmentations cleanly. Appreciate the link to the migration guide!