Skip to content

Commit

Permalink
fix the test timeout is false
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei Chen authored and Lei Chen committed Sep 9, 2021
1 parent 6e6b5a2 commit ecc4f28
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/__tests__/asyncHook.fakeTimers.test.ts
Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions src/core/asyncUtils.ts
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions 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> = []
Expand All @@ -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()
Expand All @@ -28,17 +30,15 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
return new Promise<void>((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
Expand All @@ -60,4 +60,4 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
return timeoutController
}

export { createTimeoutController }
export { createTimeoutController, DEFAULT_TIMEOUT }

0 comments on commit ecc4f28

Please sign in to comment.