Skip to content

Replace deprecated testing library in unit tests #4572

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

Merged
merged 3 commits into from
Jan 19, 2025

Conversation

mproffitt
Copy link
Contributor

@mproffitt mproffitt commented Jan 19, 2025

What changed?

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.

Why was this change made?

This change was made to replace the deprecated react-test-renderer with a more modern equivelent which is compatible with future versions of React framework

Side effects

During testing, a large number of failure messages such as:

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 have replaced svg management with jest-transformer-svg and introduced a helper method to convert imported svg images to a usable base64 encoded version for use in CSS styling

@mproffitt mproffitt force-pushed the react-testing-library branch 2 times, most recently from 3d72172 to b0747de Compare January 19, 2025 09:03
@mproffitt mproffitt requested a review from a team January 19, 2025 09:07
@mproffitt mproffitt force-pushed the react-testing-library branch from b0747de to 24d43ac Compare January 19, 2025 09:13
@erikgb
Copy link
Contributor

erikgb commented Jan 19, 2025

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?

@casibbald
Copy link
Collaborator

casibbald commented Jan 19, 2025 via email

@mproffitt
Copy link
Contributor Author

I've done some more digging on this and it's not a bug in the jest-transformer-svg library, rather it's related to these lines from DarkModeSwitch.tsx

.MuiSwitch-thumb {
color: #fff;
background-image: url(${(props) =>
props.theme.mode === ThemeTypes.Dark
? images.darkModeIcon
: images.lightModeIcon});
}

This seems to be the result of this issue in jest-styled-components styled-components/jest-styled-components#97 which causes an SVG to be embedded somehow in a way that cannot be parsed correctly.

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
@mproffitt mproffitt force-pushed the react-testing-library branch from 24d43ac to 348156d Compare January 19, 2025 17:36
@mproffitt
Copy link
Contributor Author

Introduced a util method to convert from image/svg+xml to image/svg+xml;base64 - this allows tests to pass without interfering with the front end

@erikgb
Copy link
Contributor

erikgb commented Jan 19, 2025

Introduced a util method to convert from image/svg+xml to image/svg+xml;base64 - this allows tests to pass without interfering with the front end

Sounds good! Does this mean the PR is reviewable/mergeable? Maybe update the PR description with the latest news? 😉

@mproffitt
Copy link
Contributor Author

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
gusevda previously approved these changes Jan 19, 2025
Copy link
Contributor

@gusevda gusevda left a comment

Choose a reason for hiding this comment

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

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.

@mproffitt
Copy link
Contributor Author

I think if you're happy for it to go, it's a sed followed by yarn command to drop them and I'd rather start as we mean to go on. Lets set a precedence with choosing the least confusing route for devs 😸

Copy link
Contributor

@gusevda gusevda left a comment

Choose a reason for hiding this comment

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

Awesome!

@mproffitt mproffitt merged commit edfa44a into weaveworks:main Jan 19, 2025
14 checks passed
This was referenced Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants