-
Notifications
You must be signed in to change notification settings - Fork 861
build: adjust enzyme, RTL and jest to run in React 18 runtime #6940
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
build: adjust enzyme, RTL and jest to run in React 18 runtime #6940
Conversation
…t when REACT_VERSION is 18
…on for testing React 16 and 17
| require('jest-config/build/getCacheDirectory').default; | ||
|
|
||
| // Set REACT_VERSION env variable to latest if empty or invalid | ||
| if (!['16', '17', '18'].includes(process.env.REACT_VERSION)) { |
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 could replace this with the utility function but Jest 24 doesn't support TypeScript config files
|
|
||
| const reactVersion = process.env.REACT_VERSION; | ||
|
|
||
| console.log(`Running tests on React v${reactVersion}`); |
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.
I don't like this but hopefully we can move it to the test script when available :)
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.
I'm kind of okay with it here, but yeah, having it in the individual test scripts would be helpful for thrown errors.
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.
I think I may need to take a step back here to ask what our goal is with running our test suites across multiple versions of Kibana. 🤔 I thought we were doing it for Cypress as a very basic smoke test that rendering worked, especially for portals etc. which were most affected by the React 18 upgrade.
Running all our jest suites on multiple versions is going to fairly significantly impact our CI times. What are our plans to mitigate that, and what precisely are our goals for those tests?
edit: Just want to clarify the above is not a change request or a blocker, I just want to make sure we've thought this through!
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 publish our library as compatible with React 16, 17 and (soon) 18. We should make sure it works and will continue to work with all changes we make no matter what EUI-compatible React version you're using.
The time to run all unit and e2e tests on multiple versions of react scales linearly, but that's okay. @1Copenut and I already discussed ways to parallelize it, so the total time to run tests should equal the longest single (unit or e2e) test run. Of course that may differ when the number of resources is limited due to, for example, building multiple PR environments at the same time. In this case we will prioritize builds on main.
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.
👍 Gotcha, thank you for explaining! Parallelization makes total sense. One other option could also be to run non-React18 tests (i.e., older versions that we "support" but aren't first-class citizens) on a daily/night cron job instead of per-PR. I'm not sure if that makes more sense than parallelization though - do you have a preference?
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.
The nice thing here, we can do pretty much whichever we want. Thinking out loud a second, breaking these into parallel runs shouldn't be too difficult in Builldkite. Instead of running a consolidated test command, we can set up the job with steps and multiple commands that each pass a version flag. We can increase resources if we exhaust the agent, or if the time isn't to our liking, def. run the older version tests on a cron.
| " | ||
| padding: 7px; | ||
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.
I think it comes from different whitespace serializing in the latest @testing-library/react?
1Copenut
left a 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.
👍 LGTM! I appreciate the clear explanation in your PR description for what's happening and why you've opted not to document the change yet. The upcoming work to unify how React versions are passed will be a great opportunity to make consistent and document.
|
|
||
| const reactVersion = process.env.REACT_VERSION; | ||
|
|
||
| console.log(`Running tests on React v${reactVersion}`); |
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.
I'm kind of okay with it here, but yeah, having it in the individual test scripts would be helpful for thrown errors.
| Object.defineProperty(global, 'TextEncoder', { | ||
| value: util.TextEncoder, | ||
| }); |
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.
@tkajtoch Can you add a comment explaining why this polyfill had to be added?
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.
Sure, I'll do that in the code!
| } from './react_warnings'; | ||
| export { sleep } from './sleep'; | ||
| export * from './emotion-prefix'; | ||
| export * from './react_version'; |
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.
I have some slight concerns here. Why do we want to make these utilities publicly available to consumers?
And if we don't, can we please move this to src/test/internal/ instead?
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.
This definitely shouldn't be publicly available and won't even work when used outside test environment. I honestly thought none of src/test things are included in the build, but I just noticed that's not the case. I'll move it to src/test/internal in a new PR!
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.
Perfect, thanks Tomasz!
* build: convert jest config to .js * build: add react version switching * build: add @cfaester/enzyme-adapter-react-18 enzyme adapter and use it when REACT_VERSION is 18 * build: update @testing-library/react and provide an alternative version for testing React 16 and 17 * build: polyfill TextEncoder for running React 18 tests * build: set IS_REACT_ACT_ENVIRONMENT = true when running React 18 enzyme adapter * test: reset dnd server context when running on react 16 and 17 * test: fix fireEvent import to use act-wrapped react equivalent * test: update form inline snapshot due to whitespace mismatch
* build: convert jest config to .js * build: add react version switching * build: add @cfaester/enzyme-adapter-react-18 enzyme adapter and use it when REACT_VERSION is 18 * build: update @testing-library/react and provide an alternative version for testing React 16 and 17 * build: polyfill TextEncoder for running React 18 tests * build: set IS_REACT_ACT_ENVIRONMENT = true when running React 18 enzyme adapter * test: reset dnd server context when running on react 16 and 17 * test: fix fireEvent import to use act-wrapped react equivalent * test: update form inline snapshot due to whitespace mismatch
* build: convert jest config to .js * build: add react version switching * build: add @cfaester/enzyme-adapter-react-18 enzyme adapter and use it when REACT_VERSION is 18 * build: update @testing-library/react and provide an alternative version for testing React 16 and 17 * build: polyfill TextEncoder for running React 18 tests * build: set IS_REACT_ACT_ENVIRONMENT = true when running React 18 enzyme adapter * test: reset dnd server context when running on react 16 and 17 * test: fix fireEvent import to use act-wrapped react equivalent * test: update form inline snapshot due to whitespace mismatch
* build: convert jest config to .js * build: add react version switching * build: add @cfaester/enzyme-adapter-react-18 enzyme adapter and use it when REACT_VERSION is 18 * build: update @testing-library/react and provide an alternative version for testing React 16 and 17 * build: polyfill TextEncoder for running React 18 tests * build: set IS_REACT_ACT_ENVIRONMENT = true when running React 18 enzyme adapter * test: reset dnd server context when running on react 16 and 17 * test: fix fireEvent import to use act-wrapped react equivalent * test: update form inline snapshot due to whitespace mismatch
Summary
This PR adds
@cfaester/enzyme-adapter-react-18and conditionally switches between React 17 and React 18 adapters depending on theREACT_VERSIONenvironment variable. It also converts jest config file to JS and configures jest mappers to switchreact,react-domand@testing-library/reactmodule versions depending onREACT_VERSION.Jest tests are using the new environment variable to switch between tested React versions. For now it's required to manually set the
REACT_VERSIONenvironment variable if you want to use a non-default version. It's different from the recently added cypress script argument (--react-version) but will be unified in another PR. That's why I decided to not document settingREACT_VERSIONforyarn test-unitright now.QA
Please note that CI will fail for this PR due to some tests and typings not being compatible with React 18 yet.
yarnREACT_VERSION=17 yarn test-unitand see all tests passingyarn test-unitand see many tests failing