Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

[react-testing] fixed react queue emptying for React 17 #2438

Merged
merged 1 commit into from
Oct 19, 2022

Conversation

melnikov-s
Copy link
Contributor

@melnikov-s melnikov-s commented Oct 14, 2022

Description

#2352 Introduced automatic act queue emptying when a destroying a root wrapper. It fixed this fully in React 18 but not for older versions of react. This resulted in flaky tests for quilt as well as potentially surprising behaviour for React 17 users upgrading to react-testing@5.

This PR also adds queue emptying for older version of React.

@melnikov-s melnikov-s force-pushed the react-testing-act-for-react-17 branch from 6a7c8cc to ce0c718 Compare October 14, 2022 16:20
@@ -179,9 +179,6 @@ describe('@shopify/react-testing', () => {
wrapper.act(() => new Promise(() => {}));
await wrapper.destroy();

// React 17 will fail without this await
await new Promise((resolve) => setTimeout(resolve, 0));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is no longer needed, my guess is to why the tests were flaky was due to mocked timers that were not being restored. I found one such place here:

// https://github.com/facebook/react/blob/12adaffef7105e2714f82651ea51936c563fe15c/packages/shared/enqueueTask.js#L13
let enqueueTaskImpl: any = null;

export function enqueueTask(task: (v: any) => void) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how the queue gets emptied out in older version of react. It creates a macro task either with setImmediate or postMessage . We're probably ok doing only the setImmediate here but decided to copy the entire code to be sure and match what react is doing.

@melnikov-s melnikov-s marked this pull request as ready for review October 14, 2022 16:45
@melnikov-s melnikov-s requested a review from a team as a code owner October 14, 2022 16:45
result,
this.actPromise,
]) as unknown as Promise<void>;

this.actPromises.push(promise);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We collect all the promises we need to wait on as Promise.race([Promise.resolve()] does not resolve at the same time as Promise.resolve() (it adds a tick). So best to wait on the same promise that gets returned to act

@melnikov-s melnikov-s force-pushed the react-testing-act-for-react-17 branch from ce0c718 to f898f1e Compare October 14, 2022 20:25
Copy link
Member

@BPScott BPScott left a comment

Choose a reason for hiding this comment

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

Heck yeah! Thank you so much for digging into this. I see no glaring issue with the code (not that I saw any problem with the prior code either).

Ryan ended up merging #2435 not spotting this PR was super close. Can you want to revert that commit so we can see the e2e test passes in react 17.

@melnikov-s
Copy link
Contributor Author

Ryan ended up merging #2435 not spotting this PR was super close. Can you want to revert that commit so we can see the e2e test passes in react 17.

Saw that, this PR is rebased on top of those changes and is running the e2e tests on React 17. I've been restarting the runs periodically and so far no e2e tests failures here 🤞 . I'll snapshot it and ran against web to see what the results are now. I'm still expecting some failures due to useLazyQuery but no other differences, which I'll look at next.

@melnikov-s melnikov-s force-pushed the react-testing-act-for-react-17 branch from 27067e1 to b9eadd5 Compare October 19, 2022 19:23
@melnikov-s melnikov-s requested a review from BPScott October 19, 2022 19:24
@Shopify Shopify deleted a comment from github-actions bot Oct 19, 2022
@Shopify Shopify deleted a comment from github-actions bot Oct 19, 2022
Copy link
Member

@BPScott BPScott left a comment

Choose a reason for hiding this comment

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

Magic!

Merge when you're happy

@melnikov-s
Copy link
Contributor Author

Did another snapshot run on web just in case and it continues to pass (https://buildkite.com/shopify/web-ci-builder/builds/555602).

React 17 e2e tests have been stable. Merging.

@melnikov-s melnikov-s merged commit bc50c0d into main Oct 19, 2022
@melnikov-s melnikov-s deleted the react-testing-act-for-react-17 branch October 19, 2022 21:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants