Skip to content

Commit

Permalink
test: add tests to prove jest useFakeTimers is supported (#641)
Browse files Browse the repository at this point in the history
Co-authored-by: Lei Chen <[email protected]>
Co-authored-by: Michael Peyper <[email protected]>
  • Loading branch information
3 people authored Jul 30, 2021
1 parent a54eeb3 commit 7273ba4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/__tests__/asyncHook.fakeTimers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe('async hook (fake timers) tests', () => {
beforeEach(() => {
jest.useFakeTimers()
})

afterEach(() => {
jest.useRealTimers()
})

runForRenderers(['default', 'dom', 'native', 'server/hydrated'], ({ renderHook }) => {
test('should wait for arbitrary expectation to pass when using advanceTimersByTime()', async () => {
const { waitFor } = renderHook(() => null)

let actual = 0
const expected = 1

setTimeout(() => {
actual = expected
}, 200)

let complete = false

jest.advanceTimersByTime(200)

await waitFor(() => {
expect(actual).toBe(expected)
complete = true
})

expect(complete).toBe(true)
})

test('should wait for arbitrary expectation to pass when using runOnlyPendingTimers()', async () => {
const { waitFor } = renderHook(() => null)

let actual = 0
const expected = 1

setTimeout(() => {
actual = expected
}, 200)

let complete = false

jest.runOnlyPendingTimers()

await waitFor(() => {
expect(actual).toBe(expected)
complete = true
})

expect(complete).toBe(true)
})
})
})

// eslint-disable-next-line jest/no-export
export {}

0 comments on commit 7273ba4

Please sign in to comment.