-
Notifications
You must be signed in to change notification settings - Fork 503
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
Init Migration from Enzyme to React Testing Library #1643
Conversation
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1643 +/- ##
==========================================
- Coverage 96.01% 95.89% -0.13%
==========================================
Files 242 243 +1
Lines 7559 7599 +40
Branches 1984 2004 +20
==========================================
+ Hits 7258 7287 +29
- Misses 301 311 +10
- Partials 0 1 +1
☔ View full report in Codecov by Sentry. |
/> | ||
) | ||
).toMatchSnapshot(); | ||
const { container } = render( |
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.
couldn't you just change shallow->render and keep the rest the same, to minimize the changes?
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.
As I have discovered till now, the render function is not a direct replacement for shallow. As per docs,
Enzyme's shallow renderer doesn't render sub-components, so React Testing Library's render method is more similar to Enzyme's mount method.
This is also the reason why snapshot file is modified with a lot of code additions.
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.
It can be inlined, but we need to use container out of render function to match the snapshot, as it doesn't provide that utility itself.
<div | ||
class="ScoreCard--CircularProgressbarWrapper" | ||
> | ||
<svg |
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 previous snapshot was a lot more compact, this one contains a lot of junk which is difficult to grok for a human, making it much less valuable as a tool to detect regressions. Perhaps this was the difference in using the shallow
function - is there an equivalent in the new libs?
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.
Actually, the reason for that is, previously, the snapshots were containing the react-dom, but in the case of RTL, they contain HTML dom
According to official migration guide https://testing-library.com/docs/react-testing-library/migrate-from-enzyme/
The shallow function needs to be replaced with render, which seems like the only solution.
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.
There is a solution on the internet that suggests changing the jest snapshot serializer to output smaller snapshots.
That could break all of the tests, and thus tests would need a huge code change.
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
@yurishkuro Which path should we go for in migration?
|
Using render from RTL is unacceptabl. I see that it creates huge snapshots which are very difficult to use for detecting regressions, and based on other information I read are not even stable. Ideal solution would be to avoid using snapshot testing altogether, given many reasonable arguments against snapshots. But that means we have to migrate 175 call sites, a pretty large lift. So using |
NB: please do not create a single PR that updates all 175 call sites. We should do it module by module, so that the changes to the snapshots can be actually reviewed. |
Thanks for the inputs 🚀 |
Signed-off-by: Ansh Goyal <[email protected]>
@yurishkuro I have made the changes, and as I am seeing, the new helper function just reverted the snapshot changes. That's amazing 🚀 |
Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: Ansh Goyal <[email protected]>
packages/jaeger-ui/src/components/QualityMetrics/ScoreCard.test.js
Outdated
Show resolved
Hide resolved
Signed-off-by: Ansh Goyal <[email protected]>
@yurishkuro I think, this PR can be merged safely. The codecov tests are failing because the new helper function doesn't have tests written for it. Should I add it to codecov ignore list? |
@@ -17,6 +17,8 @@ | |||
"@babel/preset-typescript": "^7.21.0", | |||
"@svgr/babel-plugin-transform-svg-component": "^8.0.0", | |||
"@svgr/babel-preset": "^8.0.0", | |||
"@testing-library/jest-dom": "^5.17.0", |
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.
why is this explicitly added to deps? It's not used in the code, looks like it's a transitive dep that will be included in the lock file
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 would need to be installed when migrating from mount
function (in Enzyme) to RTL, according to the official docs.
@@ -36,6 +38,8 @@ | |||
"jest-environment-jsdom": "^29.5.0", | |||
"jest-junit": "^16.0.0", | |||
"less": "3.13.1", | |||
"react-is": "^18.2.0", | |||
"react-shallow-renderer": "^16.15.0", |
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.
is this used?
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.
These are the dependencies for React Test Renderer, which is already installed inside jaeger-ui
@@ -0,0 +1,94 @@ | |||
// Copyright (c) 2020 Uber Technologies, Inc. |
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.
// Copyright (c) 2020 Uber Technologies, Inc. | |
// Copyright (c) 2023 The Jaeger Authors |
import ShallowRenderer from 'react-test-renderer/shallow'; | ||
import { isFragment, isLazy, isPortal, isMemo, isSuspense, isForwardRef } from 'react-is'; | ||
|
||
const shallow = (element, options) => { |
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.
so I am not clear - why are we adding this code, isn't it what react-shallow-renderer
supposed to solve?
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.
react-test-renderer already comes with react-shallow-renderer and react-is, and we have this installed already.
https://www.npmjs.com/package/react-test-renderer?activeTab=dependencies
Closing due to merge conflicts |
## Which problem is this PR solving? - This PR migrates a test `ScoreCard.test.js` from Enzyme to React Testing Library - Enzyme is no longer supported in React 18 - Replacement for #1643 ## Description of the changes - It uses the custom shallow function to render the components - Code adopted from https://thesametech.com/snapshot-testing-in-rtl/, with author's permission: #1653 (comment) ## How was this change tested? - It was tested by running the command `yarn test -- -u` to re-generate snapshots of the changed tests - After that, `yarn test-ci ScoreCard.test.js --coverage` was used to check if all tests pass and coverage is 100% ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality --------- Signed-off-by: Ansh Goyal <[email protected]> Signed-off-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
Which problem is this PR solving?
ScoreCard.test.js
from Enzyme to React Testing LibraryDescription of the changes
How was this change tested?
yarn test -- -u
to re-generate snapshots of the changed testsyarn test-ci ScoreCard.test.js --coverage
was used to check if all tests pass and coverage is 100%Checklist