Skip to content
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

fix(cleanup): microtask flushing now supports fake timers #720

Merged
merged 1 commit into from Jun 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/flush-microtasks.js
Expand Up @@ -3,9 +3,18 @@
// and the part that is not cannot easily have useful tests written
// anyway. So we're just going to ignore coverage for this file
/**
* copied from React's enqueueTask.js
* copied and modified from React's enqueueTask.js
*/

function getIsUsingFakeTimers() {
return (
typeof jest !== 'undefined' &&
typeof setTimeout !== 'undefined' &&
(setTimeout.hasOwnProperty('_isMockFunction') ||
setTimeout.hasOwnProperty('clock'))
)
}

let didWarnAboutMessageChannel = false
let enqueueTask
try {
Expand Down Expand Up @@ -43,7 +52,15 @@ try {
export default function flushMicroTasks() {
return {
then(resolve) {
enqueueTask(resolve)
if (getIsUsingFakeTimers()) {
// without this, a test using fake timers would never get microtasks
// actually flushed. I spent several days on this... Really hard to
// reproduce the problem, so there's no test for it. But it works!
jest.advanceTimersByTime(0)
resolve()
} else {
enqueueTask(resolve)
}
},
}
}