Skip to content

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

Merged
merged 12 commits into from
May 21, 2020

Conversation

mdjastrzebski
Copy link
Member

Summary

Wrap waitFor in (async) act to support async queries without explicit act call. Otherwise there was a problem when async state changes caused by e.g. async useEffect uses (fetching data, ...) generated dreded act warnings in the console during test runs.

Closes #343

@mdjastrzebski mdjastrzebski linked an issue May 21, 2020 that may be closed by this pull request
@thymikee thymikee changed the title Wrap waitFor in (async) act to support async queries without explicit act call feat: wrap waitFor in (async) act to support async queries without explicit act call May 21, 2020
src/waitFor.js Outdated
Comment on lines 46 to 54
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Suggested change
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));

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Member

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

Copy link
Member Author

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);
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed await

mdjastrzebski and others added 3 commits May 21, 2020 15:36
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>
@thymikee thymikee merged commit 4adb92b into next May 21, 2020
@thymikee thymikee deleted the feature/wrap-wait-for-in-act branch May 21, 2020 14:08
thymikee added a commit that referenced this pull request May 28, 2020
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrap waitFor with async act
2 participants