Skip to content

Commit

Permalink
chore: reset timers on each test
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Apr 5, 2021
1 parent 0159df2 commit b5077bb
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions test/use-swr-refresh.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import React, { useState } from 'react'
import useSWR, { cache } from '../src'
import { sleep } from './utils'

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

// This test heavily depends on timers, and it takes long time and makes tests flaky.
// So we use fake timers Jest provides
// This test heavily depends on setInterval/setTimeout timers, which makes tests slower and flaky.
// So we use Jest's fake timers
describe('useSWR - refresh', () => {
beforeAll(() => {
beforeEach(() => {
jest.useFakeTimers()
})
afterAll(() => {
jest.useRealTimers()
})
afterEach(() => {
jest.clearAllTimers()
jest.useRealTimers()
})
it('should rerender automatically on interval', async () => {
let count = 0
Expand Down Expand Up @@ -65,12 +63,12 @@ describe('useSWR - refresh', () => {

await act(() => advanceTimers(100)) // no update (deduped)
screen.getByText('count: 0')
await act(() => advanceTimers(400)) // exceed dudupingInterval
await act(() => advanceTimers(400)) // reach dudupingInterval
await act(() => advanceTimers(100)) // update
screen.getByText('count: 1')
await act(() => advanceTimers(100)) // no update (deduped)
screen.getByText('count: 1')
await act(() => advanceTimers(400)) // exceed dudupingInterval
await act(() => advanceTimers(400)) // reach dudupingInterval
await act(() => advanceTimers(100)) // update
screen.getByText('count: 2')
})
Expand Down

0 comments on commit b5077bb

Please sign in to comment.