Skip to content

Commit a1adc19

Browse files
author
Andreas Reiser
committed
#1179: Specify default ontimeout at configuration level
1 parent a7b7252 commit a1adc19

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/__tests__/fake-timers.js

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

44
async function runWaitFor({time = 300} = {}, options) {
@@ -11,8 +11,14 @@ async function runWaitFor({time = 300} = {}, options) {
1111
await waitFor(() => expect(result).toBe(response), options)
1212
}
1313

14+
let originalConfig
15+
beforeAll(() => {
16+
originalConfig = getConfig()
17+
})
18+
1419
afterEach(() => {
1520
jest.useRealTimers()
21+
configure(originalConfig)
1622
})
1723

1824
test('real timers', async () => {
@@ -43,6 +49,23 @@ test('fake timer timeout', async () => {
4349
).rejects.toMatchInlineSnapshot(`[Error: always throws]`)
4450
})
4551

52+
test('fake timer timeout uses default ontimeout', async () => {
53+
configure({
54+
defaultOnTimeout: _ => {
55+
return Error('Test Error')
56+
},
57+
})
58+
jest.useFakeTimers()
59+
await expect(
60+
waitFor(
61+
() => {
62+
throw new Error('always throws')
63+
},
64+
{timeout: 10},
65+
),
66+
).rejects.toMatchInlineSnapshot(`[Error: Test Error]`)
67+
})
68+
4669
test('times out after 1000ms by default', async () => {
4770
const startReal = performance.now()
4871
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)