Skip to content

Commit ecc4f28

Browse files
Lei ChenLei Chen
Lei Chen
authored and
Lei Chen
committed
fix the test timeout is false
1 parent 6e6b5a2 commit ecc4f28

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

src/__tests__/asyncHook.fakeTimers.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('async hook (fake timers) tests', () => {
3030
jest.useRealTimers()
3131
})
3232

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

@@ -146,8 +146,7 @@ describe('async hook (fake timers) tests', () => {
146146
)
147147
})
148148

149-
// eslint-disable-next-line jest/no-disabled-tests
150-
test.skip('should not reject when waiting for next update if timeout has been disabled', async () => {
149+
test('should not reject when waiting for next update if timeout has been disabled', async () => {
151150
const { result, waitForNextUpdate } = renderHook(() => useSequence(['first', 'second'], 1100))
152151

153152
expect(result.current).toBe('first')

src/core/asyncUtils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
AsyncUtils
88
} from '../types'
99

10-
import { createTimeoutController } from '../helpers/createTimeoutController'
10+
import { createTimeoutController, DEFAULT_TIMEOUT } from '../helpers/createTimeoutController'
1111
import { TimeoutError } from '../helpers/error'
1212

13-
const DEFAULT_TIMEOUT = 1000
1413
const DEFAULT_INTERVAL = 50
1514

1615
function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils {

src/helpers/createTimeoutController.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { jestFakeTimersAreEnabled } from './jestFakeTimersAreEnabled'
22

3+
const DEFAULT_TIMEOUT = 1000
4+
35
function createTimeoutController(timeout: number | false, options: { allowFakeTimers: boolean }) {
46
let timeoutId: NodeJS.Timeout
57
const timeoutCallbacks: Array<() => void> = []
@@ -8,7 +10,7 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
810
const { allowFakeTimers = false } = options
911

1012
const advanceTime = async (currentMs: number) => {
11-
if (currentMs < timeout) {
13+
if (currentMs < (!timeout === true ? DEFAULT_TIMEOUT : timeout)) {
1214
jest.advanceTimersByTime(1)
1315

1416
await Promise.resolve()
@@ -28,17 +30,15 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
2830
return new Promise<void>((resolve, reject) => {
2931
timeoutController.timedOut = false
3032
timeoutController.onTimeout(resolve)
31-
3233
if (timeout) {
3334
timeoutId = setTimeout(() => {
3435
timeoutController.timedOut = true
3536
timeoutCallbacks.forEach((callback) => callback())
3637
resolve()
3738
}, timeout)
38-
39-
if (jestFakeTimersAreEnabled() && allowFakeTimers) {
40-
advanceTime(0)
41-
}
39+
}
40+
if (jestFakeTimersAreEnabled() && allowFakeTimers) {
41+
advanceTime(0)
4242
}
4343

4444
promise
@@ -60,4 +60,4 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
6060
return timeoutController
6161
}
6262

63-
export { createTimeoutController }
63+
export { createTimeoutController, DEFAULT_TIMEOUT }

0 commit comments

Comments
 (0)