Skip to content

Vitest typings for jest-dom are broken in 6.6.2 when using "globalMode" #645

Closed
@InfiniteXyy

Description

@InfiniteXyy
  • @testing-library/jest-dom version: 6.6.2
  • node version: 20.10.0
  • vitest version: 2.1.3
  • npm (or yarn) version: 10.2.3

Relevant code or config / Reproduction:

minimun reproduce demo: https://stackblitz.com/edit/vitejs-vite-ydhnrx?file=src%2FApp.test.tsx

What you did:

Bootstramp a starter repo with vitest & testing-library (vitest should be in global mode, and there is no import xxx from 'vitest' in the whole project)

Problem description / What happened:

The toHaveTextContent should be a matcher of expect, and also is working in runtime, but a type error is thrown.

You can run npm run build to see the error

Suggested solution:

According to the documents of vitest, to extend a matcher, we have to import the "vitest" module otherwise the interface merge might not work as expected
So we could add this line of code to top of the definition

import "vitest"

You can try adding this in the stackbliz repo's dependency (jumping to the source code by ctrl + click the import) and run npm run build again, the type error should be fixed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions