Skip to content
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

Reimplement act without mock Scheduler #21714

Merged
merged 1 commit into from Jun 22, 2021

Conversation

@acdlite
Copy link
Member

@acdlite acdlite commented Jun 22, 2021

Based on #21703

Currently, in a React 18 root, act only works if you mock the Scheduler package. This was because we didn't want to add additional checks at runtime.

But now that the act testing API is dev-only, we can simplify its implementation.

Now when an update is wrapped with act, React will bypass Scheduler entirely and push its tasks onto a special internal queue. Then, when the outermost act scope exists, we'll flush that queue.

I also removed the "wrong act" warning, because the plan is to move act to an isomorphic entry point, similar to startTransition. That's not directly related to this PR, but I didn't want to bother re-implementing that warning only to immediately remove it.

I'll add the isomorphic API in a follow up.

Note that the internal version of act that we use in our own tests still depends on mocking the Scheduler package, because it needs to work in production. I'm planning to move that implementation to a shared (internal) module, too.

});
expect(container.innerHTML).toBe('0');
// Flush the pending timers
await act(async () => {

This comment has been minimized.

@acdlite

acdlite Jun 22, 2021
Author Member

Rewrote to not use a mock scheduler API

}

await act(() => {
render(<App />, container);

This comment has been minimized.

@acdlite

acdlite Jun 22, 2021
Author Member

This is actually a new feature. There was no inherent reason this wasn't already supported. This is nice because usually an act scope doesn't need to be async, but if the effects trigger microtasks you might still want to await that.

@sizebot
Copy link

@sizebot sizebot commented Jun 22, 2021

Comparing: 422e0bb...7641eec

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 127.40 kB 127.40 kB = 40.85 kB 40.84 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 130.21 kB 130.21 kB = 41.75 kB 41.76 kB
facebook-www/ReactDOM-prod.classic.js +0.01% 405.75 kB 405.81 kB +0.01% 75.02 kB 75.03 kB
facebook-www/ReactDOM-prod.modern.js +0.02% 394.18 kB 394.24 kB = 73.26 kB 73.26 kB
facebook-www/ReactDOMForked-prod.classic.js +0.01% 405.75 kB 405.81 kB +0.01% 75.03 kB 75.04 kB
oss-stable-semver/react/cjs/react.development.js +9.31% 75.47 kB 82.50 kB +9.23% 20.24 kB 22.11 kB
oss-stable/react/cjs/react.development.js +9.31% 75.47 kB 82.50 kB +9.23% 20.24 kB 22.11 kB
oss-experimental/react/cjs/react.development.js +9.25% 75.94 kB 82.97 kB +9.25% 20.29 kB 22.17 kB
facebook-react-native/react/cjs/React-dev.js +8.10% 90.86 kB 98.23 kB +9.11% 21.79 kB 23.78 kB
oss-stable-semver/react/umd/react.development.js +7.54% 98.26 kB 105.66 kB +7.21% 25.25 kB 27.07 kB
oss-stable/react/umd/react.development.js +7.54% 98.26 kB 105.66 kB +7.21% 25.25 kB 27.07 kB
oss-experimental/react/umd/react.development.js +7.50% 98.75 kB 106.15 kB +7.21% 25.28 kB 27.10 kB
facebook-www/React-dev.modern.js +7.40% 99.45 kB 106.81 kB +8.13% 24.21 kB 26.17 kB
facebook-www/React-dev.classic.js +7.33% 100.47 kB 107.83 kB +7.88% 24.44 kB 26.36 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react/cjs/react.development.js +9.31% 75.47 kB 82.50 kB +9.23% 20.24 kB 22.11 kB
oss-stable/react/cjs/react.development.js +9.31% 75.47 kB 82.50 kB +9.23% 20.24 kB 22.11 kB
oss-experimental/react/cjs/react.development.js +9.25% 75.94 kB 82.97 kB +9.25% 20.29 kB 22.17 kB
facebook-react-native/react/cjs/React-dev.js +8.10% 90.86 kB 98.23 kB +9.11% 21.79 kB 23.78 kB
oss-stable-semver/react/umd/react.development.js +7.54% 98.26 kB 105.66 kB +7.21% 25.25 kB 27.07 kB
oss-stable/react/umd/react.development.js +7.54% 98.26 kB 105.66 kB +7.21% 25.25 kB 27.07 kB
oss-experimental/react/umd/react.development.js +7.50% 98.75 kB 106.15 kB +7.21% 25.28 kB 27.10 kB
facebook-www/React-dev.modern.js +7.40% 99.45 kB 106.81 kB +8.13% 24.21 kB 26.17 kB
facebook-www/React-dev.classic.js +7.33% 100.47 kB 107.83 kB +7.88% 24.44 kB 26.36 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer.development.js +1.06% 36.31 kB 36.70 kB +0.94% 8.21 kB 8.29 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer.development.js +1.06% 36.31 kB 36.70 kB +0.94% 8.21 kB 8.29 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer.development.js +1.06% 36.31 kB 36.70 kB +0.94% 8.21 kB 8.29 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +1.06% 36.33 kB 36.71 kB +0.94% 8.22 kB 8.30 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +1.06% 36.33 kB 36.71 kB +0.94% 8.22 kB 8.30 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +1.06% 36.33 kB 36.71 kB +0.94% 8.22 kB 8.30 kB
facebook-www/ReactTestUtils-dev.modern.js +0.97% 56.80 kB 57.35 kB +0.83% 15.96 kB 16.09 kB
facebook-www/ReactTestUtils-dev.classic.js +0.97% 56.81 kB 57.35 kB +0.83% 15.96 kB 16.09 kB
oss-experimental/react-dom/cjs/react-dom-test-utils.development.js +0.89% 62.08 kB 62.63 kB +0.82% 18.04 kB 18.19 kB
oss-stable-semver/react-dom/cjs/react-dom-test-utils.development.js +0.89% 62.08 kB 62.63 kB +0.82% 18.04 kB 18.19 kB
oss-stable/react-dom/cjs/react-dom-test-utils.development.js +0.89% 62.08 kB 62.63 kB +0.82% 18.04 kB 18.19 kB
oss-experimental/react-dom/umd/react-dom-test-utils.development.js +0.87% 67.13 kB 67.72 kB +0.73% 18.54 kB 18.68 kB
oss-stable-semver/react-dom/umd/react-dom-test-utils.development.js +0.87% 67.13 kB 67.72 kB +0.73% 18.54 kB 18.68 kB
oss-stable/react-dom/umd/react-dom-test-utils.development.js +0.87% 67.13 kB 67.72 kB +0.73% 18.54 kB 18.68 kB
oss-stable-semver/react/cjs/react.production.min.js +0.76% 7.09 kB 7.14 kB +0.53% 2.82 kB 2.83 kB
oss-stable/react/cjs/react.production.min.js +0.76% 7.09 kB 7.14 kB +0.53% 2.82 kB 2.83 kB
oss-experimental/react/cjs/react-unstable-shared-subset.development.js +0.71% 69.87 kB 70.37 kB +0.97% 18.92 kB 19.11 kB
oss-experimental/react/cjs/react.production.min.js +0.71% 7.61 kB 7.66 kB +0.34% 2.95 kB 2.96 kB
facebook-react-native/react/cjs/React-prod.js +0.68% 16.69 kB 16.80 kB +1.00% 4.28 kB 4.33 kB
facebook-react-native/react/cjs/React-profiling.js +0.68% 16.69 kB 16.80 kB +1.00% 4.28 kB 4.33 kB
facebook-www/React-prod.modern.js +0.49% 16.85 kB 16.93 kB +0.25% 4.36 kB 4.37 kB
facebook-www/React-profiling.modern.js +0.49% 16.85 kB 16.93 kB +0.25% 4.36 kB 4.37 kB
facebook-www/React-prod.classic.js +0.49% 16.99 kB 17.08 kB +0.34% 4.40 kB 4.42 kB
facebook-www/React-profiling.classic.js +0.49% 16.99 kB 17.08 kB +0.34% 4.40 kB 4.42 kB
oss-stable-semver/react/umd/react.profiling.min.js +0.46% 10.97 kB 11.02 kB +0.29% 4.41 kB 4.42 kB
oss-stable/react/umd/react.profiling.min.js +0.46% 10.97 kB 11.02 kB +0.29% 4.41 kB 4.42 kB
oss-stable-semver/react/umd/react.production.min.js +0.46% 10.97 kB 11.02 kB +0.29% 4.41 kB 4.42 kB
oss-stable/react/umd/react.production.min.js +0.46% 10.97 kB 11.02 kB +0.29% 4.41 kB 4.42 kB
oss-experimental/react-dom/umd/react-dom-test-utils.production.min.js +0.43% 13.16 kB 13.21 kB +0.25% 5.14 kB 5.16 kB
oss-stable-semver/react-dom/umd/react-dom-test-utils.production.min.js +0.43% 13.16 kB 13.21 kB +0.25% 5.14 kB 5.16 kB
oss-stable/react-dom/umd/react-dom-test-utils.production.min.js +0.43% 13.16 kB 13.21 kB +0.25% 5.14 kB 5.16 kB
oss-experimental/react/umd/react.profiling.min.js +0.43% 11.43 kB 11.48 kB +0.33% 4.55 kB 4.56 kB
oss-experimental/react/umd/react.production.min.js +0.43% 11.43 kB 11.48 kB +0.33% 4.55 kB 4.56 kB
oss-experimental/react-dom/cjs/react-dom-test-utils.production.min.js +0.43% 13.09 kB 13.14 kB +0.26% 5.09 kB 5.11 kB
oss-stable-semver/react-dom/cjs/react-dom-test-utils.production.min.js +0.43% 13.09 kB 13.14 kB +0.26% 5.09 kB 5.11 kB
oss-stable/react-dom/cjs/react-dom-test-utils.production.min.js +0.43% 13.09 kB 13.14 kB +0.26% 5.09 kB 5.11 kB
react-native/implementations/ReactNativeRenderer-dev.fb.js = 731.35 kB 729.75 kB = 158.13 kB 157.69 kB
oss-experimental/react-dom/umd/react-dom.development.js = 1,037.07 kB 1,030.12 kB = 225.22 kB 223.21 kB
oss-experimental/react-dom/cjs/react-dom.development.js = 987.24 kB 980.61 kB = 222.46 kB 220.44 kB
oss-stable-semver/react-dom/umd/react-dom.development.js = 1,011.17 kB 1,004.22 kB = 220.16 kB 218.15 kB
oss-stable/react-dom/umd/react-dom.development.js = 1,011.17 kB 1,004.22 kB = 220.16 kB 218.15 kB
oss-stable-semver/react-dom/cjs/react-dom.development.js = 962.81 kB 956.18 kB = 217.36 kB 215.34 kB
oss-stable/react-dom/cjs/react-dom.development.js = 962.81 kB 956.18 kB = 217.36 kB 215.34 kB
facebook-www/ReactDOM-dev.classic.js = 1,068.00 kB 1,060.20 kB = 237.13 kB 235.02 kB
facebook-www/ReactDOMForked-dev.classic.js = 1,068.00 kB 1,060.20 kB = 237.12 kB 235.02 kB
facebook-www/ReactDOM-dev.modern.js = 1,042.97 kB 1,035.17 kB = 232.11 kB 230.02 kB
facebook-www/ReactDOMForked-dev.modern.js = 1,042.97 kB 1,035.17 kB = 232.11 kB 230.02 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js = 617.48 kB 612.62 kB = 134.83 kB 133.37 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.development.js = 647.80 kB 642.69 kB = 136.32 kB 134.87 kB
facebook-www/ReactDOMTesting-dev.classic.js = 979.02 kB 971.20 kB = 220.11 kB 217.95 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js = 601.55 kB 596.69 kB = 131.39 kB 129.94 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js = 601.55 kB 596.69 kB = 131.39 kB 129.94 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.development.js = 631.03 kB 625.91 kB = 132.88 kB 131.43 kB
oss-stable/react-test-renderer/umd/react-test-renderer.development.js = 631.03 kB 625.91 kB = 132.88 kB 131.43 kB
facebook-www/ReactDOMTesting-dev.modern.js = 951.36 kB 943.54 kB = 214.39 kB 212.25 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-dev.js = 612.66 kB 607.25 kB = 132.43 kB 130.88 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js = 718.70 kB 712.03 kB = 153.05 kB 151.11 kB
facebook-www/ReactTestRenderer-dev.modern.js = 631.59 kB 625.64 kB = 135.87 kB 134.33 kB
facebook-www/ReactTestRenderer-dev.classic.js = 631.58 kB 625.63 kB = 135.86 kB 134.32 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.development.js = 696.05 kB 689.37 kB = 148.35 kB 146.43 kB
oss-stable/react-reconciler/cjs/react-reconciler.development.js = 696.05 kB 689.37 kB = 148.35 kB 146.43 kB

Generated by 🚫 dangerJS against 7641eec

@acdlite acdlite force-pushed the acdlite:act-no-scheduler-mock branch 3 times, most recently from c38eabe to 3400ee6 Jun 22, 2021
@acdlite
Copy link
Member Author

@acdlite acdlite commented Jun 22, 2021

Think I might need to add the isomorphic module in this PR to fix the dev tools tests. I forgot they involve multiple renderers.

@acdlite acdlite force-pushed the acdlite:act-no-scheduler-mock branch 9 times, most recently from e83ad98 to 6c91e94 Jun 22, 2021
@acdlite acdlite marked this pull request as ready for review Jun 22, 2021
@acdlite acdlite changed the title act should work without mock Scheduler Reimplement act without mock Scheduler Jun 22, 2021
Copy link
Member

@rickhanlonii rickhanlonii left a comment

LGTM, pair reviewed offline online

Currently, in a React 18 root, `act` only works if you mock the
Scheduler package. This was because we didn't want to add additional
checks at runtime.

But now that the `act` testing API is dev-only, we can simplify its
implementation.

Now when an update is wrapped with `act`, React will bypass Scheduler
entirely and push its tasks onto a special internal queue. Then, when
the outermost `act` scope exists, we'll flush that queue.

I also removed the "wrong act" warning, because the plan is to move
`act` to an isomorphic entry point, simlar to `startTransition`. That's
not directly related to this PR, but I didn't want to bother
re-implementing that warning only to immediately remove it.

I'll add the isomorphic API in a follow up.

Note that the internal version of `act` that we use in our own tests
still depends on mocking the Scheduler package, because it needs to work
in production. I'm planning to move that implementation to a shared
(internal) module, too.
@acdlite acdlite force-pushed the acdlite:act-no-scheduler-mock branch from 6c91e94 to 7641eec Jun 22, 2021
@acdlite acdlite merged commit 06f7b4f into facebook:master Jun 22, 2021
2 of 22 checks passed
2 of 22 checks passed
ci/circleci: RELEASE_CHANNEL_stable_yarn_build Your tests are queued behind your running builds
Details
ci/circleci: get_base_build Your tests are queued behind your running builds
Details
ci/circleci: sync_reconciler_forks Your tests are queued behind your running builds
Details
ci/circleci: yarn_build CircleCI is running your tests
Details
ci/circleci: yarn_flow Your tests are queued behind your running builds
Details
ci/circleci: yarn_lint Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=experimental --env=development CircleCI is running your tests
Details
ci/circleci: yarn_test--r=experimental --env=production Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=stable --env=development Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=stable --env=development --persistent Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=stable --env=production CircleCI is running your tests
Details
ci/circleci: yarn_test--r=www-classic --env=development --variant=false Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=www-classic --env=development --variant=true CircleCI is running your tests
Details
ci/circleci: yarn_test--r=www-classic --env=production --variant=false Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=www-classic --env=production --variant=true Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=www-modern --env=development --variant=false Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=www-modern --env=development --variant=true CircleCI is running your tests
Details
ci/circleci: yarn_test--r=www-modern --env=production --variant=false Your tests are queued behind your running builds
Details
ci/circleci: yarn_test--r=www-modern --env=production --variant=true Your tests are queued behind your running builds
Details
@codesandbox
ci/codesandbox Building packages...
Details
@facebook-github-tools
Facebook CLA Check Contributor License Agreement is valid!
Details
ci/circleci: setup Your tests passed on CircleCI!
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants