DEV Community

Discussion on: Setting up Jest + React-Testing-Library

Collapse
 
skube profile image
skube

I believe the latest version of RTL has cleanup already baked-in.
Also, jest-dom has been scoped to @testing-library.

So, instead of adding the following to setupTests.js (or *.ts):

import '@testing-library/react/cleanup-after-each';
import 'jest-dom/extend-expect';

You should:

yarn remove jest-dom
yarn add -D @testing-library/jest-dom

And then only need to include the following in your setupTests.js (or *.ts):

import '@testing-library/jest-dom/extend-expect'
Collapse
 
rodolphonetto profile image
Rodolpho Netto

I having a lot of problems with import




It returns me Cannot find module './dist/extend-expect' from 'extend-expect.js'



even when it is installed =/

  "devDependencies": {
    "@testing-library/dom": "^6.4.0",
    "@testing-library/jest-dom": "github:testing-library/jest-dom",
    "@testing-library/react": "^9.1.4",
Collapse
 
skube profile image
skube • Edited

I think you have it installed incorrectly, my package.json looks like:

 "devDependencies": 
    "@testing-library/jest-dom": "^4.1.0",
    "@testing-library/react": "^9.1.4",
...

Did you add jest-dom with:

yarn add -D @testing-library/jest-dom
Thread Thread
 
rodolphonetto profile image
Rodolpho Netto • Edited

Yes, I dont know why it was installed like that... I've followed the docs, but now it is working fine, thank you :)

Collapse
 
abetoots profile image
abe caymo

also note (from docs):
cleanup is only done automatically if the testing framework you're using supports the afterEach global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.