@@ -44,25 +44,37 @@ const createFindQuery =
44
44
) } `,
45
45
)
46
46
47
- const { state = asyncUtilExpectedState , timeout = asyncUtilTimeout } = waitForElementOptions ?? { }
47
+ const { state : expectedState = asyncUtilExpectedState , timeout = asyncUtilTimeout } =
48
+ waitForElementOptions ?? { }
48
49
49
50
try {
50
- await locator . first ( ) . waitFor ( { state, timeout} )
51
+ await locator . first ( ) . waitFor ( { state : expectedState , timeout} )
51
52
} catch ( error ) {
52
53
// In the case of a `waitFor` timeout from Playwright, we want to
53
54
// surface the appropriate error from Testing Library, so run the
54
55
// query one more time as `get*` knowing that it will fail with the
55
56
// error that we want the user to see instead of the `TimeoutError`
56
57
if ( error instanceof errors . TimeoutError ) {
57
- return pageOrLocator
58
+ const timeoutLocator = pageOrLocator
58
59
. locator (
59
60
`${ queryToSelector ( findQueryToGetQuery ( query ) ) } =${ JSON . stringify (
60
61
synchronousOptions ,
61
62
replacer ,
62
63
) } `,
63
64
)
64
65
. 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} )
66
78
}
67
79
68
80
throw error
0 commit comments