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

[React@18] Upgrade @testing-library/react #198918

Merged
merged 15 commits into from
Nov 11, 2024

Conversation

Dosant
Copy link
Contributor

@Dosant Dosant commented Nov 5, 2024

Summary

Part of https://github.com/elastic/kibana-team/issues/1016#issuecomment-2454845292

As part of upgrade to React@18 we need to upgrade testing-library. The offical upgrade path is that for React@17 testing-library < 13 should be used. And for React@18, testing-library > 13

React Testing Library versions 13+ require React v18. If your project uses an older version of React, be sure to install version 12:

This path turns out to be very problematic for us because it requires to upgrade React together with the testing-library, but we want the React runtime upgrade PR to be very small to be able to revert easily. We were ready to consider approaches were we'd bring in React@18 for unit tests, but keep runtime running React@17 https://github.com/elastic/kibana-team/issues/1016#issuecomment-2454845292

Fortunatly, we found a way to run the latest testing-library version with React@17 using legacyRoot option on render

By default we'll render with support for concurrent features (i.e. ReactDOMClient.createRoot). However, if you're dealing with a legacy app that requires rendering like in React 17 (i.e. ReactDOM.render) then you should enable this option by setting legacyRoot: true.

In this PR we're running the latest testing-library but defaulting to legacy rendering. This is done by mocking @testing-library/react as this is not global configuration options and we don't want to update every test file. Moreover, when we upgrade our packages to React@18 (phase 1 of the upgrade) this legacyRoot:true in unit tests would logically fit nicely with our plans, as Kibana will be running in legacy mode and the tests will also be rendering in legacy mode. As we will work on phase 2 and transitioning on the new concurrent mode, we will come up with parallel transition plan for the unit tests.

A pleasant surprise is that amount of places that have broken with this change is very small - a handfull of types and a couple of unit tests.

After this PR is merged, we can continue with properly addressing the largest breaking that is caused by testing-library, which for us are changes in renderHook API #195087 . With the upgraded testing-library we can make PRs to swap old renderHook with new renderHook independently from React version upgrade.


I've created an issue to track cleaning this up post upgrade #199100

@@ -58,6 +58,10 @@ module.exports = (request, options) => {
});
}

if (request === 'react-dom/client') {
return Path.resolve(__dirname, 'mocks/react_dom_client_mock.ts');
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're just mocking this with an {}, this API from React@18 won't be used because we're forcing legacyRoot:true. Just need to it help to resolve the modules. This mock can be removed with an upgrade to React@18


return {
...actual,
render: (ui, options) => actual.render(ui, { ...options, legacyRoot: true }),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forcing legacyRoot:true by default makes testing library to use legacy API ReactDOM.render api which is how we make it work with React@17.
The option is documented here https://testing-library.com/docs/react-testing-library/api/#legacyroot .

There is no global configuration for this option, so we have to do this mocking workaround to adjust the default.

Event after upgrade of the react packages to React@18 it would make sense to keep this option as it until we start upgrading to the Concurrent Mode. We will likely come of a gradual transition plan in parallel with the runtime.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah same thing here as above, I feel at minimum we need a comment to track this.

@@ -403,10 +403,10 @@ describe('CheckAll', () => {

// simulate the wall clock advancing
for (let i = 0; i < totalIndexNames + 1; i++) {
await waitFor(() => {});
Copy link
Contributor Author

@Dosant Dosant Nov 5, 2024

Choose a reason for hiding this comment

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

This test seem to rely on some very specific internal timings that apparently have changed. I found that adding another waitFor fixes the tests. Feel free to take a look and contribute to make it more robust

Copy link
Contributor

@Ikuni17 Ikuni17 left a comment

Choose a reason for hiding this comment

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

This approach seems good to me, nice find. My only request would be to track the removal of the mock so that bit of work isn't forgotten about. Likely in the overall React@18 issue.

@Dosant Dosant changed the title bump testing library [React@18] Upgrade @testing-library/react Nov 6, 2024
@Dosant Dosant added release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) labels Nov 6, 2024
@Dosant Dosant marked this pull request as ready for review November 6, 2024 10:10
@Dosant Dosant requested review from a team as code owners November 6, 2024 10:10
@elasticmachine
Copy link
Contributor

Pinging @elastic/appex-sharedux (Team:SharedUX)

@Dosant Dosant self-assigned this Nov 6, 2024
@Dosant Dosant requested a review from Ikuni17 November 6, 2024 10:10
Copy link
Member

@wayneseymour wayneseymour left a comment

Choose a reason for hiding this comment

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

CR only, looks fine, but 2 asks and I'll approve.


return {
...actual,
render: (ui, options) => actual.render(ui, { ...options, legacyRoot: true }),
Copy link
Member

Choose a reason for hiding this comment

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

Yeah same thing here as above, I feel at minimum we need a comment to track this.

@botelastic botelastic bot added the ci:project-deploy-observability Create an Observability project label Nov 6, 2024
@adcoelho adcoelho requested a review from a team as a code owner November 7, 2024 08:58
Copy link
Contributor

@adcoelho adcoelho left a comment

Choose a reason for hiding this comment

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

response-ops changes OK

Copy link
Contributor

@maxcold maxcold left a comment

Choose a reason for hiding this comment

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

kubernetes_security and session_view changes look good to me

Copy link
Contributor

@gergoabraham gergoabraham left a comment

Choose a reason for hiding this comment

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

@elastic/security-defend-workflows changes (that one line) look good! thank you for the upgrade! 🙌

@Dosant
Copy link
Contributor Author

Dosant commented Nov 7, 2024

@kapral18, thank you for pointing that out. I think I figured this one out and should be good now. Please take another look

@Dosant
Copy link
Contributor Author

Dosant commented Nov 8, 2024

@elasticmachine merge upstream

Copy link
Contributor

@kapral18 kapral18 left a comment

Choose a reason for hiding this comment

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

LGTM for threat-hunting-explore

@Dosant
Copy link
Contributor Author

Dosant commented Nov 11, 2024

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

⏳ Build in-progress

  • Buildkite Build
  • Commit: 2229964
  • Kibana Serverless Image: docker.elastic.co/kibana-ci/kibana-serverless:pr-198918-22299648f2a0

History

cc @Dosant

@Dosant Dosant enabled auto-merge (squash) November 11, 2024 08:12
@Dosant Dosant merged commit c8227a2 into elastic:main Nov 11, 2024
50 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11774884870

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Nov 11, 2024
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Nov 11, 2024
…#199592)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[React@18] Upgrade &#x60;@testing-library/react&#x60;
(#198918)](#198918)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Anton
Dosov","email":"anton.dosov@elastic.co"},"sourceCommit":{"committedDate":"2024-11-11T08:31:15Z","message":"[React@18]
Upgrade `@testing-library/react`
(#198918)","sha":"c8227a26941a76044a89d27662dae029644b3ea7","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:SharedUX","backport:prev-minor","ci:project-deploy-observability","Team:obs-ux-infra_services","React@18"],"title":"[React@18]
Upgrade
`@testing-library/react`","number":198918,"url":"https://github.com/elastic/kibana/pull/198918","mergeCommit":{"message":"[React@18]
Upgrade `@testing-library/react`
(#198918)","sha":"c8227a26941a76044a89d27662dae029644b3ea7"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198918","number":198918,"mergeCommit":{"message":"[React@18]
Upgrade `@testing-library/react`
(#198918)","sha":"c8227a26941a76044a89d27662dae029644b3ea7"}}]}]
BACKPORT-->

Co-authored-by: Anton Dosov <anton.dosov@elastic.co>
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Nov 11, 2024
tkajtoch pushed a commit to tkajtoch/kibana that referenced this pull request Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) ci:project-deploy-observability Create an Observability project React@18 release_note:skip Skip the PR/issue when compiling release notes Team:obs-ux-infra_services Observability Infrastructure & Services User Experience Team Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.17.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.