Skip to content

Commit ac1b559

Browse files
Lei ChenLei Chen
Lei Chen
authored and
Lei Chen
committed
fix fakeTimer problem
add new fakeTimer test and revise the function add advanceTime revise the advanceTime use jest.advanceTimersByTime change timeout type fix converage and revise type
1 parent 4a03704 commit ac1b559

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

src/__tests__/asyncHook.fakeTimers.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,41 @@ describe('async hook (fake timers) tests', () => {
5151

5252
expect(complete).toBe(true)
5353
})
54+
55+
test('should waitFor arbitrary expectation to pass when fake timers are not advanced explicitly', async () => {
56+
const fn = jest.fn().mockReturnValueOnce(false).mockReturnValueOnce(true)
57+
58+
const { waitFor } = renderHook(() => null)
59+
60+
await waitFor(() => {
61+
expect(fn()).toBe(true)
62+
})
63+
})
64+
65+
test('should reject if timeout is passed close to when promise resolves', async () => {
66+
const { waitFor } = renderHook(() => null)
67+
68+
let actual = 0
69+
const expected = 1
70+
71+
setTimeout(() => {
72+
actual = expected
73+
}, 101)
74+
75+
let complete = false
76+
77+
await expect(
78+
waitFor(
79+
() => {
80+
expect(actual).toBe(expected)
81+
complete = true
82+
},
83+
{ timeout: 100, interval: 50 }
84+
)
85+
).rejects.toThrow(Error('Timed out in waitFor after 100ms.'))
86+
87+
expect(complete).toBe(false)
88+
})
5489
})
5590
})
5691

src/core/asyncUtils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
import { createTimeoutController } from '../helpers/createTimeoutController'
1111
import { TimeoutError } from '../helpers/error'
1212

13-
const DEFAULT_INTERVAL = 50
1413
const DEFAULT_TIMEOUT = 1000
14+
const DEFAULT_INTERVAL = 50
1515

1616
function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils {
1717
const wait = async (callback: () => boolean | void, { interval, timeout }: WaitOptions) => {
@@ -20,11 +20,11 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
2020
return callbackResult ?? callbackResult === undefined
2121
}
2222

23-
const timeoutSignal = createTimeoutController(timeout)
23+
const timeoutSignal = createTimeoutController(timeout as number | boolean, false)
2424

2525
const waitForResult = async () => {
2626
while (true) {
27-
const intervalSignal = createTimeoutController(interval)
27+
const intervalSignal = createTimeoutController(interval as number | boolean, true)
2828
timeoutSignal.onTimeout(() => intervalSignal.cancel())
2929

3030
await intervalSignal.wrap(new Promise<void>(addResolver))

src/helpers/createTimeoutController.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { WaitOptions } from '../types'
1+
import { jestFakeTimersAreEnabled } from './jestFakeTimersAreEnabled'
22

3-
function createTimeoutController(timeout: WaitOptions['timeout']) {
3+
function createTimeoutController(timeout: number | boolean, allowFakeTimers: boolean) {
44
let timeoutId: NodeJS.Timeout
55
const timeoutCallbacks: Array<() => void> = []
66

@@ -18,7 +18,11 @@ function createTimeoutController(timeout: WaitOptions['timeout']) {
1818
timeoutController.timedOut = true
1919
timeoutCallbacks.forEach((callback) => callback())
2020
resolve()
21-
}, timeout)
21+
}, timeout as number)
22+
23+
if (jestFakeTimersAreEnabled() && allowFakeTimers) {
24+
jest.advanceTimersByTime(timeout as number)
25+
}
2226
}
2327

2428
promise
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const jestFakeTimersAreEnabled = () => {
2+
/* istanbul ignore else */
3+
if (typeof jest !== 'undefined' && jest !== null) {
4+
return (
5+
// legacy timers
6+
jest.isMockFunction(setTimeout) ||
7+
// modern timers
8+
Object.prototype.hasOwnProperty.call(setTimeout, 'clock')
9+
)
10+
}
11+
// istanbul ignore next
12+
return false
13+
}

0 commit comments

Comments
 (0)