-
-
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
[Bug]: document
is null
during microtasks at the end of a test
#12670
Comments
This error is difficult for non-react savvy folks to understand. It would be great to fix the issue and add more error messaging to direct folks on what to do about it a bit more clearly. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
comment |
As discussed in testing-library/react-testing-library#1027. This issue is experienced very often while running async tests on Github Actions CI. As a mitigation I add Also, I have tests which run successfully on local machine but fail only in Github Actions CI with:
|
I have been able to consistently reproduce this error on my local machine. But to do that I have to do |
I ended up patching react-dom in my node_modules locally and added some debugging code to identify the problematic test. You can see it at https://github.com/Zylphrex/react-testing-library-error-repro. It's a little gross but I hope it helps. |
This worked excellently well! 🎉 Thank you so much @Zylphrex ! It did point me to the test and place I thought was probably the culprit. I still can't figure out why it is failing though. But I can solve the problem by adding a longer timeout for the test that is failing but it is not a solution I'm satisfied with. |
After trying random things to solve this issue, I discovered that increasing test timeout ( |
@mjchang If you were facing the same problem, that should fix it. The root cause of this problem when I initially opened up testing-library/react-testing-library#1027 was that the test timed out during one of the I believe what's happening here is because the test had timed out, jest started to do tear downs (I'm not sure exactly what). And at some point after, the |
I experienced this one today. Error is obtuse and stack trace is unhelpful, especially within a large team. Some days I fight with Jest, some days I let Jest win. I just gave up and deleted the spec. Problem solved ;) |
I believe I'm having this same issue, only the test that consistently triggers this error is empty. With or without code in the test case it gives me this error, but as soon as I comment out the entire test case, my other tests pass without an issue. My Jest version is 24.9.0 My test file:
And here is a gist with the stack trace and the component that I'm testing. When the 5th and last test is commented out (like above) my tests run. But if I do not comment it out, I get the error. The test did have code in it but removing it did not make a difference. I'm hoping someone could point me in the direction of a work around, as I'm completely stumped. |
@ramonaspence Your error looks slightly different to what I've seen but something you can try is to comment out the test that passed just before the failure (which is probably the second last test in the test suite). If this is the same problem/something similar, the previous test that passed may be leaving some asynchronous work that runs after it passes. |
Okay, thank you! I didn't consider that what I'm testing has a promise in it. |
It seems that the reason why this is rarely happening locally but often while running async tests on Github Actions CI is that those failing tests (for whatever various reasons) take up quite some time to complete and ultimately running into a timeout especially on smaller CPU machines / environments. Of course one would expect those tests to fail with a timeout. What seems to happen instead though is that said tests are getting cleared and after clearing, document-object becomes unavailable. That is the error that we are getting. Totally confusing. That's the reason why increasing timeout for those tests avoids this issue. |
Is it possible to get a reproduction without React where this is an issue? I think the underlying problem isn't jest doing something weird (we cannot inspect promise states, so we don't know to wait. And even if we could - how long are we supposed to wait?), but rather React scheduling callbacks in a way that breaks stack traces leading to very confusing errors. React throws an error, but the trace is useless - if React kept the frames from when the callback was scheduled I assume at least the error would be usable? Of course, Jest can add a wait before |
I see I pretty much just repeated what @Zylphrex said in #12670 (comment). IMO React should do that by default (or, not that as it's quite hacky 😅 but something that keeps a usable stack). Again, I agree the error from Jest is unfortunate in this case, but I'm not sure there's anything we can really do (while React definitely can do something) |
The general problem is that Jest is the only environment where |
I guess you can say the underlying issue is something like jsdom/jsdom#955. We call I doubt there's anything that we can do in Jest, unless you have any suggestions? |
is there any WA for this issue? |
As of #13972 (released in 29.5.0), we no longer set the document to If it is, please open up a new issue. |
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. |
Version
27.5.1
Steps to reproduce
jest-repro
branchyarn install
yarn test
Expected behavior
Test fails due to timeout.
Actual behavior
document
isnull
during a microtask scheduled from aReactDOMTestUtils.act
call.Additional context
Continued from #9056 (comment) which is a reduced repro of testing-library/react-testing-library#1027
Environment
The text was updated successfully, but these errors were encountered: