From c4378c62fdc9a7e7bd2fccfaa8641e394a0a94d5 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Wed, 24 Jun 2020 12:41:09 -0600 Subject: [PATCH] fix(cleanup): microtask flushing now supports fake timers --- src/flush-microtasks.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/flush-microtasks.js b/src/flush-microtasks.js index e309fe6a..0f3dbfaf 100644 --- a/src/flush-microtasks.js +++ b/src/flush-microtasks.js @@ -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() { + const globalObj = typeof window === 'undefined' ? global : window + return ( + typeof jest !== 'undefined' && + (globalObj.setTimeout?.hasOwnProperty('_isMockFunction') || + globalObj.setTimeout?.hasOwnProperty('clock')) + ) +} + let didWarnAboutMessageChannel = false let enqueueTask try { @@ -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) + } }, } }