skip to Main Content
import '@testing-library/jest-dom/extend-expect';


expect(screen.getByText('User does not exist.')).toBeInTheDocument()

I’m writing the unit test for my web app, I use toBeInTheDocument() in my code, but after i run npm test in terminal, it shows that ‘Cannot find module ‘@testing-library/jest-dom/extend-expect’ from ‘jest.setup.js”.

I have a jest.setup.js file which includes the code:require(‘@testing-library/jest-dom/extend-expect’);

and also a jest.config.js file which includes the code:
setupFilesAfterEnv:["./jest.setup.js"],

and I also import ‘@testing-library/jest-dom/extend-expect’; in my test.tsx file, also check the node_modules there is @testing-library/jest-dom under it.
what should I do now?`

2

Answers


  1. I also encountered the same problem, and the issue was with the @testing-library/jest-dom version 6.1.4, which I changed to version 5.16.5

    Login or Signup to reply.
  2. Remove extend-expect from your import:

    import '@testing-library/jest-dom'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search