Skip to content

Commit 0e528c4

Browse files
author
Andreas Reiser
committed
testing-library#1179: Specify default ontimeout at configuration level
1 parent a7b7252 commit 0e528c4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/__tests__/fake-timers.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {waitFor, waitForElementToBeRemoved} from '..'
1+
import {configure, waitFor, waitForElementToBeRemoved} from '..'
22
import {render} from './helpers/test-utils'
33

44
async function runWaitFor({time = 300} = {}, options) {
@@ -43,6 +43,23 @@ test('fake timer timeout', async () => {
4343
).rejects.toMatchInlineSnapshot(`[Error: always throws]`)
4444
})
4545

46+
test('fake timer timeout uses default ontimeout', async () => {
47+
configure({
48+
defaultOnTimeout: _ => {
49+
return Error('Test Error')
50+
},
51+
})
52+
jest.useFakeTimers()
53+
await expect(
54+
waitFor(
55+
() => {
56+
throw new Error('always throws')
57+
},
58+
{timeout: 10},
59+
),
60+
).rejects.toMatchInlineSnapshot(`[Error: Test Error]`)
61+
})
62+
4663
test('times out after 1000ms by default', async () => {
4764
const startReal = performance.now()
4865
jest.useFakeTimers()

src/wait-for.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ function waitFor(
2424
stackTraceError,
2525
interval = 50,
2626
onTimeout = error => {
27+
if (getConfig().defaultOnTimeout) {
28+
return getConfig().defaultOnTimeout(error)
29+
}
2730
Object.defineProperty(error, 'message', {
2831
value: getConfig().getElementError(error.message, container).message,
2932
})

types/config.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface Config {
1414
defaultHidden: boolean
1515
/** default value for the `ignore` option in `ByText` queries */
1616
defaultIgnore: string
17+
defaultOnTimeout?: (error: Error) => Error
1718
showOriginalStackTrace: boolean
1819
throwSuggestions: boolean
1920
getElementError: (message: string | null, container: Element) => Error

0 commit comments

Comments
 (0)