-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
toHaveStyleRule does not work with babel-plugin-styled-components #290
Comments
I have the same issue using
import styled from 'styled-components/macro';
const MyComponent = styled.div`
-webkit-text-stroke: 1px white;
cursor: pointer;
`;
test('it works', () => {
const { container } = render(<MyComponent />);
const component = container.firstChild;
expect(component).toHaveStyleRule('-webkit-text-stroke', '1px white');
expect(component).toHaveStyleRule('cursor', 'pointer');
}); Result:
|
I don't think that Not yet sure where the culprit lies, but I do know that in my case, simply changing where the component is rendered (i.e. moving it into the test itself, instead of at the top level ... yeah 🤷♂ ) makes it work well again. Update: I now think my comment above is probably not relevant for this issue, as I've tracked down the source of my errors. Specifically, this is new: https://github.com/styled-components/jest-styled-components/blob/master/src/index.js#L5. We used to reuse rendered components in multiple tests (I guess that's a bit of an anti-pattern anyway), but now there's |
I can confirm that this issue is present, after updating styled-components to v5 and jest-styled-components to v7 (with babel-plugin-styled-components v1.10.6). Worked fine before! |
As far as I understand this line, that was introduced with the following commit.
Unfortunately if your project is bootstrapped with Create React App, and you cannot simply add babel-plugin-styled-components you have to use Babel Macro instead eg:
that change class names from A quickfix for that I suggest here, is to change this line in
to filter not only starting with
It works properly for both new setup (styled-components to v5 and jest-styled-components to v7) as well as older one. |
Setting the ssr and displayName plugin parameters to false helped me during testing.
|
@weyheyhey is there a way to apply this setting just to my test folder? I've tried all the tricks I know with |
@visoft you can try something like that in
|
Above fix seems to work, but now |
@stefee Is the plan to get this working without such fixes or should I proceed with my project to try these workarounds? Would hope there'd be no breaking changes 🤞 |
@mulholio |
As a alternative fix, I submitted styled-components/babel-plugin-styled-components#268 to The correct solution depends on the how styled-components should work. |
My team is experiencing this issue as well for the tests on our design system as we upgrade to Styled Components V5. Tests were working fine just before the upgrade. |
I am also experiencing this issue and found that coping the change @robilars-amazon suggests in styled-components/babel-plugin-styled-components#268 didn't actually fix the issue for me. I have used the workaround suggested by @weyheyhey to disable "displayName" when running tests and found it works in my case. |
Thanks for taking care of this @probablyup ! Excited to see next release ship. |
It's still not working for me with |
Same here have also tried upgrading to v5 again today (with jest-styled-components v7) but also still run into this problem. |
Me too, upgraded to v5 and had this issue pop up. |
If you're using create-react-app and the babel macro (
and run your test script with |
This is still an issue on: "jest-styled-components": "7.0.8",
"styled-components": "5.3.1",
"babel-plugin-styled-components": "2.0.7",
"@babel/core": "7.17.9" And I cannot disable displayName for the babel plugin as we use it in testing |
For anyone else still struggling with this issue. The commonly recommended solution below "worked"..
Though we use the |
When using the latest version alongside
babel-plugin-styled-components
, thetoHaveStyleRule
matcher always fails ("No style rules found on passed Component"
).This issue seems to have been caused by this commit: 8c2ea4a#diff-4eed74593d3d8efde6a0959c9c35119bR71
Specifically, this line assumes classnames will have a
sc-
prefix which is not present:(similar issue to #285)
The text was updated successfully, but these errors were encountered: