score:1

importing '@testing-library/jest-dom' doesn't help me but importing @testing-library/jest-dom/extend-expect' help me resolve the error.

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

score:6

The expect().not.toBeVisible method comes from the @testing-library/jest-dom library, since there is no setup or reference to that library the default jest expect is used (thus the function is not found). A quick fix would be to add this import to the top of your test file (assuming you have already imported the library into your project via npm or yarn):

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

For scalability you may want to add a setupTest.js file (reference here: https://create-react-app.dev/docs/running-tests/)

score:10

I was facing a similar issue. In my case, it was resolved by the following steps:-

  • Adding the @testing-library/jest-dom package to the devDependencies instead of dependencies in the package.json file.

Next add one of the following:

  1. Adding import '@testing-library/jest-dom'; to the setupTests.js
  2. Or adding in jest configuration (package.json): "setupFilesAfterEnv": [ "@testing-library/jest-dom/extend-expect" ]

Related Query

More Query from same tag