-
Notifications
You must be signed in to change notification settings - Fork 472
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
infinite loop when DOM mutation happens in waitFor callback #808
Comments
Thanks for this @drudv. Perhaps this is related to the change in #801. Cc @romain-trotard |
Hi guys.
(The waitFor fails the first time and pass the second one) |
Maybe I should provide some details on how we came to this problem. There was the following code in our tests:
At the first glance it doesn't do any DOM mutations. But there was a listener for click events that cause changes in the DOM. Since we were using Jest 24 with MutationObserver shim, the problem occurred only sometimes and led to failures in
However, it's strange that such innocent code (although I agree it's not great that it produces DOM mutations as a side effect) hangs the whole Jest. |
Oh, yeah doing mutations in |
I agree that it's better not to put interactions and we should mention it in the docs. But IMO Maybe I will try to go deeper and prepare some solution for the problem later. |
That makes sense to me @drudv. This definitely sounds like a bug 👍 Thanks for any assistance you can offer! |
I don't know if we can do something for this, I think that we can only update the doc. The problem is that mutating the dom inside the callback will cause an infinite recursive check. When the attribute is set |
The same behavior is also happening in the browser https://jsfiddle.net/g3sdnkyu/, so I'm pretty sure that we can also add a note in the doc |
Could we possibly add a warning that detects a loop, or should this only be a documentation change? |
I don't how we can detects this loop. We can in someway make it works wrapping the callback in a |
I wonder what impact putting the callback in the next tick of the event loop would have on perf 🤔 @romain-trotard's investigation showed that removing a simple |
Personnaly, I would prefer to just update the documentation. And clarify that we should not do mutation or interaction in |
I'd love to have the perf improvement and help people avoid making this mistake if possible. Either way, I think it's safe to document this right now. |
I do not really understand how some env infos:
|
The very first step of waitFor with or without fake timers is to check the callback. (https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js#L56 or https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js#L96) await waitFor(() => expect(condition).toBeTruthy()) |
@ryuuji3 Thanks. At first sight it was not clear to me that an exception has to be thrown for it to work. I was overwhelmed by the documentation and could not find the information you describe immediately. Later I found out by debugging it myself. Unfortunately I hadn't thought to report this here anymore. |
Because ATL invokes an Angular detection cycle within the Would it be an option to keep track of the execution time inside If we want to add this to DTL, I can create a PR for this and we can take it from there. |
Another possibility would be to send the trigger (interval timer or observer) to the callback. For ATL specific, we can ignore an Angular change detection cycle when the callback is invoked by the observer. |
Jest hangs on the following sample test:
Two conditions have to be met:
Expected result:
waitFor should resolve the promise after a successful iteration regardless whether there were DOM mutations or not
Current result:
waitFor calls the callback infinitely even if subsequent iterations don't throw any exception.
Environment:
@testing-library/dom@7.26.4
jest@26.6.2
The text was updated successfully, but these errors were encountered: