jest-styled-components: jest-styled-components is breaking with new 5.0.0 styled-components
We just udpated to styled-components 5.0.0 and our snapshot tests are breaking:
> 3 | import 'jest-styled-components';
Could neither find styled-components secret internals nor styled-components/lib/models/StyleSheet.js
I can see that you had the pre 5.0.0 your package.json
.
Is this expected?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 18
- Comments: 36 (3 by maintainers)
Commits related to this issue
- npm install jest-styled-components This is makes reading the diffs of snapshots easier to read if the diff is regarding a changed style in a styled component. Note: a previous version was installed ... — committed to iFixit/product-page-components by zdmitchell 4 years ago
- test(package.json): uninstall jest styled components library Uninstall jest styled components library as it has a bug that breaks the test. See https://github.com/styled-components/jest-styled-compon... — committed to LisaPisa12/React-Customizable-Timeline by erikpr1994 4 years ago
toHaveStyleRule
expectations are failing after upgrading styled components to 5 and jest-styled-components to 7.That error you see has been resolved when you update jest-styled-components to 7.0.0
yup, styled-components 5.0 + jest-styled-components 7.0 + Typescript also not happy:
I have upgraded to 7.0.0 and are experiencing the exact samt problem.
thanks @mirco312312 - since it took me more than a few minutes to solve, here was the diff in my
package.json
that worked:replaced:
"setupFiles": ["./spec/setupTests.js"],
with:"setupFilesAfterEnv": ["<rootDir>/spec/setupTests.js"],
after two months, the problem is still existing. I have to use styled-components v4.4.1 and jest-styled-components 6.3.3 due to this problem.
Not working with react-native v0.61.5 and
styled-components
v5 /jest-styled-components
v7. GetTypeError: global.beforeEach is not a function
#302 does not fix the issue. jest-styled-components looks for the presence of “sc-” anywhere in the component id. babel-plugin-styled-components does not guarantee that the component id will contain the “sc-” substring.
The issue is that babel-plugin-styled-components only applies the “sc-” prefix if it detects the component ID has an invalid css classname, specifically whether it is prefixed by a digit.
Temporarily fixed in my project via patch-package and the following patch for version 1.10.7 which modifies the getComponentId function to ensure it contains “sc-”:
I’m also experiencing issues for tests that used to work on v4.4… places where
toHaveStyleRule
is used are generating:Error: No style rules found on passed Component
related to this issue
I had this issue with
styled-components@5.3.1
andjest-styled-components@7.0.5
. Turns out that in addition to importingjest-styled-components
insetupTests.js
(which is run once before all tests) I was also importing it in one other test which was breaking all tests.Removing the second import resolved my issue.
for me worked with this versions:
"styled-components": "^5.2.3"
"jest-styled-components": "^7.0.3"
On that note, is it better to just remove the class name filtering entirely?
@lucianomlima are you using global installation approach?
Note the deprecation warning here: https://jestjs.io/docs/en/configuration#setupfilesafterenv-array
When importing from a file referenced via
jest.setupFilesAfterEnv
its working for me with v5/v7.@Thomazella yes we dropped support for v4 with the v7 release. The internals changed a lot between v4 and v5 and the library needed incompatible updates.
However theres a few other errors occurring for me after upgrading, would be nice to see the changelog to 7.0.0
The fix I was referring to was a fix for the babel plugin.
Temporary? workaround for
"styled-components": "^5.1.0"
and"jest-styled-components": "^7.0.2"
is to render the component inside the test.failing test
workable test
Thanks. I tried
jest-styled-components v6
is working fine withstyled-components v4
.Can confirm latest is broken with styled-components 4.4.1. Does a version rollback fix?
edit: can confirm version v6.3.4 works with styled-components 4.4.1