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

[docs] StylesProvider to avoid ID changes with RTL snapshots #42986

Closed
challme28 opened this issue Jul 17, 2024 · 3 comments
Closed

[docs] StylesProvider to avoid ID changes with RTL snapshots #42986

challme28 opened this issue Jul 17, 2024 · 3 comments
Assignees
Labels
package: system Specific to @mui/system support: docs-feedback Feedback from documentation page test

Comments

@challme28
Copy link

challme28 commented Jul 17, 2024

Related page

https://mui.com/system/styles/api/#stylesprovider

Kind of issue

Missing information

Issue description

As described in this medium article, Snapshot tests become much less useful when paired with MaterialUI’s CSS-in-JS solution. At runtime, MUI guarantees globally unique class names for your app by adding incremental IDs to the end of the generated class names. This class generation method leads to frivolous snapshot updates like the example below:

- emotion-8 .MuiChip-avatarSmall
+ emotion-9 .MuiChip-avatarSmall

Similarly, I've found that ripples are sometimes caught in the tests and fail. Re-testing makes the ripples caught go away or actually stay for subsequent tests.

The author's solution is to use StylesProvider with generateClassName to create a wrapper for RTL's render function to overwrite how these class names are created.

I'm open to believing that my user events like clicking and interacting with the UI also change the snapshots because of some kind of race condition.

Context

MUI's legacy StylesProvider imports it from @mui/styles. As suggested by the deprecated warning, @mui/system installation docs doesn't have anything related to StylesProvider. Furthermore, I found in the migration guide to v5 a mention of it and a possible candidate to what I'm looking for in the experimental API called classname-generator. I also found this StackOverflow question related to my concerns but also led to not enough documentation about StyledEngineProvider as a replacement for StylesProvider

I want to be able to get the best of snapshot testing without MUI's auto-generated class names giving false negatives or a caught ripple in a component.

Search keywords: snapshot classname react-testing-library RTL v5

@challme28 challme28 added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Jul 17, 2024
@brijeshb42
Copy link
Contributor

There is no StylesProvider but we do have ThemeProvider which you can use to customize the behavior. For starters, you can disable the ripple for your tests.
Also, have you set-up emotion's jest serializer if you are using jest ?

@brijeshb42 brijeshb42 self-assigned this Jul 18, 2024
@brijeshb42 brijeshb42 added status: waiting for author Issue with insufficient information test package: system Specific to @mui/system and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 18, 2024
@challme28
Copy link
Author

There is no StylesProvider but we do have ThemeProvider which you can use to customize the behavior.

I don't see an option to customize the class names

For starters, you can disable the ripple for your tests.

tyvm. It cleared a lot of snapshots.

Also, have you set-up emotion's jest serializer if you are using jest ?

We are using vitest and we are using createSerializer()

import { createSerializer } from '@emotion/jest';

expect.addSnapshotSerializer(createSerializer());

I'm starting to think that my issue is more related to what gets to be rendered at the time of the snapshot rather than the styles created.
I don't want to close the issue in case I do need to customize the class names. I'll try to stress-test and see if I'm doing the tests wrong.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Jul 22, 2024
@challme28
Copy link
Author

I'll close this because the main issue was my bad test.
The state of the component at the time of the snapshot was not idempotent, thus, resulting in different snapshots some times.

@github-actions github-actions bot removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: system Specific to @mui/system support: docs-feedback Feedback from documentation page test
Projects
None yet
Development

No branches or pull requests

2 participants