Skip to content

Commit 081814d

Browse files
committed
feat: support asyncUtilsTimeout option via configure() API
Closes #460
1 parent 1ffcd7b commit 081814d

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

lib/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
2+
13
import {readFileSync} from 'fs'
24
import * as path from 'path'
35

@@ -177,14 +179,21 @@ export function configure(options: Partial<ConfigurationOptions>): void {
177179
return
178180
}
179181

180-
const {testIdAttribute} = options
182+
const {testIdAttribute, asyncUtilTimeout} = options
181183

182184
if (testIdAttribute) {
183185
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace(
184186
/testIdAttribute: (['|"])data-testid(['|"])/g,
185187
`testIdAttribute: $1${testIdAttribute}$2`,
186188
)
187189
}
190+
191+
if (asyncUtilTimeout) {
192+
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace(
193+
/asyncUtilTimeout: \d+/g,
194+
`asyncUtilTimeout: ${asyncUtilTimeout}`,
195+
)
196+
}
188197
}
189198

190199
export function getQueriesForElement<T>(

lib/typedefs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,5 @@ export interface Queries extends QueryMethods {
191191

192192
export interface ConfigurationOptions {
193193
testIdAttribute: string
194+
asyncUtilTimeout: number
194195
}

test/standalone/index.test.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ describe('lib/index.ts', () => {
124124
expect(await queries.getNodeText(element)).toEqual('Hello h1')
125125
})
126126

127+
it('should support regex on raw queries object', async () => {
128+
const scope = await page.$('#scoped')
129+
if (!scope) throw new Error('Should have scope')
130+
const element = await queries.getByText(scope, /Hello/i)
131+
expect(await queries.getNodeText(element)).toEqual('Hello h3')
132+
})
133+
134+
it('should bind getQueriesForElement', async () => {
135+
// FIXME: I think it will take some work to get the types in a
136+
// place to prevent @typescript-eslint from flagging this
137+
// eslint-disable-next-line @typescript-eslint/unbound-method
138+
const {getByText} = getQueriesForElement(await getDocument(page))
139+
const element = await getByText('Hello h1')
140+
expect(await queries.getNodeText(element)).toEqual('Hello h1')
141+
})
142+
127143
describe('configuration', () => {
128144
afterEach(() => {
129145
configure({testIdAttribute: 'data-testid'}) // cleanup
@@ -148,34 +164,42 @@ describe('lib/index.ts', () => {
148164
'should keep the default data-testid when input passed is invalid (%s)',
149165
async options => {
150166
const document = await getDocument(page)
167+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
151168
configure(options as any)
152169
const element = await queries.getByTestId(document, 'testid-label')
153170
expect(await queries.getNodeText(element)).toEqual('Label A')
154171
},
155172
)
156-
})
157-
it('should support regex on raw queries object', async () => {
158-
const scope = await page.$('#scoped')
159-
if (!scope) throw new Error('Should have scope')
160-
const element = await queries.getByText(scope, /Hello/i)
161-
expect(await queries.getNodeText(element)).toEqual('Hello h3')
162-
})
163173

164-
it('should bind getQueriesForElement', async () => {
165-
// FIXME: I think it will take some work to get the types in a
166-
// place to prevent @typescript-eslint from flagging this
167-
// eslint-disable-next-line @typescript-eslint/unbound-method
168-
const {getByText} = getQueriesForElement(await getDocument(page))
169-
const element = await getByText('Hello h1')
170-
expect(await queries.getNodeText(element)).toEqual('Hello h1')
174+
describe('async utils timeout', () => {
175+
beforeEach(async () =>
176+
page.goto(`file://${path.join(__dirname, '../fixtures/late-page.html')}`),
177+
)
178+
179+
it('supports configuring timeout for findBy* queries', async () => {
180+
configure({asyncUtilTimeout: 9000})
181+
182+
const element = await queries.findByText(await getDocument(page), 'Loaded!')
183+
184+
expect(element).toBeTruthy()
185+
}, 9000)
186+
})
171187
})
172188

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

178-
it('should use `wait` properly', async () => {
194+
it('waits for deferred element using findBy* queries', async () => {
195+
const element = await queries.findByText(await getDocument(page), 'Loaded!', undefined, {
196+
timeout: 9000,
197+
})
198+
199+
expect(element).toBeTruthy()
200+
}, 9000)
201+
202+
it('waits for deferred element using `waitFor`', async () => {
179203
// FIXME: I think it will take some work to get the types in a
180204
// place to prevent @typescript-eslint from flagging this
181205
// eslint-disable-next-line @typescript-eslint/unbound-method

0 commit comments

Comments
 (0)