@@ -502,8 +502,9 @@ describe('utils', () => {
502
502
}
503
503
} ;
504
504
const { iframe, url } = setup ( message ) ;
505
+ jest . useFakeTimers ( ) ;
505
506
await runIframe ( url , origin ) ;
506
-
507
+ jest . runAllTimers ( ) ;
507
508
expect ( message . source . close ) . toHaveBeenCalled ( ) ;
508
509
expect ( window . document . body . appendChild ) . toHaveBeenCalledWith ( iframe ) ;
509
510
expect ( window . document . body . removeChild ) . toHaveBeenCalledWith ( iframe ) ;
@@ -524,20 +525,10 @@ describe('utils', () => {
524
525
] . forEach ( m => {
525
526
it ( `ignores invalid messages: ${ JSON . stringify ( m ) } ` , async ( ) => {
526
527
const { iframe, url, origin } = setup ( m ) ;
527
- /**
528
- * We need to run the timers after we start `runIframe` to simulate
529
- * the window event listener, but we also need to use `jest.useFakeTimers`
530
- * to trigger the timeout. That's why we're using a real `setTimeout`,
531
- * then using fake timers then rolling back to real timers
532
- */
533
- setTimeout ( ( ) => {
534
- jest . runAllTimers ( ) ;
535
- } , 10 ) ;
536
528
jest . useFakeTimers ( ) ;
537
- await expect ( runIframe ( url , origin ) ) . rejects . toMatchObject (
538
- TIMEOUT_ERROR
539
- ) ;
540
- jest . useRealTimers ( ) ;
529
+ const promise = runIframe ( url , origin ) ;
530
+ jest . runAllTimers ( ) ;
531
+ await expect ( promise ) . rejects . toMatchObject ( TIMEOUT_ERROR ) ;
541
532
expect ( window . document . body . removeChild ) . toHaveBeenCalledWith ( iframe ) ;
542
533
} ) ;
543
534
} ) ;
@@ -553,9 +544,11 @@ describe('utils', () => {
553
544
}
554
545
} ;
555
546
const { iframe, url } = setup ( message ) ;
547
+ jest . useFakeTimers ( ) ;
556
548
await expect ( runIframe ( url , origin ) ) . resolves . toMatchObject (
557
549
message . data . response
558
550
) ;
551
+ jest . runAllTimers ( ) ;
559
552
expect ( message . source . close ) . toHaveBeenCalled ( ) ;
560
553
expect ( window . document . body . removeChild ) . toHaveBeenCalledWith ( iframe ) ;
561
554
} ) ;
@@ -570,30 +563,22 @@ describe('utils', () => {
570
563
}
571
564
} ;
572
565
const { iframe, url } = setup ( message ) ;
566
+ jest . useFakeTimers ( ) ;
573
567
await expect ( runIframe ( url , origin ) ) . rejects . toMatchObject (
574
568
message . data . response
575
569
) ;
570
+ jest . runAllTimers ( ) ;
576
571
expect ( message . source . close ) . toHaveBeenCalled ( ) ;
577
572
expect ( window . document . body . removeChild ) . toHaveBeenCalledWith ( iframe ) ;
578
573
} ) ;
579
574
it ( 'times out after timeoutInSeconds' , async ( ) => {
580
575
const { iframe, url, origin } = setup ( '' ) ;
581
576
const seconds = 10 ;
582
- /**
583
- * We need to run the timers after we start `runIframe` to simulate
584
- * the window event listener, but we also need to use `jest.useFakeTimers`
585
- * to trigger the timeout. That's why we're using a real `setTimeout`,
586
- * then using fake timers then rolling back to real timers
587
- */
588
- setTimeout ( ( ) => {
589
- jest . runTimersToTime ( seconds * 1000 ) ;
590
- } , 10 ) ;
591
577
jest . useFakeTimers ( ) ;
592
- await expect ( runIframe ( url , origin , seconds ) ) . rejects . toMatchObject (
593
- TIMEOUT_ERROR
594
- ) ;
578
+ const promise = runIframe ( url , origin , seconds ) ;
579
+ jest . runTimersToTime ( seconds * 1000 ) ;
580
+ await expect ( promise ) . rejects . toMatchObject ( TIMEOUT_ERROR ) ;
595
581
expect ( window . document . body . removeChild ) . toHaveBeenCalledWith ( iframe ) ;
596
- jest . useRealTimers ( ) ;
597
582
} ) ;
598
583
} ) ;
599
584
describe ( 'getCrypto' , ( ) => {
0 commit comments