Skip to content

Commit

Permalink
#1179: Specify default ontimeout at configuration level
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Reiser committed Oct 11, 2023
1 parent a7b7252 commit 0e528c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/__tests__/fake-timers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {waitFor, waitForElementToBeRemoved} from '..'
import {configure, waitFor, waitForElementToBeRemoved} from '..'
import {render} from './helpers/test-utils'

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

test('fake timer timeout uses default ontimeout', async () => {
configure({
defaultOnTimeout: _ => {
return Error('Test Error')
},
})
jest.useFakeTimers()
await expect(
waitFor(
() => {
throw new Error('always throws')
},
{timeout: 10},
),
).rejects.toMatchInlineSnapshot(`[Error: Test Error]`)
})

test('times out after 1000ms by default', async () => {
const startReal = performance.now()
jest.useFakeTimers()
Expand Down
3 changes: 3 additions & 0 deletions src/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function waitFor(
stackTraceError,
interval = 50,
onTimeout = error => {
if (getConfig().defaultOnTimeout) {
return getConfig().defaultOnTimeout(error)
}
Object.defineProperty(error, 'message', {
value: getConfig().getElementError(error.message, container).message,
})
Expand Down
1 change: 1 addition & 0 deletions types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Config {
defaultHidden: boolean
/** default value for the `ignore` option in `ByText` queries */
defaultIgnore: string
defaultOnTimeout?: (error: Error) => Error
showOriginalStackTrace: boolean
throwSuggestions: boolean
getElementError: (message: string | null, container: Element) => Error
Expand Down

0 comments on commit 0e528c4

Please sign in to comment.