Jest-dom: "expect is not defined" when using with jest

Created on 14 Aug 2019  ยท  9Comments  ยท  Source: testing-library/jest-dom

  • @testing-library/jest-dom version: 4.0.0
  • node version: 12.8.0
  • npm (or yarn) version: 6.10.2

Relevant code or config:

Jest config

module.exports = {
  snapshotSerializers: ['jest-emotion'],
  setupFilesAfterEnv: ['./config/jest.js']
};

jest.js file

require('@testing-library/jest-dom/extend-expect');

What you did:


run the npm test

What happened:

Getting error expect is not defined ...

Determining test suites to run...

  โ— Test suite failed to run

    ReferenceError: expect is not defined

      at Object.<anonymous> (node_modules/@testing-library/jest-dom/dist/extend-expect.js:7:1)
      at Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7)
      at Object.<anonymous> (node_modules/@testing-library/jest-dom/extend-expect.js:2:1)

Reproduction:

https://github.com/tomitrescak/react-boilerplate/tree/Demo

Problem description:

I cannot run my tests

Suggested solution:

Most helpful comment

Just to collate the above together without having to click about through links and do _reading_, my solution was:

In jest.config.js, replace:

setupFiles: [
  './src/__setup__/setupTests.js',
],

With:

setupFilesAfterEnv: [
  './src/__setup__/setupTests.js',
],

All 9 comments

Found the culprit. Sorry for bothering. The same setup file was run in the global setup.

If anyone else who hits a roadblock here like me, have a look at this comment to solve the issue: https://github.com/FormidableLabs/enzyme-matchers/issues/86#issuecomment-312489052

Ran into this issue on CI server runnning node v10.10 after upgrading the following dependencies:

* "jest": "^25.1.0",  --> "^26.0.1"
* "@testing-library/jest-dom": "^5.1.1" --> "^5.9.0"
* "@testing-library/react": "^9.5.0" --> "^10.2.1"

and having switched from running tests with --env=jsdomto --env=jest-environment-jsdom-sixteen

Can confirm that upgrading node to v10.15 fixed the problem, assuming the jest setup file config is correctly defined.

Just to collate the above together without having to click about through links and do _reading_, my solution was:

In jest.config.js, replace:

setupFiles: [
  './src/__setup__/setupTests.js',
],

With:

setupFilesAfterEnv: [
  './src/__setup__/setupTests.js',
],

had the same issue, only in CI, but the setup file was under setupFilesAfterEnv, as expected. upgraded node from 10 -> 12 and it was resolved.

Thanks, @MaffooBristol! Your solution works!

If anyone is using Vue Test Utils and still having this problem after trying these solutions, make sure all of your packages are up to date in package.json then delete your node modules folder and run npm install again.

"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-unit-jest": "^4.5.4",
"@vue/cli-service": "~4.5.0",
"@vue/test-utils": "^1.0.3",

Just to add my findings: you can do the exact same thing as a command line option when running an npm script. I needed this because I was using react-scripts, and wanted to change the default name (setupTests.js) to snake case to fit the rest of my codebase (setup-tests.js), without adding a jest.config.js file. Ahh it's the little things isn't it.

In my package.json scripts entries I had
"test": "react-scripts test --setupFiles <test setup filepath>
and it was giving me this error. Fixed by changing to
"test": "react-scripts test --setupFilesAfterEnv <test setup filepath>

If anyone is using Vue Test Utils and still having this problem after trying these solutions, make sure all of your packages are up to date in package.json then delete your node modules folder and run npm install again.

"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-unit-jest": "^4.5.4",
"@vue/cli-service": "~4.5.0",
"@vue/test-utils": "^1.0.3",

Sorry this was not working for me I was forced to import the expect function from Chai

Was this page helpful?
0 / 5 - 0 ratings