ReferenceError: expect is not defined
See original GitHub issueWhen 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.2jest: 26.5.2nodeversion: 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:
- Created 3 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.jsonfrom:to
This might not work for everyone, but I wanted to share my mix. 👋
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.