Skip to content

✨ Support timeout configuration option #461

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

Merged
merged 1 commit into from
Jul 25, 2022
Merged
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
11 changes: 10 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import {readFileSync} from 'fs'
import * as path from 'path'

Expand Down Expand Up @@ -177,14 +179,21 @@ export function configure(options: Partial<ConfigurationOptions>): void {
return
}

const {testIdAttribute} = options
const {testIdAttribute, asyncUtilTimeout} = options

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

if (asyncUtilTimeout) {
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace(
/asyncUtilTimeout: \d+/g,
`asyncUtilTimeout: ${asyncUtilTimeout}`,
)
}
}

export function getQueriesForElement<T>(
Expand Down
1 change: 1 addition & 0 deletions lib/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,5 @@ export interface Queries extends QueryMethods {

export interface ConfigurationOptions {
testIdAttribute: string
asyncUtilTimeout: number
}
54 changes: 39 additions & 15 deletions test/standalone/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ describe('lib/index.ts', () => {
expect(await queries.getNodeText(element)).toEqual('Hello h1')
})

it('should support regex on raw queries object', async () => {
const scope = await page.$('#scoped')
if (!scope) throw new Error('Should have scope')
const element = await queries.getByText(scope, /Hello/i)
expect(await queries.getNodeText(element)).toEqual('Hello h3')
})

it('should bind getQueriesForElement', async () => {
// FIXME: I think it will take some work to get the types in a
// place to prevent @typescript-eslint from flagging this
// eslint-disable-next-line @typescript-eslint/unbound-method
const {getByText} = getQueriesForElement(await getDocument(page))
const element = await getByText('Hello h1')
expect(await queries.getNodeText(element)).toEqual('Hello h1')
})

describe('configuration', () => {
afterEach(() => {
configure({testIdAttribute: 'data-testid'}) // cleanup
Expand All @@ -148,34 +164,42 @@ describe('lib/index.ts', () => {
'should keep the default data-testid when input passed is invalid (%s)',
async options => {
const document = await getDocument(page)
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
configure(options as any)
const element = await queries.getByTestId(document, 'testid-label')
expect(await queries.getNodeText(element)).toEqual('Label A')
},
)
})
it('should support regex on raw queries object', async () => {
const scope = await page.$('#scoped')
if (!scope) throw new Error('Should have scope')
const element = await queries.getByText(scope, /Hello/i)
expect(await queries.getNodeText(element)).toEqual('Hello h3')
})

it('should bind getQueriesForElement', async () => {
// FIXME: I think it will take some work to get the types in a
// place to prevent @typescript-eslint from flagging this
// eslint-disable-next-line @typescript-eslint/unbound-method
const {getByText} = getQueriesForElement(await getDocument(page))
const element = await getByText('Hello h1')
expect(await queries.getNodeText(element)).toEqual('Hello h1')
describe('async utils timeout', () => {
beforeEach(async () =>
page.goto(`file://${path.join(__dirname, '../fixtures/late-page.html')}`),
)

it('supports configuring timeout for findBy* queries', async () => {
configure({asyncUtilTimeout: 9000})

const element = await queries.findByText(await getDocument(page), 'Loaded!')

expect(element).toBeTruthy()
}, 9000)
})
})

describe('loading the deferred page', () => {
beforeEach(async () =>
page.goto(`file://${path.join(__dirname, '../fixtures/late-page.html')}`),
)

it('should use `wait` properly', async () => {
it('waits for deferred element using findBy* queries', async () => {
const element = await queries.findByText(await getDocument(page), 'Loaded!', undefined, {
timeout: 9000,
})

expect(element).toBeTruthy()
}, 9000)

it('waits for deferred element using `waitFor`', async () => {
// FIXME: I think it will take some work to get the types in a
// place to prevent @typescript-eslint from flagging this
// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down