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
React 18 - @shopify/react-testing #2248
Conversation
3e5c3bb
to
aec99c1
Compare
7af2f19
to
9fd6c0e
Compare
642b054
to
1707e20
Compare
e66a9b4
to
a1c8a6a
Compare
02ab752
to
9981d9d
Compare
a1c8a6a
to
f0b56ec
Compare
25adbcb
to
c86bc23
Compare
187f52c
to
cdbb3aa
Compare
Loom typically looks for setupFiles under tests/setup/environment.* but we have ours under the additional sub-directory of src/ because of the way that react-testing lays out its own tests.
The previous ReactDOM.render method is deprecated and instead tests should be making use of createRoot to generate a "Root" element and then calling render/unmount on that. This causes some confusion here because we already have a concept of a "Root" in react-testing, and so we use the name "reactRoot" to differentiate here.
b38292f
to
fcd8aa8
Compare
Ensure that we're awaiting the promise within the scope of the act block and use the flushSync API to mimic the behaviour of React 17 to synchronously flush state to the render rather than trying to batch it. Without the use of flushSync, we were seeing issues in shopify/web tests where a previously tested component that left a promise in the act queue could cause subsequent mounts in other tests to fail.
fcd8aa8
to
f71f134
Compare
In React < 18 returning a value that isn't a promise to act will result in a console warning, we need to match the previous behaviour of react-testing for applications that are still using React 17 to avoid introducing a warning.
Both versions are already functionally installed because react-server is still using React 17. This installs them both under namespaced packages as react17 and react18. We'll use these values to allow testing under particular react versions using the REACT_VERSION env var.
React 17 doesn't have a react-dom/client directory, nor does it have a createRoot or Root API. We provide backwards compatibility for this in the compat module so that our root.tsx code can still use "createRoot" just in React < 18 it'll be our own version of createRoot which wraps ReactDOM.render and ReactDOM.unmountComponentAtNode
a192bed
to
9564b82
Compare
Makes it clearer what that 14/16/17/18 numbers correspond to
9564b82
to
7de68f3
Compare
15860c7
to
3e3f3ee
Compare
Tested here against |
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.
PR looks good to me! 💯 🚀 small nit comment about using block comment instead
Description
Base: #2241
Fixes https://github.com/Shopify/web/issues/63209
Fixes #2252
We're upgrading
@shopify/react-testing
to support React 18 while maintaining backwards compatibility with React 17. This takes a couple of steps:createRoot
API, and provide a shim for it for React 17flushSync
API within our customact
wrapper in order to help ease the transition to React 18 forshopify/web
.On
flushSync
Our Web project has a lot of code that relies on some very specific ordering of promise resolution and state updates and thousands of tests break as a result of the new batching of state updates in React 18. Wrapping these act()s in
flushSync
helps make that a much easier update.In the future, what we'd like to do is to transition web over to better testing and component structuring practices which removes the need for
flushSync
. To do that, we'll probably make an option to opt-out offlushSync
on every act and run that as a test suite for Web that we know will be failing and then incrementally fix the tests that are failing because of it.Type of change
Checklist