Skip to content

Commit ee5115b

Browse files
committed
fix(fixture): throw correct error when find* query times out on visibility
1 parent 5b31e3b commit ee5115b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/fixture/locator/queries.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,37 @@ const createFindQuery =
4444
)}`,
4545
)
4646

47-
const {state = asyncUtilExpectedState, timeout = asyncUtilTimeout} = waitForElementOptions ?? {}
47+
const {state: expectedState = asyncUtilExpectedState, timeout = asyncUtilTimeout} =
48+
waitForElementOptions ?? {}
4849

4950
try {
50-
await locator.first().waitFor({state, timeout})
51+
await locator.first().waitFor({state: expectedState, timeout})
5152
} catch (error) {
5253
// In the case of a `waitFor` timeout from Playwright, we want to
5354
// surface the appropriate error from Testing Library, so run the
5455
// query one more time as `get*` knowing that it will fail with the
5556
// error that we want the user to see instead of the `TimeoutError`
5657
if (error instanceof errors.TimeoutError) {
57-
return pageOrLocator
58+
const timeoutLocator = pageOrLocator
5859
.locator(
5960
`${queryToSelector(findQueryToGetQuery(query))}=${JSON.stringify(
6061
synchronousOptions,
6162
replacer,
6263
)}`,
6364
)
6465
.first()
65-
.waitFor({state, timeout: 100})
66+
67+
// Handle case where element is attached, but hidden, and the expected
68+
// state is set to `visible`. In this case, dereferencing the
69+
// `Locator` instance won't throw a `get*` query error, so just
70+
// surface the original Playwright timeout error
71+
if (expectedState === 'visible' && !(await timeoutLocator.isVisible())) {
72+
throw error
73+
}
74+
75+
// In all other cases, dereferencing the `Locator` instance here should
76+
// cause the above `get*` query to throw an error in Testing Library
77+
return timeoutLocator.waitFor({state: expectedState, timeout})
6678
}
6779

6880
throw error

test/fixture/locators.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,32 @@ test.describe('lib/fixture.ts (locators)', () => {
203203
)
204204
})
205205

206+
test('throws Playwright error when locator times out for visible state (but is attached)', async ({
207+
queries,
208+
}) => {
209+
const query = async () =>
210+
queries.findByText(/Hidden/, undefined, {state: 'visible', timeout: 500})
211+
212+
await expect(query).rejects.toThrowError(
213+
expect.objectContaining({
214+
message: expect.stringContaining('500'),
215+
}),
216+
)
217+
})
218+
219+
test('throws Testing Library error when locator times out for attached state', async ({
220+
queries,
221+
}) => {
222+
const query = async () =>
223+
queries.findByText(/Loaded!/, undefined, {state: 'attached', timeout: 500})
224+
225+
await expect(query).rejects.toThrowError(
226+
expect.objectContaining({
227+
message: expect.stringContaining('TestingLibraryElementError'),
228+
}),
229+
)
230+
})
231+
206232
test('throws Testing Library error when multi-element locator times out', async ({queries}) => {
207233
const query = async () => queries.findAllByText(/Hello/, undefined, {timeout: 500})
208234

0 commit comments

Comments
 (0)