@@ -64,6 +64,8 @@ function waitFor(
64
64
65
65
const wasUsingJestFakeTimers = jestFakeTimersAreEnabled ( )
66
66
if ( wasUsingJestFakeTimers ) {
67
+ const { advanceTimersWrapper} = getConfig ( )
68
+
67
69
// this is a dangerous rule to disable because it could lead to an
68
70
// infinite loop. However, eslint isn't smart enough to know that we're
69
71
// setting finished inside `onDone` which will be called when we're done
@@ -73,26 +75,31 @@ function waitFor(
73
75
try {
74
76
// jest.advanceTimersByTime will not throw if
75
77
ensureInvariantTimers ( )
78
+
79
+ advanceTimersWrapper ( ( ) => {
80
+ // we *could* (maybe should?) use `advanceTimersToNextTimer` but it's
81
+ // possible that could make this loop go on forever if someone is using
82
+ // third party code that's setting up recursive timers so rapidly that
83
+ // the user's timer's don't get a chance to resolve. So we'll advance
84
+ // by an interval instead. (We have a test for this case).
85
+ jest . advanceTimersByTime ( interval )
86
+ } )
87
+
88
+ // In this rare case, we *need* to wait for in-flight promises
89
+ // to resolve before continuing. We don't need to take advantage
90
+ // of parallelization so we're fine.
91
+ // https://stackoverflow.com/a/59243586/971592
92
+ // eslint-disable-next-line no-await-in-loop
93
+ await advanceTimersWrapper ( async ( ) => {
94
+ await new Promise ( r => {
95
+ setTimeout ( r , 0 )
96
+ jest . advanceTimersByTime ( 0 )
97
+ } )
98
+ } )
76
99
} catch ( error ) {
77
100
reject ( error )
78
101
return
79
102
}
80
- // we *could* (maybe should?) use `advanceTimersToNextTimer` but it's
81
- // possible that could make this loop go on forever if someone is using
82
- // third party code that's setting up recursive timers so rapidly that
83
- // the user's timer's don't get a chance to resolve. So we'll advance
84
- // by an interval instead. (We have a test for this case).
85
- jest . advanceTimersByTime ( interval )
86
-
87
- // In this rare case, we *need* to wait for in-flight promises
88
- // to resolve before continuing. We don't need to take advantage
89
- // of parallelization so we're fine.
90
- // https://stackoverflow.com/a/59243586/971592
91
- // eslint-disable-next-line no-await-in-loop
92
- await new Promise ( r => {
93
- setTimeout ( r , 0 )
94
- jest . advanceTimersByTime ( 0 )
95
- } )
96
103
}
97
104
}
98
105
0 commit comments