-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Why does flushPromises
work the way that it does
#11
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
Comments
Not an expert here, but I'll give it a try: I think it works because If there are promise callbacks already in javascript's queue pending to be processed at the time As to wether this might break in the future, if you read the This article can help understand the even loop in general. And in particular, look for the section titled "Zero delays", which talks about |
That was a great explanation @gnapse! I should note also that this is only effective in your tests if you've mocked out your async requests to resolve immediately (like the axios mock we have in our tests). I'm not worried about Anyone wanna add this to the FAQ? |
@kentcdodds I'll do it! I too thought about this being based on ajax requests being mocked, as they otherwise would not be immediately in the queue. I forgot to mention it though. Thanks for clarifying that. |
Thanks all! This tracks with what I assumed, but it's good to see it written down and I think talking about why it worksin libs that expose it is probably good. :) Also the bit about async stuff needing to resolve immediately bis pretty important. I was somehow assuming that it would it would work for all async calls but had no clue how. I suppose for end to end testing, you'd probably still need some way to wait for whatever result you were looking for to occur. (Element on the page, row change in a database, etc.) |
For end-to-end tests, check out cypress. It has an amazing way to declare the network requests you expect to occur in your test, and then declaratively wait for them to resolve before continuing the test. In that case the network requests can either be mocked or not, and the test will work anyway. |
feat: 🎸 add userEvent.type
From https://twitter.com/Dru89/status/976472959345815552
I've seen examples before where
flushPromises
is written basically the way that you say:What I don't really understand is why this actually works. I'm assuming it has something to do with scheduling and how JavaScript actually handles asynchronous published behavior.
And relatedly:
Is this a "hack" that only works because of an implementation detail in Node/V8? (Would this break in a browser or in something like Chakra?) If it's not something in the spec, is it something that's likely to change?
The text was updated successfully, but these errors were encountered: