react-testing-library why is toBeInTheDocument() not a function react-testing-library why is toBeInTheDocument() not a function reactjs reactjs

react-testing-library why is toBeInTheDocument() not a function


toBeInTheDocument is not part of RTL. You need to install jest-dom to enable it.


As mentioned by Giorgio, you need to install jest-dom. Here is what worked for me:

(I was using typescript)

npm i --save-dev @testing-library/jest-dom

Then add an import to your setupTests.ts

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

Then in your jest.config.js you can load it via:

"setupFilesAfterEnv": [    "<rootDir>/src/setuptests.ts"  ]


When you do npm i @testing-library/react make sure there is a setupTests.js file with the following statement in it

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