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

Jest snapshot testing passing unique classNames #29

Closed
k15a opened this issue Jun 13, 2017 · 4 comments
Closed

Jest snapshot testing passing unique classNames #29

k15a opened this issue Jun 13, 2017 · 4 comments

Comments

@k15a
Copy link
Member

k15a commented Jun 13, 2017

Moved form styled-components/styled-components#916 by @corbanbrook

Styled components generate a unique classname hash based on the style content and passes this classname to the snapshot. This causes snapshots to break when styles are adjusted.

I understand this might be useful if you want to break snapshots on style change or even show style diffs with the jest-styled-components plugin but by including the hashed classname it ends up not only breaking the snapshot of the changed component but also the snapshots of any components that wrap it. This can be a mild annoyance when adjusting a Button styling causes hundreds of snapshots all over the application to break because they included Button.

CSS Modules had a similar issue but solved it with the identity-obj-proxy lib and moduleNameMapper:

"moduleNameMapper": {
  "\\.(css)$": "identity-obj-proxy"
 }

which would convert className={styles.myStyle} to className="myStyle" in the snapshot rather than "myStyle-{unique-hash}".

Is there something similar for mocking in classnames for styled-components?

@geelen
Copy link
Member

geelen commented Jun 15, 2017

Had another bug report similar to this, with a reproduction repo that I've figured out the problem for and left some description: JamieDixon/sc-jest-issue#1

Basically, ordering causes classnames to jump around wildly unless you're using babel-plugin-styled-components in your jest test environment. Which kinda sucks, since the generated classnames should be free to change without invalidating your snapshot. So I'm not sure that that fix is appropriate, I think building something into this library that ignores classname churn would be better.

@MicheleBertoli
Copy link
Member

Thank you very much @philpl @k15a @geelen and @JamieDixon.

Given this:

So, the bug is caused because the class names are influenced by the order in which the styled components are created. This is an unfortunate necessity, since we have nothing else to uniquely identify which component is which at runtime.

I strongly agree the problem should be fixed by this package.

@MicheleBertoli
Copy link
Member

I implemented a solution, which seems to work as expected.
Feedback is welcome :)

@MicheleBertoli
Copy link
Member

🙌 v4 is out
Thanks to everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants