Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: testing-library/react-hooks-testing-library
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 43c377e0073fd64ae07e2790aabf426454c71552
Choose a base ref
..
head repository: testing-library/react-hooks-testing-library
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ac1b5597fba3398336f1087d0fbddd3ab982aade
Choose a head ref
Showing with 8 additions and 11 deletions.
  1. +4 −3 src/core/asyncUtils.ts
  2. +4 −8 src/helpers/createTimeoutController.ts
7 changes: 4 additions & 3 deletions src/core/asyncUtils.ts
Original file line number Diff line number Diff line change
@@ -7,9 +7,10 @@ import {
AsyncUtils
} from '../types'

import { createTimeoutController, DEFAULT_TIMEOUT } from '../helpers/createTimeoutController'
import { createTimeoutController } 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 {
@@ -19,11 +20,11 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
return callbackResult ?? callbackResult === undefined
}

const timeoutSignal = createTimeoutController(timeout, false)
const timeoutSignal = createTimeoutController(timeout as number | boolean, false)

const waitForResult = async () => {
while (true) {
const intervalSignal = createTimeoutController(interval, true)
const intervalSignal = createTimeoutController(interval as number | boolean, true)
timeoutSignal.onTimeout(() => intervalSignal.cancel())

await intervalSignal.wrap(new Promise<void>(addResolver))
12 changes: 4 additions & 8 deletions src/helpers/createTimeoutController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { jestFakeTimersAreEnabled } from './jestFakeTimersAreEnabled'
const DEFAULT_TIMEOUT = 1000

function createTimeoutController(
timeout: number | false = DEFAULT_TIMEOUT,
allowFakeTimers: boolean
) {
function createTimeoutController(timeout: number | boolean, allowFakeTimers: boolean) {
let timeoutId: NodeJS.Timeout
const timeoutCallbacks: Array<() => void> = []

@@ -22,10 +18,10 @@ function createTimeoutController(
timeoutController.timedOut = true
timeoutCallbacks.forEach((callback) => callback())
resolve()
}, timeout)
}, timeout as number)

if (jestFakeTimersAreEnabled() && allowFakeTimers) {
jest.advanceTimersByTime(timeout)
jest.advanceTimersByTime(timeout as number)
}
}

@@ -44,4 +40,4 @@ function createTimeoutController(
return timeoutController
}

export { createTimeoutController, DEFAULT_TIMEOUT }
export { createTimeoutController }