vitest icon indicating copy to clipboard operation
vitest copied to clipboard

`typecheck`: Custom DOM matchers (`toHaveTextContent`, `toHaveAttribute`) missing from `Assertion` types in v5.0.0

Open vickbk opened this issue 2 months ago • 2 comments

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

  1. Setup a minimal project with [email protected] and @testing-library/jest-dom. Can check my failing action directly on this repo react-heading-manager
  2. Add a setup file importing @testing-library/jest-dom
  3. 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');
});

  1. 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

vickbk avatar Sep 11 '26 06:09 vickbk

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.

hi-ogawa avatar Sep 11 '26 06:09 hi-ogawa

Thanks for clarifying! Updating the import to @testing-library/jest-dom/vitest resolved the type augmentations cleanly. Appreciate the link to the migration guide!

vickbk avatar Sep 11 '26 10:09 vickbk