Skip to content

Support global timeout #72

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

Open
wants to merge 2 commits 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
21 changes: 18 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const delegateFnBodyToExecuteInPageInitial = `

let delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial

let config: Partial<IConfigureOptions> = {
asyncUtilTimeout: 4500
}

configure(config)

type DOMReturnType = ElementHandle | ElementHandle[] | null

type ContextFn = (...args: any[]) => ElementHandle
Expand Down Expand Up @@ -156,7 +162,7 @@ export async function getDocument(_page?: Page): Promise<ElementHandle> {

export function wait(
callback: () => void,
{timeout = 4500, interval = 50}: {timeout?: number; interval?: number} = {},
{timeout = config.asyncUtilTimeout, interval = 50}: {timeout?: number; interval?: number} = {},
): Promise<{}> {
return waitForExpect(callback, timeout, interval)
}
Expand All @@ -168,14 +174,23 @@ export function configure(options: Partial<IConfigureOptions>): void {
return
}

const {testIdAttribute} = options
config = {
...config,
...options,
}

const {testIdAttribute, asyncUtilTimeout} = config

if (testIdAttribute) {
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace(
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPage.replace(
/testIdAttribute: (['|"])data-testid(['|"])/g,
`testIdAttribute: $1${testIdAttribute}$2`,
)
}

if (asyncUtilTimeout) {
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPage.replace(/asyncUtilTimeout: ([-+]?\d*\.?\d+)(?:[eE]([-+]?\d+))?/g, `asyncUtilTimeout: ${asyncUtilTimeout}`)
}
}

export function getQueriesForElement<T>(
Expand Down
3 changes: 2 additions & 1 deletion lib/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@ export interface IQueryUtils extends IQueryMethods {
}

export interface IConfigureOptions {
testIdAttribute: string
testIdAttribute: string,
asyncUtilTimeout: number
}
8 changes: 4 additions & 4 deletions test/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ describe('lib/extend.ts', () => {
})

it('should handle the findBy* methods', async () => {
expect(await document.findByText('Loaded!', {}, {timeout: 7000})).toBeTruthy()
}, 9000)
expect(await document.findByText('Loaded!')).toBeTruthy()
})

it('should handle the findByAll* methods', async () => {
const elements = await document.findAllByText(/Hello/, {}, {timeout: 7000})
const elements = await document.findAllByText(/Hello/)
expect(elements).toHaveLength(2)

const text = await Promise.all([
Expand All @@ -145,7 +145,7 @@ describe('lib/extend.ts', () => {
])

expect(text).toEqual(['Hello h1', 'Hello h2'])
}, 9000)
})
})

afterAll(async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/late-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const heading2 = document.createElement('h2')
heading2.textContent = 'Hello h2'
document.body.appendChild(heading2)
}, 5000)
}, 3000)
</script>
</body>
</html>
16 changes: 15 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ describe('lib/index.ts', () => {
expect(await queries.getNodeText(element)).toEqual('Hello h1')
})

it('should support custom global timeout', async () => {
configure({asyncUtilTimeout: 2000})
let timeout1 = false
let timeout2 = false
setTimeout(() => {
timeout1 = true
}, 1000)
setTimeout(() => {
timeout2 = true
}, 3000)
await waitFor(() => expect(timeout1).toBe(true))
expect(timeout2).toBe(false)
})

it('should support custom data-testid attribute name', async () => {
configure({testIdAttribute: 'data-id'})
const document = await getDocument(page)
Expand Down Expand Up @@ -66,7 +80,7 @@ describe('lib/index.ts', () => {
}, 9000)

afterEach(() => {
configure({testIdAttribute: 'data-testid'}) //cleanup
configure({testIdAttribute: 'data-testid', asyncUtilTimeout: 4500}) //cleanup
})

afterAll(async () => {
Expand Down