Replace deprecated testing library in unit tests#4572
Replace deprecated testing library in unit tests#4572mproffitt merged 3 commits intoweaveworks:mainfrom
Conversation
3d72172 to
b0747de
Compare
b0747de to
24d43ac
Compare
|
Seems like the package intentions is to support newer versions of Jest, ref. Em-Ant/jest-transformer-svg#5. Maybe we should file an issue and eventually help with fixing the Jest 29 support? |
|
Ideal if we can.
…On Sun, 19 Jan 2025 at 12:55, Erik Godding Boye ***@***.***> wrote:
Seems like the package intentions is to support newer versions of Jest,
ref. Em-Ant/jest-transformer-svg#5
<Em-Ant/jest-transformer-svg#5>. Maybe we
should file an issue and eventually help with fixing the Jest 29 support?
—
Reply to this email directly, view it on GitHub
<#4572 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA6C3ZYHDSDVD4XVGSZ2A32LOACTAVCNFSM6AAAAABVOPJMUWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMBQHAYDQMJVHE>
.
You are receiving this because your review was requested.Message ID:
***@***.***>
|
|
I've done some more digging on this and it's not a bug in the weave-gitops/ui/components/DarkModeSwitch.tsx Lines 28 to 34 in 22a1bd7 This seems to be the result of this issue in From what I understand, the correct behaviour to resolve this would be to encode the images as base64 instead of plain XML. This would allow both the browser, and the tests to interpret them properly instead of them only being interpretable inside the browser. |
This commit upgrades the testing library from `react-test-renderer` to
`@testing-library/react`.
This largely results in changes to `renderer.create().toJSON()` becoming
`render().asFragment()`. To ensure minimal snapshot changes were
recorded, I used `firstChild` from the fragment so the fragment wrapper
was not added to the snapshots.
During testing, a large number of failure messages such as:
```plaintext
An empty string ("") was passed to the src attribute
```
This message would cause tests to fail and is the result of `svg` images
being passed to the fileMock in `ui/lib/fileMock.js`
To resolve this, I replaced `svg` management with
`jest-svg-transformer` but this raises a concern that the library is 7
years old with no recent maintainence. Whilst there is a more modern
variant `jest-transformer-svg`, this transformer does not seem to work
with `jest >= v29`, ultimately causing snapshots to fail with
`:1:2088: property missing ':'`
This replacement largely affects two test suites:
- Page
- Logo
In both of these suites, images are now replaced with an empty `<img />`
tag rather than a more complete styled tag
24d43ac to
348156d
Compare
|
Introduced a util method to convert from |
Sounds good! Does this mean the PR is reviewable/mergeable? Maybe update the PR description with the latest news? 😉 |
|
It should be reviewable yes. I'd like to wait for @gusevda as he has way more experience with Typescript than I do and may know of a better way to solve the same issue. |
gusevda
left a comment
There was a problem hiding this comment.
Looks great to me!
Only one comment which should not block merging. I'm not sure about use of .firstChild(). If it's only about existing snapshots, I would not worry about them. We are not changing the implementation in this PR, only tests, so we can be sure that we are not introducing any new bugs, so we can safely update all the snapshots. Use of firstChild() can be confusing for developers in the future.
|
I think if you're happy for it to go, it's a |
What changed?
This commit upgrades the testing library from
react-test-rendererto@testing-library/react.This largely results in changes to
renderer.create().toJSON()becomingrender().asFragment(). To ensure minimal snapshot changes were recorded, I usedfirstChildfrom the fragment so the fragment wrapper was not added to the snapshots.Why was this change made?
This change was made to replace the deprecated
react-test-rendererwith a more modern equivelent which is compatible with future versions of React frameworkSide effects
During testing, a large number of failure messages such as:
This message would cause tests to fail and is the result of
svgimages being passed to the fileMock inui/lib/fileMock.jsTo resolve this, I have replaced
svgmanagement withjest-transformer-svgand introduced a helper method to convert importedsvgimages to a usable base64 encoded version for use in CSS styling