-
Notifications
You must be signed in to change notification settings - Fork 466
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
waitFor errors with a weird error if a test using fake timers stops (because of an error for example) while it's waiting #830
Comments
I checked that we couldn't have an infinite loop because of the tight loop by simply adding more timers, either jest or testing-library stops at one point. |
I'm not sure this is caused by testing library. I suspect that this can happen to any async test where we switch back to real timers in Generally curious how async cleanup logic is supposed to be handled. From the current behavior I can only assume that it has to know how the "clock works" i.e. are timers automatically advanced or should the cleanup logic advance them? |
Well, this is not what I'm seeing here. Actually there are no fake setTimeout in this failing test... (of course there are some in my real test). So I'm not sure about what you're saying about rescheduling dangling fake timers. |
|
I believe that's not true.
Am I missing something? |
I think it's reasonable to add a check in our while loop to detect whether we're still using fake timers and if we're not throw a helpful error message. Can probably do the opposite as well (when going from no fake timers to fake timers). This could help avoid this problem. I'm working on this now. |
PR Open: #832 |
Thanks @kentcdodds ! BTW I was wondering if we could avoid the loop altogether, by replacing dom-testing-library/src/wait-for.js Line 68 in 9494fdc
with jest.advanceTimersByTime(timeout) Probably a bad idea (this could run more app code than the current behavior), but I thought it's worth considering because this reduces the complexity of this code. |
Whoops, you're correct. This feature is still pending: jestjs/jest#6876 |
🎉 This issue has been resolved in version 7.28.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@testing-library/dom
version: 7.26.3 (but I also reproduce in 7.27.0)Relevant code or config:
See below.
What you did:
Simply using
findByText
in some promise-based code while using fake timers, working on a new test.What happened:
This is the error I get:
One main issue is that the error is repeated indefinitely, I have to ctrl-c to stop the runner. It also obscures the real error happening before.
Reproduction:
This is the simplest code showing the problem:
Obviously in this code I forgot to "await"
waitFor
, as a result the test stops before waitFor ends.An example closer to the real life is this:
The problem is that the test errors before waitFor finishes, fake timers are torn down automatically, but the code in waitFor still wants to use the fake timers.
I couldn't make it fail on codesandbox, I believe because
setImmediate
is aliased tosetTimeout
in browsers, but here is a repository where I reproduce locally: https://github.com/julienw/jest-dom-library-issueSuggested solution:
I believe that the most important issue is that this never stops. The timeout should set
finished
to false... but I think that the timeout never runs because the loop is too "tight" (it's usingsetImmediate
, maybe it's using a microtask, but couldn't find anything conclusive about that -- but probably it has precedence oversetTimeout
).Note: I could live with one such log that would let me see easily the real error.
Other ideas:
beforeEach
andafterEach
callbacks to get notified of when a test starts and stops?But again I could live with these 2 problems if this wasn't looping forever.
The text was updated successfully, but these errors were encountered: