-
Notifications
You must be signed in to change notification settings - Fork 273
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
refactor: flush microtask queue implementation #1374
Conversation
src/flushMicroTasks.ts
Outdated
}, | ||
}; | ||
export function flushMicroTasks() { | ||
return new Promise((resolve) => setImmediate(resolve)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the most straightforward and correct way to wait for setImmediate
. But the original comment said that this implementation breaks with modern fake timers? That turns out to be not true, doesn't it?
By the way, there's one more setImmediate
await that could be replaced with flushMicroTasks
, in the waitForInternal
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you test it with older versions of Jest and see where they fixed the implementation?
bb2d115
to
37cfbec
Compare
I tried to debug why the Before that PR, React Native's With recent versions of React Native, and the current |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mdjastrzebski we might need a note somewhere that this is only compatible with newer version of RN
That Or, alternatively, it could advance the timers, like: new Promise(resolve => {
setImmediate(() => {
resolve();
if (jestFakeTimersEnabled()) {
jest.advanceTimersByTime(0);
}
});
}); The Here the situation is a bit different, the timers need to be advanced after |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dcalhoun adding you, as you seem knowledgeable in this area. Could you pls share your comments and/or concerns.
Thanks for the ping. I do not have any specific concerns at this time.
@jsnajdr's original concern makes sense. Also, he has done a fair amount of in-depth research into our project's tests. I believe he has a great understanding of the domain.
Additionally, the tests (added in #568) covering one of my original discoveries related to Promise
and fake timers appear to still be present and passing in this PR. 🎉
I've done some tests with our current setup (which includes custom jest preset saving the Promise):
When I swap the preset to
|
37cfbec
to
4263696
Compare
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1374 +/- ##
==========================================
+ Coverage 96.88% 96.89% +0.01%
==========================================
Files 65 65
Lines 3693 3709 +16
Branches 538 539 +1
==========================================
+ Hits 3578 3594 +16
Misses 115 115
☔ View full report in Codecov by Sentry. |
@jsnajdr @thymikee I've updated the PR with a different approach when we delay the release of changes to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good preparation for future deprecation 👍
This PR has been released in v12.1.3 🚢 |
Summary
Edit
This PR renames the old
flushMicroTasks
functions asflushMicroTasksLegacy
, and adds a proper one under the name offlushMicroTasks
. Respective implementations are invoked in the sample places as before, so there should be no behavior change for the users. We will delay removal offlushMicroTasksLegacy
until the next major release, to avoid introducing breaking changes mid-cycle.Original
PR to fix
flushMicrotaskQueue
implemantion based on @jsnajdr comment:This is the implementation in question:
Test plan
All current tests should pass.
Reviewers
@jsnajdr pls review and share you comments.
@mikeduminy, @dcalhoun adding you, as you seem knowledgeable in this area. Could you pls share your comments and/or concerns.