Testing styled component with enzyme
See original GitHub issueI got theme defined in the app and the child component contains style like
color: ${props => props.theme.options.color};
if i use Enzyme to mount
the parent component, it cause TypeError: Cannot read property of undefined
Is there any solution for this issue, i try to setContext(__styled-component__)
in the test but it doesn’t work.
Issue Analytics
- State:
- Created 6 years ago
- Comments:33 (5 by maintainers)
Top Results From Across the Web
Test styled component with Jest-Enzyme - Stack Overflow
The test runner is Jest and I use the testing utilities library Enzyme. reactjs · unit-testing · jestjs · enzyme · styled-components ·...
Read more >Testing React Styled Components with Jest & Enzyme | Neon
We have covered how to test the expected functionality of components by using the jest-styled-components library we installed earlier, we can ...
Read more >Targeting styled components with enzyme's .find() #896 - GitHub
If I use an imported styled-component like <ImportedComponent> , I can easily find it like with enzyme like: describe('<Thing />', () => {...
Read more >Test styled components in React efficiently using 'displayName'
This means that we are traversing the component tree using the actual component, rather than just the name(which is one of the benefits...
Read more >Unit Testing Styled Components - Medium
Unit testing React components isn't always easy. In this short article i'll show some tricks to make our lives a little easier using...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When I use styled-components to make “building block” components defined in the same file as the “assembled” component, I am having a really difficult time targeting them in my tests using enzyme’s
.find()
method.For example, with this styled-component:
If I use an imported styled-component like
<ImportedComponent>
, I can easily find it like with enzyme like:But I haven’t found an elegant way to find the “building block” components like
<Label />
because they can’t be found by the component name as they appear more generically like “<styled.span>”. Is there a way for these “building block” components to appear with their component name like they are when imported? I don’t want to resort to targeting them by html or css as that is more brittle.created a workaround but it might not be the best solution.
now i can get the wrapper component’s
state()
by using:const wrapper = mountWithTheme(<Component {...props} />);