ReferenceError: expect is not defined

See original GitHub issue

When I add import '@testing-library/jest-native/extend-expect'; inside my setup.js which is referenced in setupFiles inside my jest-config.json, I get this error on every test:

ReferenceError: expect is not defined

When I reference it in setupFilesAfterEnv instead, it’s all working fine.

I’d be happy to push a PR to add that gotcha in the README if the maintainers thing it makes sense.

  • react-native: 0.63.2
  • @testing-library/react-native: 7.0.2
  • jest: 26.5.2
  • node version: 10.16.3

Relevant code or config:

{
  "preset": "react-native",
  "globals": {
    "__TEST__": true,
    "window": {}
  },
  "rootDir": "../",
  "setupFiles": [
    "./jest/setup.js",
    "./node_modules/react-native-gesture-handler/jestSetup.js"
  ],
  "setupFilesAfterEnv": [
    "./jest/setupAfterEnv.js"
  ],
...
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
M-Jascommented, Dec 20, 2020

I know this is closed but I wanted to add an alternative solution because the suggestions above didn’t work in my case.

Keep import '@testing-library/jest-native/extend-expect'; in your setup file.

Then move your setup file call in your package.json from:

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

to

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

This might not work for everyone, but I wanted to share my mix. 👋

0reactions
JaapWeijlandcommented, May 25, 2022

Note for people like me who are using the jest extension in vs code: restart your IDE (or the jest runner) after making @M-Jas’ change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I solve "ReferenceError: expect is not defined" error ...
Try. First, in the terminal. npm install expect.js. And in your code:
Read more >
"expect is not defined" when using with jest · Issue #122 - GitHub
Test suite failed to run ReferenceError: expect is not defined at Object. ... "expect is not defined" when importing jest-enzyme in setup.js ...
Read more >
How to fix an error about “expect is not defined” | by Kyosuke Ito
I downgraded node version from latest(v 17.01) to stable (v 14.8) by using “n stable” command(in my case, I had to use “sudo”...
Read more >
How can I solve "ReferenceError: expect is not defined" error ...
JavaScript : How can I solve " ReferenceError : expect is not defined " error message? [ Gift : Animated Search Engine ...
Read more >
React + Jest Testing Error - ReferenceError: expect is not ...
Coding example for the question React + Jest Testing Error - ReferenceError: expect is not defined-Reactjs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found