-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests to prove jest useFakeTimers is supported (#641)
Co-authored-by: Lei Chen <[email protected]> Co-authored-by: Michael Peyper <[email protected]>
- Loading branch information
1 parent
a54eeb3
commit 7273ba4
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |