From ecc4f28f6a06e13e8475f6b26c2b88d4d7bec946 Mon Sep 17 00:00:00 2001 From: Lei Chen Date: Thu, 9 Sep 2021 14:20:21 -0700 Subject: [PATCH] fix the test timeout is false --- src/__tests__/asyncHook.fakeTimers.test.ts | 5 ++--- src/core/asyncUtils.ts | 3 +-- src/helpers/createTimeoutController.ts | 14 +++++++------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/__tests__/asyncHook.fakeTimers.test.ts b/src/__tests__/asyncHook.fakeTimers.test.ts index 90e3a257..9c1c4559 100644 --- a/src/__tests__/asyncHook.fakeTimers.test.ts +++ b/src/__tests__/asyncHook.fakeTimers.test.ts @@ -30,7 +30,7 @@ describe('async hook (fake timers) tests', () => { jest.useRealTimers() }) - runForRenderers(['default'], ({ renderHook }) => { + runForRenderers(['default', 'dom', 'native', 'server/hydrated'], ({ renderHook }) => { test('should wait for arbitrary expectation to pass when using advanceTimersByTime()', async () => { const { waitFor } = renderHook(() => null) @@ -146,8 +146,7 @@ describe('async hook (fake timers) tests', () => { ) }) - // eslint-disable-next-line jest/no-disabled-tests - test.skip('should not reject when waiting for next update if timeout has been disabled', async () => { + test('should not reject when waiting for next update if timeout has been disabled', async () => { const { result, waitForNextUpdate } = renderHook(() => useSequence(['first', 'second'], 1100)) expect(result.current).toBe('first') diff --git a/src/core/asyncUtils.ts b/src/core/asyncUtils.ts index ab890539..07c795fa 100644 --- a/src/core/asyncUtils.ts +++ b/src/core/asyncUtils.ts @@ -7,10 +7,9 @@ import { AsyncUtils } from '../types' -import { createTimeoutController } from '../helpers/createTimeoutController' +import { createTimeoutController, DEFAULT_TIMEOUT } from '../helpers/createTimeoutController' import { TimeoutError } from '../helpers/error' -const DEFAULT_TIMEOUT = 1000 const DEFAULT_INTERVAL = 50 function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils { diff --git a/src/helpers/createTimeoutController.ts b/src/helpers/createTimeoutController.ts index 95accab0..cb22d5fe 100644 --- a/src/helpers/createTimeoutController.ts +++ b/src/helpers/createTimeoutController.ts @@ -1,5 +1,7 @@ import { jestFakeTimersAreEnabled } from './jestFakeTimersAreEnabled' +const DEFAULT_TIMEOUT = 1000 + function createTimeoutController(timeout: number | false, options: { allowFakeTimers: boolean }) { let timeoutId: NodeJS.Timeout const timeoutCallbacks: Array<() => void> = [] @@ -8,7 +10,7 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi const { allowFakeTimers = false } = options const advanceTime = async (currentMs: number) => { - if (currentMs < timeout) { + if (currentMs < (!timeout === true ? DEFAULT_TIMEOUT : timeout)) { jest.advanceTimersByTime(1) await Promise.resolve() @@ -28,17 +30,15 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi return new Promise((resolve, reject) => { timeoutController.timedOut = false timeoutController.onTimeout(resolve) - if (timeout) { timeoutId = setTimeout(() => { timeoutController.timedOut = true timeoutCallbacks.forEach((callback) => callback()) resolve() }, timeout) - - if (jestFakeTimersAreEnabled() && allowFakeTimers) { - advanceTime(0) - } + } + if (jestFakeTimersAreEnabled() && allowFakeTimers) { + advanceTime(0) } promise @@ -60,4 +60,4 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi return timeoutController } -export { createTimeoutController } +export { createTimeoutController, DEFAULT_TIMEOUT }