react-testing-library why is toBeInTheDocument() not a function
Solution 1:
toBeInTheDocument
is not part of RTL. You need to install jest-dom to enable it.
And then import it in your test files by:
import '@testing-library/jest-dom'
Solution 2:
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"
]
Solution 3:
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';
Solution 4:
Having tried all of the advice in this post and it still not working for me, I'd like to offer an alternative solution:
Install jest-dom:
npm i --save-dev @testing-library/jest-dom
Then create a setupTests.js
file in the src directory (this bit is important! I had it in the root dir and this did not work...). In here, put:
import '@testing-library/jest-dom'
(or require(...)
if that's your preference).
This worked for me :)
Solution 5:
Some of the accepted answers were basically right but some may be slightly outdated: Some references that are good for now:
- https://github.com/testing-library/jest-dom
- https://jestjs.io/docs/configuration
Here are the full things you need:
- in the project's
<rootDir>
(aka wherepackage.json
andjest.config.js
are), make sure you have a file calledjest.config.js
so that Jest can automatically pick it up for configuration. The file is in JS but is structured similarly to a package.json. - Make sure you input the following:
module.exports = {
testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/dist'], // might want?
moduleNameMapper: {
'@components(.*)': '<rootDir>/src/components$1' // might want?
},
moduleDirectories: ['<rootDir>/node_modules', '<rootDir>/src'],
setupFilesAfterEnv: ['<rootDir>/src/jest-setup.ts'] // this is the KEY
// note it should be in the top level of the exported object.
};
-
Also, note that if you're using typescript you will need to make sure your
jest-setup.ts
file is compiled (so add it tosrc
or to the list of items to compile in yourtsconfig.json
. -
At the top of
jest-setup.ts/js
(or whatever you want to name this entrypoint) file: addimport '@testing-library/jest-dom';
. -
You may also want to make sure it actually runs so put a
console.log('hello, world!');
. You also have the opportunity to add any global functions you'd like to have available in jest such as (global.fetch = jest.fn()
). -
Now you actually have to install
@testing-library/jest-dom
:npm i -D @testing-library/jest-dom
in the console.
With those steps you should be ready to use jest-dom:
Without TS: you still need:
npm i -D @testing-library/jest-dom
- Creating a
jest.config.js
and adding to it a minimum of:module.exports = { setupFilesAfterEnv: ['<rootDir>/[path-to-file]/jest-setup.js'] }
. - Creating a
[path-to-file]/jest-setup.js
and adding to it:import '@testing-library/jest-dom';
.
The jest-setup file is also a great place to configure tests like creating a special renderWithProvider(
function or setting up global window functions.