question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[jest-emotion] snapshots reordering css based on test order, causing failures.

See original GitHub issue

Current behavior: Rendering styled components in jest using @testing-library/react and @testing-library/jest-dom is causing some sort of mutation that changes the order css is written in the snapshot. This causes snapshots to fail when a test is skipped, removed, or re-ordered, among other situations.

It took me many hours to track this down in a large project and it makes our tests very unreliable and causes a ton of noise when the code in many of our styled components changes.

To reproduce: I’ve created a reproduction repo here: https://github.com/Jimmydalecleveland/emotion-jest-reordering-bug

The instructions are in the readme. This is not the only way to reproduce this bug, but it’s the most simple and clear that I could come up with. Thank you, in advance, for looking at this and any help you can give.

Here is a quick example screenshot from the repo I linked which shows the css being re-ordered: image

Expected behavior: The order of the tests should not mutate other test snapshots. Removing, skipping, or moving a test should not fail other tests snapshots due to css being moved around.

Environment information:

  • react version: 16.12.0
  • emotion version: 10.0.27
  • jest version: 24.9.0 (25’s new diffing tool still has conflicts with jest-emotion)
  • jest-emotion version: 10.0.27

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, May 25, 2020

Both tests share the same underlaying cache which is only populated when styles are about to be rendered (so as lazily as we can get). If you have 2 tests - A and B (in that order) and you render both then you end up with A and B styles injected (in that order), if you skip A then you only have B styles injected and this has messed up your snapshots.

1reaction
Andaristcommented, May 25, 2020

getStylesFromClassNames currently works by iterating through extracted classNames: https://github.com/emotion-js/emotion/blob/fc119766b539f73546dc8e2863cca1439af3cb1c/packages/jest/src/utils.js#L168 and appending matches to the output string, so yes - this currently is prone to style elements being reordered.

A possible solution to this would be to change how this is being iterated in a way that would preserve the order of input classNames. Would you like to take a stab at this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Snapshot Testing: The Bad Parts | by Nick Gard
Vague assertions lead to vague failures which lead to vague fixes. Poor Test Behavior. Tests are only as useful as the information they...
Read more >
react css modules causes the snapshot tests to fail
The issue is that i cloned a repo from github, and the tests are failing because of this. (and it will probably fail...
Read more >
Snapshot Testing - Emotion
Adding snapshot tests with Jest is a great way to help avoid unintended changes to your app's UI. By diffing the serialized value...
Read more >
order - CSS: Cascading Style Sheets - MDN Web Docs
The order CSS property sets the order to lay out an item in a flex or grid container. Items in a container are...
Read more >
Testing - Sourcegraph docs
React component snapshot tests are one way of testing React components. ... When an e2e test fails (example), CI displays a snapshot of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found