@@ -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
0 commit comments