@@ -3,19 +3,17 @@ import React, { useState } from 'react'
3
3
import useSWR , { cache } from '../src'
4
4
import { sleep } from './utils'
5
5
6
+ // This has to be an async function to wait a microtask to flush updates
6
7
const advanceTimers = async ( ms : number ) => jest . advanceTimersByTime ( ms )
7
8
8
- // This test heavily depends on timers, and it takes long time and makes tests flaky.
9
- // So we use fake timers Jest provides
9
+ // This test heavily depends on setInterval/setTimeout timers, which makes tests slower and flaky.
10
+ // So we use Jest's fake timers
10
11
describe ( 'useSWR - refresh' , ( ) => {
11
- beforeAll ( ( ) => {
12
+ beforeEach ( ( ) => {
12
13
jest . useFakeTimers ( )
13
14
} )
14
- afterAll ( ( ) => {
15
- jest . useRealTimers ( )
16
- } )
17
15
afterEach ( ( ) => {
18
- jest . clearAllTimers ( )
16
+ jest . useRealTimers ( )
19
17
} )
20
18
it ( 'should rerender automatically on interval' , async ( ) => {
21
19
let count = 0
@@ -65,12 +63,12 @@ describe('useSWR - refresh', () => {
65
63
66
64
await act ( ( ) => advanceTimers ( 100 ) ) // no update (deduped)
67
65
screen . getByText ( 'count: 0' )
68
- await act ( ( ) => advanceTimers ( 400 ) ) // exceed dudupingInterval
66
+ await act ( ( ) => advanceTimers ( 400 ) ) // reach dudupingInterval
69
67
await act ( ( ) => advanceTimers ( 100 ) ) // update
70
68
screen . getByText ( 'count: 1' )
71
69
await act ( ( ) => advanceTimers ( 100 ) ) // no update (deduped)
72
70
screen . getByText ( 'count: 1' )
73
- await act ( ( ) => advanceTimers ( 400 ) ) // exceed dudupingInterval
71
+ await act ( ( ) => advanceTimers ( 400 ) ) // reach dudupingInterval
74
72
await act ( ( ) => advanceTimers ( 100 ) ) // update
75
73
screen . getByText ( 'count: 2' )
76
74
} )
0 commit comments