My project uses a css-in-js library (in this case cxs) to apply styling. The cxs library appends a style tag to the <head> at runtime and adds style definitions to this injected tag.
When tests run normally, styling appears as expected.
However, when switching between snapshots, injected style tags (css-in-js) is not preserved and styling is broken on the page
Styles should be preserved, or the javascript bundle should be reran so that styles can be injected again.
(although the second option sounds less likely to be reliable given the nature of snapshotting).
Run Cypress with a project that uses a css-in-js library that appends a style tags in <head> at run time.
(pseudo) DOM structure while tests are running (correct behavior):
<iframe id="Your App: 'my app name'">
...
<head>
...
< !-- style tag injected by cxs -->
<style id="_cxs_"></style>
...
</head>
...
</iframe>
(pseudo) DOM structure while navigating between snapshots after tests have finished:
<iframe id="Your App: 'my app name'">
...
<head>
...
<!-- I did notice that there is still a style tag in the head
that _could_ be the injected style tag, but its missing
its style declarations, and no longer has the `_cxs_` id, so this could just be a red herring -->
<style></style>
...
</head>
...
</iframe>
Great observation, that is a problem. Can you create a tiny example, for example by forking https://github.com/cypress-io/cypress-test-tiny so we are looking at the same common example?
So I put together a create-react-app that demonstrates the issue here. Setup instructions and explanation of whats happening are in the README. But I'll paste the relevant bits here:
The cxs example loses styling when navigating between snapshots.
However, the styled-components styling is retained. So it seems this might be an issue explicitly with how cxs is injecting styles. You can see here that cxs uses styleTag.sheet.insertRule to inject styles, and these styles that get inserted aren't actually viewable in the DOM inspector; it seems they are attached to the element directly.
Thanks for taking time to do this.
No problem! Let me know what other info I can provide.
However, the styled-components styling is retained.
I use styled-components@^3.2.3 and styling is not retained for me
I use emotion.js and styling is broken
Released in 3.0.2.
I'm experiencing the same issue with css-in-js lib styletron@5. Cypress version is 3.4.1.
This issue will be closed to further comment as the exact issue here was resolved and tested.
@jaxxreal If you're experiencing a bug similar to this in Cypress, please open a new issue with a fully reproducible example that we can run. There may be a specific edge case with the issue that we need more detail to fix.
Most helpful comment
I use
emotion.jsand styling is broken