Property 'toBeInTheDocument' does not exist on type 'Matchers<any>'

Solution 1:

Most of the solutions above seems to tackle Babel or ESLint. If you have this problem with tslint and pure Typescript it's enough to add: @testing-library/jest-dom to your types

So that in your tsconfig.json:

"types": ["node", "jest", "@testing-library/jest-dom"],

Please also bare in mind that you should include that library into Jest. Rather than import it in every file it is better to do it inside the config file:

setupFilesAfterEnv: [
   "<rootDir>/support/setupTests.js"
],

and then in the file setupTests.js:

import '@testing-library/jest-dom/extend-expect'

or use to require() if using JavaScript or different configuration.

Solution 2:

Please make sure that the correct types are installed in your project. i.e.

npm i -D @testing-library/jest-dom@^4.2.4

From my experience the Typescript types seem to be missing from version 5.x

Solution 3:

eslint overrides didn't help, but

import '@testing-library/jest-dom/extend-expect'

in the begging of the test file solved it.

I found this answer here and also in the jest setup file from the facebook's official react starter app. I hope it helps.

Solution 4:

In my case it was enough to:

  • add to the package.json in devDependencies and install:
"@testing-library/jest-dom": "^5.11.9",
  • add to the .spec file:
import '@testing-library/jest-dom';