Skip to content

TypeScript errors with vitest #266

Closed
@unikitty37

Description

@unikitty37

In a TypeScript test file, I have this line of code:

expect(screen.getByTestId('card-info')).toHaveTextContent(/^No information available\.$/)

However, it produces the TypeScript error "Property 'toHaveTextContent' does not exist on type 'Assertion'."

The code still executes properly, but I would like to have TypeScript errors cause a CI pipeline failure, and I can't do this while it's throwing this error (it also makes the VSCode window very ugly with all the red blocks :)

testing-library/react-testing-library#37 merges documentation on fixing this, but a later PR moves it to dom-testing-library — and it's since vanished from there. I'm at a loss to find out how to fix this.

I've already run pnpm i -D @types/testing-library__jest-dom. I'm running import '@testing-library/jest-dom' in a vitest.globals.ts file, and putting a console.log in there confirms it's running for each test file — but TypeScript doesn't appear to be taking any notice of it. I have compilerOptions.types: ['vitest/globals'] in my tsconfig.json.

Short of changing the extension to .js and doing away with type checking in my tests, how can I get rid of this error?

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