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

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


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

So that in your Jest configuration:

"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 even swap to require() if using directly TypeScript.


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.