-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: You are trying to import
a file after the Jest environment has been torn down.
#6434
Comments
Found #4359 Closing this issue |
I have same problem. ReferenceError: You are trying to
ReferenceError: You are trying to
● Cannot log after tests are done. Did you forget to wait for something async in your test?
package.json |
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. |
Works for me! thanks! |
Mocking the timers caused cascading errors in my tests. I'm using |
I am using it('your test case', () => {...}) becomes: it('your test case', async () => {...}) |
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 |
This works for me |
Safe my time, Thank so much my bro |
I was getting this issue while testing Apollo with react-native-testing-library. In this case there were two queries, in a parent and child component. The parent query needed to resolve before the child rendered and fired its query. The solution was to run the test("...", () => {
const rr = render(...);
await wait();
await wait(); // Child query must also run
expect(...);
}
// Wait a tick so the query runs
// https://www.apollographql.com/docs/react/development-testing/testing/#testing-final-state
export async function wait(ms = 0) {
await act(async () => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
});
} |
write this into import 'react-native/Libraries/Animated/src/bezier'; // for https://github.com/facebook/jest/issues/4710 |
I was presenting this issue in a RN project. I fixed changing "default props" in the tested component cause expected a function prop, but received an object, after add a empty function as default (() => null), solved the test problem. |
deleting node_modules and NPM I solved it for me. hope it helps someone |
I believe sometimes this happens when your mocked component has async behaviour (state updates) in it, which takes a while to complete, but your test does not wait for it and ends prematurely leading to an "Async warning" or the above error "You are trying to import a file". Now this might be a hack, but I found that the best way to solve this is to wait for a component to finish all its async behavior before we can start with the next steps. Calling an empty async callback usually does the trick. Here's an example: it('renders component and waits for all async behavior to finish ', async () => {
renderMockComponent(mockData);
await act(async () => {}); //this is where the magic happens
const allHiddenElementsAfterRendering = getAllByTestId('hiddenElements');
expect(allHiddenElementsAfterRendering).toHaveLength(x);
}); Hope this helps someone! |
We have thousands of tests and this has been bugging me for a while. The stack trace is really unhelpful in finding the offending test so it's too easy for a dev to accidentally forget to await something and their test returns a false positive. Made this patch to improve visibility of the problem.
diff --git a/node_modules/jest-runtime/build/index.js b/node_modules/jest-runtime/build/index.js
index 35d64c0..2a82fcc 100644
--- a/node_modules/jest-runtime/build/index.js
+++ b/node_modules/jest-runtime/build/index.js
@@ -1190,7 +1190,43 @@ class Runtime {
if (compiledFunction === null) {
this._logFormattedReferenceError(
- 'You are trying to `import` a file after the Jest environment has been torn down.'
+`🚨
+🚨 🚨
+🚨 🚨 🚨
+🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
+____ _____ _______ ______ _____ _______ _
+| _ \\ /\\ | __ \\ |__ __| ____|/ ____|__ __| |
+| |_) | / \\ | | | | | | | |__ | (___ | | | |
+| _ < / /\\ \\ | | | | | | | __| \\___ \\ | | | |
+| |_) / ____ \\| |__| | | | | |____ ____) | | | |_|
+|____/_/ \\_\\_____/ |_| |______|_____/ |_| (_)
+
+Got this error from Jest: You are trying to \`import\` a file after the Jest environment has been torn down.
+
+This means you have a leaky promise somewhere and your test is not working properly.
+
+Find the offending test and make sure it is async, and await any function calls inside.
+
+The stack trace here is useless (thanks Jest), but it is probably the test suite right above or below this error.
+
+Don't ignore this.
+
+🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨 🚨
+🚨 🚨 🚨 🚨
+🚨 🚨 🚨
+🚨 🚨
+🚨`,
);
process.exitCode = 1; |
Try it:
It's cleaner that: |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
I have a component that makes use of
Animated
component from react native. I started writing a test case to simulateonPress
of a component, which calls a function that hasAnimated.timing
in it, andsetState
.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:Expected behavior
Tests passes
Link to repl or repo (highly encouraged)
repl.it demo:
https://repl.it/repls/PartialGrimyMetadata
Run
npx envinfo --preset jest
The text was updated successfully, but these errors were encountered: