Jest snapshots different classnames
See original GitHub issueEverytime i run snapshot test with jest and rest-testing-library for component with tooltip inside, new css classes are generated and thus snapshot is failing. Any ideas to solve this ?
- Snapshot
+ Received
@@ -49,80 +49,80 @@
>
XSS
</div>
</a>
<div
- class="__react_component_tooltip t478c1214-c3a1-4e62-b8d9-bec855c08c42 place-top type-dark"
+ class="__react_component_tooltip t6228f914-bd48-4261-96f8-0d9fbdc0096a place-top type-dark"
data-id="tooltip"
id="sizePicker-1001"
>
<style>
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42 {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a {
color: #fff;
background: #222;
border: 1px solid transparent;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-top {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-top {
margin-top: -10px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-top::before {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-top::before {
border-top: 8px solid transparent;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-top::after {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-top::after {
border-left: 8px solid transparent;
border-right: 8px solid transparent;
bottom: -6px;
left: 50%;
margin-left: -8px;
border-top-color: #222;
border-top-style: solid;
border-top-width: 6px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-bottom {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-bottom {
margin-top: 10px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-bottom::before {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-bottom::before {
border-bottom: 8px solid transparent;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-bottom::after {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-bottom::after {
border-left: 8px solid transparent;
border-right: 8px solid transparent;
top: -6px;
left: 50%;
margin-left: -8px;
border-bottom-color: #222;
border-bottom-style: solid;
border-bottom-width: 6px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-left {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-left {
margin-left: -10px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-left::before {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-left::before {
border-left: 8px solid transparent;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-left::after {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-left::after {
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
right: -6px;
top: 50%;
margin-top: -4px;
border-left-color: #222;
border-left-style: solid;
border-left-width: 6px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-right {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-right {
margin-left: 10px;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-right::before {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-right::before {
border-right: 8px solid transparent;
}
- .t478c1214-c3a1-4e62-b8d9-bec855c08c42.place-right::after {
+ .t6228f914-bd48-4261-96f8-0d9fbdc0096a.place-right::after {
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
left: -6px;
top: 50%;
margin-top: -4px;
31 | const { asFragment } = render(component);
32 |
> 33 | expect(asFragment()).toMatchSnapshot();
| ^
34 | });
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:6
Top Results From Across the Web
Jest snapshots different classnames · Issue #595 - GitHub
Everytime i run snapshot test with jest and rest-testing-library for component with tooltip inside, new css classes are generated and thus ...
Read more >Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >Snapshot tests className with emotion/styled - Stack Overflow
Everytime I update a component the className in my snapshot tests updates because of how emotion/styled uses hash for classNames. enter image ...
Read more >Tech Poppy: Challenges with Snapshot Testing in Styled ...
Enter jest-styled-components. This utility helpfully serializes the class names that styled-components generates for cleaner, and more ...
Read more >Styled Components + Snapshot Testing + Jest - Medium
Styled Components + Snapshot Testing + Jest. Do your styled component classnames keep changing when you run snapshot tests? Not to worry.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This first started to occur with version
4.0.5
(see #562). The proposed solution was to adduuid
as a dependency and have developers mock the module (see #566):Initially
uuid
was bundled todist/index.js
by rollup souuid
could not be mocked. That got fixed with #582.The current implementation has certain limitations. For example, if your package (or a dependency) depends on a non-compatible
uuid
version (as ofreact-tooltip@4.2.6
its^7.0.3
),uuid
will be installed innode_modules/react-tooltip/node_modules/uuid
. This requires to change the mocking to:This could be fixed by making
uuid
apeerDependency
. However that would result in a breaking change, since consumers with no dependency touuid
will get a Cannot find module ‘uuid’ error message. Instructions for installation would need to be updated as well.Another alternative that does not depend on
uuid
’s location is to mockcrypto
’srandomBytes
function like so:However, this approach can potentially break other testing that depend on the core
crypto
module.As you can see, no solution is without its hiccups, and besides, none of these approaches are officially documented. I believe a more robust approach would be to expose a static method or property that could be executed so that generated uuids are static. For example:
@marcogrcr Thank you for the initial ideas. I’ve found a workaround for the
Cannot find module
message that tests for package availability in a tricky sort of way to survive hoisting.That should make the tests a bit more reliable across dependency and react-tooltip updates.