-
Notifications
You must be signed in to change notification settings - Fork 274
feat: wrap waitFor
in (async) act to support async queries without explicit act
call
#344
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
Conversation
waitFor
in (async) act to support async queries without explicit act
callwaitFor
in (async) act to support async queries without explicit act
call
src/waitFor.js
Outdated
let result: T; | ||
|
||
//$FlowFixMe: this is just too complicated for flow | ||
await act(async () => { | ||
result = await waitForInternal(expectation, options); | ||
}); | ||
|
||
//$FlowFixMe: either we have result or `waitFor` there error | ||
return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
let result: T; | |
//$FlowFixMe: this is just too complicated for flow | |
await act(async () => { | |
result = await waitForInternal(expectation, options); | |
}); | |
//$FlowFixMe: either we have result or `waitFor` there error | |
return result; | |
return act(() => waitForInternal(expectation, options)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately does not work, as act is not forwarding the return value from the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to capture it to intermediate variable so that it can be returned by the waitFor
async wrapper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see it now. The error suppressed by "//$FlowFixMe: this is just too complicated for flow" comes from lacking lib defs of react-test-renderer, not Flow being dumb. Worth updating the comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated comment
src/waitFor.js
Outdated
options?: WaitForOptions | ||
): Promise<T> { | ||
if (!checkReactVersionAtLeast(16, 9)) { | ||
return await waitForInternal(expectation, options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return await
is redundant, you can omit the await
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed await
Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
…explicit `act` call (#344) * Wrapped waitFor call in act to support async waitFor/findBy * Updated docs * Spelling mistake * Added short info in migration guide * Corrected spelling * Clarified comments * Added async act implementation conditional on React version * Code review changes * Update src/waitFor.js Co-authored-by: Michał Pierzchała <thymikee@gmail.com> * Update src/waitFor.js Co-authored-by: Michał Pierzchała <thymikee@gmail.com> * Fixed prettier * Update src/waitFor.js Co-authored-by: Michał Pierzchała <thymikee@gmail.com> Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
Summary
Wrap
waitFor
in (async) act to support async queries without explicitact
call. Otherwise there was a problem when async state changes caused by e.g. asyncuseEffect
uses (fetching data, ...) generated drededact
warnings in the console during test runs.Closes #343