File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -204,6 +204,37 @@ test.each([false, true])(
204
204
}
205
205
) ;
206
206
207
+ const fibonaci = ( n : number ) : number => {
208
+ if ( n === 0 || n === 1 ) {
209
+ return 1 ;
210
+ }
211
+
212
+ return fibonaci ( n - 1 ) + fibonaci ( n - 2 ) ;
213
+ } ;
214
+
215
+ test . each ( [ false , true ] ) (
216
+ 'it should not depend on real time when using fake timers (legacyFakeTimers = %s)' ,
217
+ async ( ) => {
218
+ jest . useFakeTimers ( { legacyFakeTimers : false } ) ;
219
+
220
+ const mockErrorFn = jest . fn ( ( ) => {
221
+ fibonaci ( 30 ) ;
222
+ throw new Error ( 'test' ) ;
223
+ } ) ;
224
+
225
+ try {
226
+ await waitFor ( ( ) => mockErrorFn ( ) , {
227
+ timeout : 200 ,
228
+ interval : 5 ,
229
+ } ) ;
230
+ } catch ( error ) {
231
+ // suppress
232
+ }
233
+
234
+ expect ( mockErrorFn ) . toHaveBeenCalledTimes ( 41 ) ;
235
+ }
236
+ ) ;
237
+
207
238
test . each ( [ false , true ] ) (
208
239
'awaiting something that succeeds before timeout works with fake timers (legacyFakeTimers = %s)' ,
209
240
async ( legacyFakeTimers ) => {
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ function waitForInternal<T>(
38
38
let finished = false ;
39
39
let promiseStatus = 'idle' ;
40
40
41
- const overallTimeoutTimer = setTimeout ( handleTimeout , timeout ) ;
41
+ let overallTimeoutTimer : NodeJS . Timeout | null = null ;
42
42
43
43
const usingFakeTimers = jestFakeTimersAreEnabled ( ) ;
44
44
@@ -64,6 +64,7 @@ function waitForInternal<T>(
64
64
65
65
// when fake timers are used we want to simulate the interval time passing
66
66
if ( fakeTimeRemaining <= 0 ) {
67
+ handleTimeout ( ) ;
67
68
return ;
68
69
} else {
69
70
fakeTimeRemaining -= interval ;
@@ -90,6 +91,7 @@ function waitForInternal<T>(
90
91
await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
91
92
}
92
93
} else {
94
+ overallTimeoutTimer = setTimeout ( handleTimeout , timeout ) ;
93
95
intervalId = setInterval ( checkRealTimersCallback , interval ) ;
94
96
checkExpectation ( ) ;
95
97
}
@@ -98,7 +100,9 @@ function waitForInternal<T>(
98
100
done : { type : 'result' ; result : T } | { type : 'error' ; error : unknown }
99
101
) {
100
102
finished = true ;
101
- clearTimeout ( overallTimeoutTimer ) ;
103
+ if ( overallTimeoutTimer ) {
104
+ clearTimeout ( overallTimeoutTimer ) ;
105
+ }
102
106
103
107
if ( ! usingFakeTimers ) {
104
108
clearInterval ( intervalId ) ;
You can’t perform that action at this time.
0 commit comments