waitFor
introduces unexpected error with rejected Promise
#1210
Labels
needs discussion
We need to discuss this to come up with a good solution
@testing-library/dom
version: 8.20.0[email protected]
[email protected]
What you did:
Here's a minimal test to reproduce the issue
What happened:
Running this change as-is produces the following error:
Console output
However, if I uncomment Line 20, that section of code now looks like:
and the tests pass as expected:
Console output
Reproduction:
You can also find this test file available at https://gist.github.com/Dru89/8c37aa36b8fe7093b97761283e9feece
The jest config is basically just the default configuration, with added support for React. I have tried this using both "fake timers" and "real timers" in jest, and it produces the same result.
Problem description:
As you can see, I'm not actually doing anything with that call to
result.catch(() => {})
, and the promise does get handled, albeit after the call toawait openWindow()
.My best guess about why this happens comes from a hint in the warnings from Node:
As I mentioned, the promise does actually get handled with the call to
await result;
and then gets caught by the surroundingtry
/catch
blocks, but I think maybe something in thewaitFor
might be causing fulfilled promises to settle, which triggers some "unhandled promise rejections" in jest/node or similar?That theory is buoyed by the fact that if I change the
reticulate
mock to reject the promise after a delay, that also makes the tests pass. Basically, take the above reproduction, but changereticulate
to be:Suggested solution:
I don't have any suggestions for possible solutions here. And I'm not even really sure if there's anything that could be done, if the problem is basically that
waitFor
is "running out" any remaining promises, and jest is maybe just listening for those errors somewhere.But it was a weird bug that I couldn't find any existing examples for after searching, and wanted to at least document this in case it helped anyone else. (And maybe it is an issue that could be fixed in how
waitFor
is implemented? 🤷)The text was updated successfully, but these errors were encountered: