@@ -29,7 +29,8 @@ describe('mutations', () => {
29
29
} ,
30
30
} )
31
31
32
- await mutation . mutate ( null )
32
+ mutation . mutate ( null )
33
+ await vi . advanceTimersByTimeAsync ( 0 )
33
34
34
35
expect ( variables ) . toBe ( null )
35
36
} )
@@ -42,14 +43,15 @@ describe('mutations', () => {
42
43
mutationFn : fn ,
43
44
} )
44
45
45
- await executeMutation (
46
+ executeMutation (
46
47
queryClient ,
47
48
{
48
49
mutationKey : key ,
49
50
} ,
50
51
'vars' ,
51
52
)
52
53
54
+ await vi . advanceTimersByTimeAsync ( 0 )
53
55
expect ( fn ) . toHaveBeenCalledTimes ( 1 )
54
56
expect ( fn ) . toHaveBeenCalledWith ( 'vars' )
55
57
} )
@@ -106,7 +108,7 @@ describe('mutations', () => {
106
108
submittedAt : expect . any ( Number ) ,
107
109
} )
108
110
109
- await vi . advanceTimersByTimeAsync ( 5 )
111
+ await vi . advanceTimersByTimeAsync ( 0 )
110
112
111
113
expect ( states [ 1 ] ) . toEqual ( {
112
114
context : 'todo' ,
@@ -126,7 +128,7 @@ describe('mutations', () => {
126
128
submittedAt : expect . any ( Number ) ,
127
129
} )
128
130
129
- await vi . advanceTimersByTimeAsync ( 20 )
131
+ await vi . advanceTimersByTimeAsync ( 10 )
130
132
131
133
expect ( states [ 2 ] ) . toEqual ( {
132
134
context : 'todo' ,
@@ -153,7 +155,7 @@ describe('mutations', () => {
153
155
sleep ( 20 ) . then ( ( ) => Promise . reject ( new Error ( 'err' ) ) ) ,
154
156
onMutate : ( text ) => text ,
155
157
retry : 1 ,
156
- retryDelay : 1 ,
158
+ retryDelay : 10 ,
157
159
} )
158
160
159
161
const states : Array < MutationState < string , unknown , string , string > > = [ ]
@@ -184,7 +186,7 @@ describe('mutations', () => {
184
186
submittedAt : expect . any ( Number ) ,
185
187
} )
186
188
187
- await vi . advanceTimersByTimeAsync ( 10 )
189
+ await vi . advanceTimersByTimeAsync ( 0 )
188
190
189
191
expect ( states [ 1 ] ) . toEqual ( {
190
192
context : 'todo' ,
@@ -292,7 +294,6 @@ describe('mutations', () => {
292
294
} )
293
295
294
296
void queryClient . resumePausedMutations ( )
295
- await vi . advanceTimersByTimeAsync ( 0 )
296
297
297
298
// check that the mutation is correctly resumed
298
299
expect ( mutation . state ) . toEqual ( {
@@ -307,7 +308,7 @@ describe('mutations', () => {
307
308
submittedAt : 1 ,
308
309
} )
309
310
310
- await vi . advanceTimersByTimeAsync ( 20 )
311
+ await vi . advanceTimersByTimeAsync ( 10 )
311
312
312
313
expect ( mutation . state ) . toEqual ( {
313
314
context : 'todo' ,
@@ -371,7 +372,9 @@ describe('mutations', () => {
371
372
} ,
372
373
} )
373
374
374
- await mutation . mutate ( undefined , { onSuccess, onSettled } )
375
+ mutation . mutate ( undefined , { onSuccess, onSettled } )
376
+
377
+ await vi . advanceTimersByTimeAsync ( 0 )
375
378
expect ( mutation . getCurrentResult ( ) . data ) . toEqual ( 'update' )
376
379
expect ( onSuccess ) . not . toHaveBeenCalled ( )
377
380
expect ( onSettled ) . not . toHaveBeenCalled ( )
@@ -387,7 +390,9 @@ describe('mutations', () => {
387
390
} ,
388
391
} )
389
392
390
- await mutation . mutate ( undefined , { onSuccess, onSettled } )
393
+ mutation . mutate ( undefined , { onSuccess, onSettled } )
394
+
395
+ await vi . advanceTimersByTimeAsync ( 0 )
391
396
expect ( mutation . getCurrentResult ( ) . data ) . toEqual ( 'update' )
392
397
expect ( onSuccess ) . not . toHaveBeenCalled ( )
393
398
expect ( onSettled ) . not . toHaveBeenCalled ( )
@@ -479,7 +484,7 @@ describe('mutations', () => {
479
484
isPaused : true ,
480
485
} )
481
486
482
- await vi . runAllTimersAsync ( )
487
+ await vi . advanceTimersByTimeAsync ( 20 )
483
488
484
489
expect ( results ) . toStrictEqual ( [
485
490
'start-A' ,
@@ -524,7 +529,7 @@ describe('mutations', () => {
524
529
'vars2' ,
525
530
)
526
531
527
- await vi . runAllTimersAsync ( )
532
+ await vi . advanceTimersByTimeAsync ( 10 )
528
533
529
534
expect ( results ) . toStrictEqual ( [
530
535
'start-A' ,
@@ -601,7 +606,7 @@ describe('mutations', () => {
601
606
'vars2' ,
602
607
)
603
608
604
- await vi . runAllTimersAsync ( )
609
+ await vi . advanceTimersByTimeAsync ( 20 )
605
610
606
611
expect ( results ) . toStrictEqual ( [
607
612
'start-A1' ,
@@ -620,7 +625,7 @@ describe('mutations', () => {
620
625
const key = queryKey ( )
621
626
const results : Array < string > = [ ]
622
627
623
- await executeMutation (
628
+ executeMutation (
624
629
queryClient ,
625
630
{
626
631
mutationKey : key ,
@@ -645,6 +650,8 @@ describe('mutations', () => {
645
650
'vars' ,
646
651
)
647
652
653
+ await vi . advanceTimersByTimeAsync ( 0 )
654
+
648
655
expect ( results ) . toEqual ( [
649
656
'onMutate-sync' ,
650
657
'onSuccess-implicit-void' ,
@@ -663,12 +670,12 @@ describe('mutations', () => {
663
670
mutationFn : ( ) => Promise . resolve ( 'success' ) ,
664
671
onMutate : async ( ) => {
665
672
results . push ( 'onMutate-async' )
666
- await sleep ( 1 )
673
+ await sleep ( 10 )
667
674
return { backup : 'async-data' }
668
675
} ,
669
676
onSuccess : async ( ) => {
670
677
results . push ( 'onSuccess-async-start' )
671
- await sleep ( 2 )
678
+ await sleep ( 20 )
672
679
results . push ( 'onSuccess-async-end' )
673
680
// Implicit void return from async
674
681
} ,
@@ -680,7 +687,7 @@ describe('mutations', () => {
680
687
'vars' ,
681
688
)
682
689
683
- await vi . runAllTimersAsync ( )
690
+ await vi . advanceTimersByTimeAsync ( 30 )
684
691
685
692
expect ( results ) . toEqual ( [
686
693
'onMutate-async' ,
@@ -702,14 +709,14 @@ describe('mutations', () => {
702
709
onSuccess : ( ) => {
703
710
results . push ( 'onSuccess-start' )
704
711
return Promise . all ( [
705
- sleep ( 2 ) . then ( ( ) => results . push ( 'invalidate-queries' ) ) ,
706
- sleep ( 1 ) . then ( ( ) => results . push ( 'track-analytics' ) ) ,
712
+ sleep ( 20 ) . then ( ( ) => results . push ( 'invalidate-queries' ) ) ,
713
+ sleep ( 10 ) . then ( ( ) => results . push ( 'track-analytics' ) ) ,
707
714
] )
708
715
} ,
709
716
onSettled : ( ) => {
710
717
results . push ( 'onSettled-start' )
711
718
return Promise . allSettled ( [
712
- sleep ( 1 ) . then ( ( ) => results . push ( 'cleanup-1' ) ) ,
719
+ sleep ( 10 ) . then ( ( ) => results . push ( 'cleanup-1' ) ) ,
713
720
Promise . reject ( 'error' ) . catch ( ( ) =>
714
721
results . push ( 'cleanup-2-failed' ) ,
715
722
) ,
@@ -719,7 +726,7 @@ describe('mutations', () => {
719
726
'vars' ,
720
727
)
721
728
722
- await vi . runAllTimersAsync ( )
729
+ await vi . advanceTimersByTimeAsync ( 30 )
723
730
724
731
expect ( results ) . toEqual ( [
725
732
'onSuccess-start' ,
@@ -746,7 +753,7 @@ describe('mutations', () => {
746
753
} ,
747
754
onSuccess : async ( ) => {
748
755
results . push ( 'async-onSuccess' )
749
- await sleep ( 1 )
756
+ await sleep ( 10 )
750
757
return 'success-return-ignored'
751
758
} ,
752
759
onError : ( ) => {
@@ -764,7 +771,7 @@ describe('mutations', () => {
764
771
'vars' ,
765
772
)
766
773
767
- await vi . runAllTimersAsync ( )
774
+ await vi . advanceTimersByTimeAsync ( 10 )
768
775
769
776
const mutationResult = await mutationPromise
770
777
@@ -798,11 +805,11 @@ describe('mutations', () => {
798
805
} ,
799
806
onError : async ( ) => {
800
807
results . push ( 'onError-async' )
801
- await sleep ( 1 )
808
+ await sleep ( 10 )
802
809
// Test Promise.all() in error callback
803
810
return Promise . all ( [
804
- sleep ( 1 ) . then ( ( ) => results . push ( 'error-cleanup-1' ) ) ,
805
- sleep ( 2 ) . then ( ( ) => results . push ( 'error-cleanup-2' ) ) ,
811
+ sleep ( 10 ) . then ( ( ) => results . push ( 'error-cleanup-1' ) ) ,
812
+ sleep ( 20 ) . then ( ( ) => results . push ( 'error-cleanup-2' ) ) ,
806
813
] )
807
814
} ,
808
815
onSettled : ( _data , _error , _variables , context ) => {
@@ -818,7 +825,7 @@ describe('mutations', () => {
818
825
mutationError = error
819
826
} )
820
827
821
- await vi . runAllTimersAsync ( )
828
+ await vi . advanceTimersByTimeAsync ( 30 )
822
829
823
830
expect ( results ) . toEqual ( [
824
831
'onMutate' ,
0 commit comments