File tree Expand file tree Collapse file tree 1 file changed +24
-2
lines changed Expand file tree Collapse file tree 1 file changed +24
-2
lines changed Original file line number Diff line number Diff line change 3
3
// and the part that is not cannot easily have useful tests written
4
4
// anyway. So we're just going to ignore coverage for this file
5
5
/**
6
- * copied from React's enqueueTask.js
6
+ * copied and modified from React's enqueueTask.js
7
7
*/
8
8
9
+ // the jest fake timers bit is borrowed from DOM Testing Library
10
+ // and is copy/pasted rather than imported because I'm not sure
11
+ // we want to expose this functionality from DOM Testing Library
12
+ // just yet (or ever)
13
+ const globalObj = typeof window === 'undefined' ? global : window
14
+
15
+ function getIsUsingFakeTimers ( ) {
16
+ return (
17
+ typeof jest !== 'undefined' &&
18
+ ( globalObj . setTimeout ?. hasOwnProperty ( '_isMockFunction' ) ||
19
+ globalObj . setTimeout ?. hasOwnProperty ( 'clock' ) )
20
+ )
21
+ }
22
+
9
23
let didWarnAboutMessageChannel = false
10
24
let enqueueTask
11
25
try {
43
57
export default function flushMicroTasks ( ) {
44
58
return {
45
59
then ( resolve ) {
46
- enqueueTask ( resolve )
60
+ if ( getIsUsingFakeTimers ( ) ) {
61
+ // without this, a test using fake timers would never get microtasks
62
+ // actually flushed. I spent several days on this... Really hard to
63
+ // reproduce the problem, so there's no test for it. But it works!
64
+ jest . advanceTimersByTime ( 0 )
65
+ resolve ( )
66
+ } else {
67
+ enqueueTask ( resolve )
68
+ }
47
69
} ,
48
70
}
49
71
}
You can’t perform that action at this time.
0 commit comments