Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1179: Specify default ontimeout at configuration level #1272

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 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, getConfig, waitFor, waitForElementToBeRemoved} from '..'
import {render} from './helpers/test-utils'

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

let originalConfig
beforeAll(() => {
originalConfig = getConfig()
})

afterEach(() => {
jest.useRealTimers()
configure(originalConfig)
})

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

test('fake timer timeout uses default ontimeout', async () => {
configure({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this updates global state, we should also revert it back to it's original state to prevent these changes from leaking through other test cases.
As an example see

configure(originalConfig)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint, I have reset the configuration after the tests as in the example you provided.

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