Skip to content

Commit b5077bb

Browse files
committed
chore: reset timers on each test
1 parent 0159df2 commit b5077bb

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

test/use-swr-refresh.test.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ import React, { useState } from 'react'
33
import useSWR, { cache } from '../src'
44
import { sleep } from './utils'
55

6+
// This has to be an async function to wait a microtask to flush updates
67
const advanceTimers = async (ms: number) => jest.advanceTimersByTime(ms)
78

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
1011
describe('useSWR - refresh', () => {
11-
beforeAll(() => {
12+
beforeEach(() => {
1213
jest.useFakeTimers()
1314
})
14-
afterAll(() => {
15-
jest.useRealTimers()
16-
})
1715
afterEach(() => {
18-
jest.clearAllTimers()
16+
jest.useRealTimers()
1917
})
2018
it('should rerender automatically on interval', async () => {
2119
let count = 0
@@ -65,12 +63,12 @@ describe('useSWR - refresh', () => {
6563

6664
await act(() => advanceTimers(100)) // no update (deduped)
6765
screen.getByText('count: 0')
68-
await act(() => advanceTimers(400)) // exceed dudupingInterval
66+
await act(() => advanceTimers(400)) // reach dudupingInterval
6967
await act(() => advanceTimers(100)) // update
7068
screen.getByText('count: 1')
7169
await act(() => advanceTimers(100)) // no update (deduped)
7270
screen.getByText('count: 1')
73-
await act(() => advanceTimers(400)) // exceed dudupingInterval
71+
await act(() => advanceTimers(400)) // reach dudupingInterval
7472
await act(() => advanceTimers(100)) // update
7573
screen.getByText('count: 2')
7674
})

0 commit comments

Comments
 (0)