@@ -335,18 +335,18 @@ test('does not work after it resolves', async () => {
335
335
} ,
336
336
} )
337
337
338
- let data = null
338
+ let timeoutResolved = false
339
339
setTimeout ( ( ) => {
340
340
contextStack . push ( 'timeout' )
341
- data = 'resolved'
341
+ timeoutResolved = true
342
342
} , 100 )
343
343
344
344
contextStack . push ( 'waitFor:start' )
345
345
await waitFor (
346
346
( ) => {
347
347
contextStack . push ( 'callback' )
348
348
// eslint-disable-next-line jest/no-conditional-in-test -- false-positive
349
- if ( data === null ) {
349
+ if ( ! timeoutResolved ) {
350
350
throw new Error ( 'not found' )
351
351
}
352
352
} ,
@@ -391,3 +391,64 @@ test('does not work after it resolves', async () => {
391
391
]
392
392
` )
393
393
} )
394
+
395
+ test ( `when fake timer is installed, on waitFor timeout, it doesn't call the callback afterward` , async ( ) => {
396
+ jest . useFakeTimers ( 'modern' )
397
+
398
+ configure ( {
399
+ // @testing -library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
400
+ unstable_advanceTimersWrapper : callback => {
401
+ try {
402
+ const result = callback ( )
403
+ // eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false-positive
404
+ if ( typeof result ?. then === 'function' ) {
405
+ const thenable = result
406
+ return {
407
+ then : ( resolve , reject ) => {
408
+ thenable . then (
409
+ returnValue => {
410
+ resolve ( returnValue )
411
+ } ,
412
+ error => {
413
+ reject ( error )
414
+ } ,
415
+ )
416
+ } ,
417
+ }
418
+ } else {
419
+ return undefined
420
+ }
421
+ } catch {
422
+ return undefined
423
+ }
424
+ } ,
425
+ asyncWrapper : async callback => {
426
+ try {
427
+ await callback ( )
428
+ } finally {
429
+ /* eslint no-empty: "off" */
430
+ }
431
+ } ,
432
+ } )
433
+
434
+ let waitForHasResolved = false
435
+ let callbackCalledAfterWaitForResolved = false
436
+
437
+ await expect ( ( ) =>
438
+ waitFor (
439
+ ( ) => {
440
+ // eslint-disable-next-line jest/no-conditional-in-test -- false-positive
441
+ if ( waitForHasResolved ) {
442
+ callbackCalledAfterWaitForResolved = true
443
+ }
444
+ throw new Error ( 'We want to timeout' )
445
+ } ,
446
+ { interval : 50 , timeout : 100 } ,
447
+ ) ,
448
+ ) . rejects . toThrow ( )
449
+ waitForHasResolved = true
450
+
451
+ await Promise . resolve ( )
452
+
453
+ expect ( callbackCalledAfterWaitForResolved ) . toBe ( false )
454
+ } )
0 commit comments