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
Migrate to Styled Components v5 breaks snapshots #276
Comments
which versions specifically are you using? |
System: |
@shraddha-2407 what test approach are you using? Enzyme shallow rendering? React-testing-library? Something else? I'm having quite different problems (see #285) with my setup, also on SC v5 and jest-styled-components v7 beta. |
I'm having a similar problem but only for some of the snapshots. About a third of the tests kept working but the rest are falling as it renders styled-components@5.0.0 I'm not using the babel plugin... System: |
I am also seeing lots of diffs in the snapshots, indicating that <a
- className="c3 c4"
+ className="sc-fzXfNK c3 c4"
href="/"
onClick={[Function]}
> Happy to help out if someone gives me some pointers! |
On top of the classname change (which to me isn't that big of a problem) we also got some extra things popping up in the snapshots. Like with
|
@jure - can you confirm if that specific scenario you've screen grabbed uses Referring To Other Components? My suspicion is the current serialiser does not look at components that have not been rendered yet: jest-styled-components/test/styleSheetSerializer.spec.js Lines 200 to 252 in e9ef3e0
As you can see in the matching snapshot, it will use the hash over the reduced classNames: jest-styled-components/test/__snapshots__/styleSheetSerializer.spec.js.snap Lines 537 to 545 in e9ef3e0
This has made it difficult to migrate our source code from 4.2.x to 5.x due to these snapshots now taking the hash over the generated classNames which we have found to be non-deterministic in the past. |
@byronmejia no, unfortunately we don't use this way of referring to components. For example, the component that now shows the hashes instead of the const common = css`
color: ${th('colorPrimary')};
font: ${th('fontInterface')};
font-size: ${th('fontSizeBase')};
font-weight: ${props => (props.active ? 'bold' : 'normal')};
text-decoration: none;
text-transform: none;
transition: ${th('transitionDuration')} ${th('transitionTimingFunction')};
&:hover,
&:focus,
&:active {
background: none;
color: ${th('colorPrimary')};
text-decoration: underline;
}
${override('ui.Action')};
`
const Button = styled(OriginalButton)`
background: none;
border: none;
min-width: 0;
padding: 0;
${common};
`
const Link = styled(OriginalLink)`
${common};
`
I'll try to dig a bit deeper. |
I'm also experiencing this problem. This is the simplest example I could come up with to reproduce the problem, seems to happen whenever the Here is an implementation to demonstrate: import React from "react";
import styled from "styled-components";
import "jest-styled-components";
const Foo = styled.p`
color: blue;
`;
const Bar = styled(Foo)`
color: red;
`;
export default () => <Bar>test</Bar>; A test with import React from "react";
import { render } from "@testing-library/react";
import Foo from "./Foo";
test("it works", () => {
const { container } = render(<Foo />);
expect(container.firstChild).toMatchSnapshot();
}); Generates a snapshot like this: // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`it works 1`] = `
.c0 {
color: blue;
color: red;
}
<p
class="sc-AxjAm c0"
>
test
</p>
`; I also tried with |
Also, it looks like this scenario isn't covered by tests, this is the only test I saw that uses Although this one is fine because the the component being extended is not a styled-component. I just realised the snapshots for "referring to other components" actually includes auto-generated hashes (like Are those hashed that supposed to be the snaphots? 🤔 |
@probablyup @MicheleBertoli @mxstbr - is the fix here to just clear out any unreferenced As @probablyup mentioned on styled-components/styled-components#2980 (comment) these are static classes for component selector targeting, so can we just strip them? We're getting updated classes in every snapshot (I think, with a new component / order change in a top level barrel file) which is very noisy. - className="c28 sc-LzLOJ c37"
+ className="c28 sc-LzLOQ c37" The problem seems to be that in the case that a component extends from a component that isn't itself used, Simplest repro: it('breaks', () => {
const Container = styled.div`
background: red;
`;
const StyledContainer = styled(Container)`
background: blue;
`;
const component = (
<StyledContainer>Hi</StyledContainer>
);
expect(render(component).container.firstChild).toMatchSnapshot('react-testing-library');
}); returns: exports[`breaks: react-testing-library 1`] = `
.c0 {
background: red;
background: blue;
}
<div
class="sc-AxjAm c0"
>
Hi
</div>
` and with exports[`breaks: react-testing-library 1`] = `
.dZTZYu {
background: red;
background: blue;
}
<div
class="sc-AxjAm sc-AxirZ dZTZYu"
>
Hi
</div>
` Thanks folks! Would really love to get this one fixed. |
Style Components v5 will break class orders so we stay at v4 for now. See: styled-components/jest-styled-components#276
Same issues here with the empty media queries (#276 (comment)). What's worse is that the storyshots created under Windows differ from the storyshots created under Linux because of a different number of empty media queries written. Update: We were able to resolve this issue by adding an explicit import to jest-styled-components as documented here: https://github.com/styled-components/jest-styled-components#global-installation The empty media queries are no longer generated. |
is this fixed?. i am still facing this issue |
I'm also seeing this with code like this const StepSequenceItemStyle = styled.li`
${StyledIcon} {
position: relative;
} - .c1 .c4 {
+ .c1 .sc-AxiKw { I'm trying to update to the latest versions:
The original snapshot (
|
This did not work for me.
|
Faced with same issue when extending component. For code: |
any updates?🤔 |
This looks fixed for me in v7.0.3 - Thanks! 🎉 |
This is still happening for me, with the exact versions above even after |
Well, it also depends how you use |
I'm still seeing Given the following layout
And styling ChildComponent via ParentComponent as such
My snapshot has the following:
Edit: looks like this is to do with wrapping the child component |
@robert-hurst-cmd could you please provide the entire file including imports? It would be ideal if you can provide a standalone repro steps |
Still seeing a lot of empty media queries with these versions:
|
I'm also experiencing this issue after upgrading |
Any updates guys? It seems like every release of |
|
it appears that the empty media queries are still present with |
I am still getting the empty media queries. Any updates? |
This is still not fixed in |
I'm having multiple empty media queries as well in |
Same issue |
Hey everyone! I found a workaround to this issue but given I'm not an expert on the codebase I would need some help from the community. If I change the following line:
to be: const atRules = getAtRules(ast, filter).filter(entry => entry.rules.length > 0); Makes all the empty media queries go away (this fails with @supports rules as well). Looks the way the serializer search for rules, in some cases generates empty rules, so adding the filter fixes the problem on the project I'm working on. Hope we could fix the problem with this! |
Hi, thanks for a great library :)
I've updated to Styled Components v5, and while everything seems to be fine, the snapshots have broken. I have also upgraded to beta version of jest-styled-components -> v7.
I don't want to update any snapshots until I really understand what the changes are.
I am including an image below — a lot of the changes seem to be to class names, and also random insertion of empty media queries but there is also removed styles which appear further down, see attached. Anyone know why this is happening? Any advice would be much appreciated!
The text was updated successfully, but these errors were encountered: