@@ -204,21 +204,25 @@ 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 ;
207
+ const blockThread = ( timeToBlockThread : number , legacyFakeTimers : boolean ) => {
208
+ jest . useRealTimers ( ) ;
209
+ let end = Date . now ( ) + timeToBlockThread ;
210
+
211
+ while ( Date . now ( ) < end ) {
212
+ // do nothing
210
213
}
211
214
212
- return fibonaci ( n - 1 ) + fibonaci ( n - 2 ) ;
215
+ jest . useFakeTimers ( { legacyFakeTimers } ) ;
213
216
} ;
214
217
215
- test . each ( [ false , true ] ) (
218
+ test . each ( [ true , false ] ) (
216
219
'it should not depend on real time when using fake timers (legacyFakeTimers = %s)' ,
217
- async ( ) => {
218
- jest . useFakeTimers ( { legacyFakeTimers : false } ) ;
220
+ async ( legacyFakeTimers ) => {
221
+ jest . useFakeTimers ( { legacyFakeTimers } ) ;
219
222
220
223
const mockErrorFn = jest . fn ( ( ) => {
221
- fibonaci ( 30 ) ;
224
+ // Wait 10 seconds so that check time is longer than interval
225
+ blockThread ( 10 , legacyFakeTimers ) ;
222
226
throw new Error ( 'test' ) ;
223
227
} ) ;
224
228
@@ -227,10 +231,13 @@ test.each([false, true])(
227
231
timeout : 200 ,
228
232
interval : 5 ,
229
233
} ) ;
230
- } catch ( error ) {
231
- // suppress
234
+ } catch ( e ) {
235
+ // do nothing
232
236
}
233
237
238
+ // Verify that even though time to perform check is longer than interval
239
+ // test won't timeout until number of checks * interval >= timeout
240
+ // ie fake timers have been advanced by timeout when waitfor rejects
234
241
expect ( mockErrorFn ) . toHaveBeenCalledTimes ( 41 ) ;
235
242
}
236
243
) ;
0 commit comments