Cannot find module '@testing-library/jest-dom/extend-expect' from 'jest.setup.js'
Asked Answered
A

4

31
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?`

Arraignment answered 20/10, 2023 at 5:17 Comment(0)
A
67

This was a breaking change in version 6.0 that removed the extend-expect entry point in favor of a default entry point and some platform specific ones. For my use case, the default entry point worked:

import '@testing-library/jest-dom'
Aforementioned answered 27/10, 2023 at 15:41 Comment(0)
I
22

Remove extend-expect from your import:

import '@testing-library/jest-dom'
Ignacia answered 26/10, 2023 at 17:15 Comment(0)
H
4

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

Hannus answered 22/10, 2023 at 20:6 Comment(1)
shouldn't have downgraded, there are security vulnerabilities in 5.16.5 - the answer is pretty clear above in saying you need to update your import to only be import '@testing-library/jest-dom'Inch
H
3

Changing setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"] to setupFilesAfterEnv: ["@testing-library/jest-dom"] in jest.config.js file worked fine.

Update:

If you use jest setup file do following

  • add import "@testing-library/jest-dom" inside jest.setup.js
  • add reference to setup file in jest.config: setupFilesAfterEnv: ["<rootDir>/jest.setup.js"]
Hewett answered 28/2 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.