@@ -6,9 +6,16 @@ import { useClient, useQuery } from '../../villus/src';
66import waitForExpect from 'wait-for-expect' ;
77import { PostQuery , PostsQuery , QueryErrorWith500 , QueryWithNetworkError } from 'villus/test/mocks/queries' ;
88
9+ beforeEach ( ( ) => {
10+ jest . useFakeTimers ( ) ;
11+ } ) ;
12+
13+ afterEach ( ( ) => {
14+ jest . useRealTimers ( ) ;
15+ } ) ;
16+
917describe ( 'batch plugin' , ( ) => {
1018 test ( 'batches queries with batcher' , async ( ) => {
11- jest . useFakeTimers ( ) ;
1219 mount ( {
1320 setup ( ) {
1421 useClient ( {
@@ -32,16 +39,17 @@ describe('batch plugin', () => {
3239
3340 jest . advanceTimersByTime ( 100 ) ;
3441 await flushPromises ( ) ;
42+
43+ // wait-for-expect uses timers under the hood, so we need to reset here
44+ jest . useRealTimers ( ) ;
3545 await waitForExpect ( ( ) => {
3646 expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
3747 expect ( document . querySelector ( '#multi' ) ?. children ) . toHaveLength ( 5 ) ;
3848 expect ( document . querySelector ( '#single' ) ?. textContent ) . toContain ( 'Awesome' ) ;
3949 } ) ;
40- jest . useRealTimers ( ) ;
4150 } ) ;
4251
4352 test ( 'results with non-200 code will be evaluated separately' , async ( ) => {
44- jest . useFakeTimers ( ) ;
4553 mount ( {
4654 setup ( ) {
4755 useClient ( {
@@ -65,16 +73,17 @@ describe('batch plugin', () => {
6573
6674 jest . advanceTimersByTime ( 100 ) ;
6775 await flushPromises ( ) ;
76+
77+ // wait-for-expect uses timers under the hood, so we need to reset here
78+ jest . useRealTimers ( ) ;
6879 await waitForExpect ( ( ) => {
6980 expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
7081 expect ( document . querySelector ( '#multi' ) ?. children ) . toHaveLength ( 5 ) ;
7182 expect ( document . querySelector ( '#error' ) ?. textContent ) . toContain ( 'Not authenticated' ) ;
7283 } ) ;
73- jest . useRealTimers ( ) ;
7484 } ) ;
7585
7686 test ( 'handles network errors' , async ( ) => {
77- jest . useFakeTimers ( ) ;
7887 mount ( {
7988 setup ( ) {
8089 useClient ( {
@@ -94,9 +103,10 @@ describe('batch plugin', () => {
94103
95104 jest . advanceTimersByTime ( 100 ) ;
96105 await flushPromises ( ) ;
106+ // wait-for-expect uses timers under the hood, so we need to reset here
107+ jest . useRealTimers ( ) ;
97108 await waitForExpect ( ( ) => {
98109 expect ( document . querySelector ( '#error' ) ?. textContent ) . toContain ( 'Failed to connect' ) ;
99110 } ) ;
100- jest . useRealTimers ( ) ;
101111 } ) ;
102112} ) ;
0 commit comments