jest: ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
🐛 Bug Report
I have a component that makes use of Animated component from react native. I started writing a test case to simulate onPress of a component, which calls a function that has Animated.timing in it, and setState.
jest works fine, but the tests never stops running, and one unrelated test case that I’ve written before never seem to pass now (which passed before).
running jest --watch, I get this error:
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
at Function.bezier (node_modules/react-native/Libraries/Animated/src/Easing.js:113:21)
at ease (node_modules/react-native/Libraries/Animated/src/Easing.js:34:24)
at TimingAnimation._easing (node_modules/react-native/Libraries/Animated/src/Easing.js:133:18)
at TimingAnimation.onUpdate (node_modules/react-native/Libraries/Animated/src/animations/TimingAnimation.js:107:45)
RUNS src/__tests__/SlideDownMenu.test.js
/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:114
return _bezier(x1, y1, x2, y2);
^
TypeError: _bezier is not a function
at Function.bezier (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:224:12)
at ease (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:94:21)
at TimingAnimation._easing (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/Easing.js:255:16)
at TimingAnimation.onUpdate (/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native/Libraries/Animated/src/animations/TimingAnimation.js:138:14)
at ontimeout (timers.js:386:11)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
Expected behavior
Tests passes
Link to repl or repo (highly encouraged)
repl.it demo: https://repl.it/repls/PartialGrimyMetadata
Run npx envinfo --preset jest
Environment:
OS: Linux 4.14
Node: 6.14.2
Yarn: 1.7.0
npm: 3.10.10
Watchman: Not Found
Xcode: N/A
Android Studio: Not Found
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 21
- Comments: 17
I am on react-native 0.60 was struggling with same error
I solved it by adding jest.useFakeTimers();
With above it’s extremely important to understand this
jest.useFakeTimers() mocks out setTimeout and other timer functions with mock functions.
If running multiple tests inside of one file or describe block, jest.useFakeTimers(); can be called before each test manually or with a setup function such as beforeEach.
Not doing so will result in the internal usage counter not being reset.
I am using
react-native-testing-libraryand what resolved this error for me was to simply make the callback of the test caseasynclikebecomes:
Mocking the timers caused cascading errors in my tests.
I’m using
testing-libraryand was able to fix this issue by addingafterEach(cleanup)before my render helper function definition.I have same problem.
ReferenceError: You are trying to
importa file after the Jest environment has been torn down.ReferenceError: You are trying to
importa file after the Jest environment has been torn down.● Cannot log after tests are done. Did you forget to wait for something async in your test? Attempted to log "The above error occurred in the <NavigationContainer> component: in NavigationContainer (created by _default) in View (created by Component) in Component (at Root.js:13) in Root (created by Styled(Root)) in Styled(Root) (created by _default) in _default (created by Setup) in PersistGate (created by Setup) in Provider (created by Setup) in StyleProvider (created by Setup) in Setup (created by App) in App
package.json { “main”: “node_modules/expo/AppEntry.js”, “scripts”: { “start”: “expo start”, “android”: “expo start --android”, “ios”: “expo start --ios”, “eject”: “expo eject”, “test”: “node_modules/.bin/jest”, “test:debug”: “node --inspect-brk node_modules/jest/bin/jest.js --runInBand” }, “dependencies”: { “@builderx/icons”: “^0.1.7”, “@builderx/utils”: “^0.1.6”, “@expo/vector-icons”: “^9.0.0”, “@expo/videoplayer”: “^0.4.0”, “dot-prop-immutable”: “^1.5.0”, “expo”: “^32.0.6”, “lodash”: “^4.17.11”, “moment”: “^2.24.0”, “native-base”: “^2.12.1”, “query-string”: “^6.2.0”, “react”: “16.5.0”, “react-native”: “https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz”, “react-native-axios”: “^0.17.1”, “react-native-easy-grid”: “^0.2.1”, “react-native-elements”: “^1.1.0”, “react-native-htmlview”: “^0.13.0”, “react-native-modalbox”: “^1.7.1”, “react-native-progress”: “^3.6.0”, “react-native-stopwatch-timer”: “0.0.20”, “react-navigation”: “^3.0.9”, “react-redux”: “^6.0.0”, “redux”: “^4.0.1”, “redux-form”: “^8.1.0”, “redux-persist”: “^5.10.0”, “redux-saga”: “^1.0.1”, “redux-thunk”: “^2.3.0”, “reduxsauce”: “^1.0.1” }, “devDependencies”: { “babel-preset-expo”: “^5.0.0”, “jest”: “^24.5.0”, “jest-expo”: “^32.0.0”, “react-test-renderer”: “^16.8.6”, “remote-redux-devtools”: “^0.5.16” }, “jest”: { “preset”: “jest-expo”, “transformIgnorePatterns”: [ “node_modules/(?!((jest-)?react-native|react-clone-referenced-element|expo(nent)?|@expo(nent)?/.|react-navigation|@react-navigation/.|sentry-expo|native-base|@builderx))” ] }, “private”: true }
Thanks, this pointed me in the right direction!
I’d got this error with a dangling Promise in my test. Had an async function but missed an
await.Try it:
It’s cleaner that:
it('your test case', async () => {...})