Testing: TypeError: Cannot read property 'apply' of undefined at StyledComponent.componentWillMount
See original GitHub issueAfter setting up tests with Jest and getting through the auto-mocking issues described in #264, I’m getting:
TypeError: Cannot read property 'apply' of undefined at StyledComponent.componentWillMount (decorators/withStyles.js:19:50)
The specific line in the withStyles decorator causing the error is:
this.removeCss = this.context.insertCss.apply(undefined, styles);
insertCss is undefined. How can the context be passed into the decorator for testing?
<bountysource-plugin>
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:17
Top Results From Across the Web
React testing library and styled components - Stack Overflow
React testing library and styled components - cannot read properties of undefined ; from "react"; ; "styled-components/macro" · import { rem } ...
Read more >Cannot read property 'type' of undefined while using react ...
Coding example for the question Cannot read property 'type' of undefined while using react-select with formik-Reactjs.
Read more >jest cannot create styled-component for component: undefined.
React Jest Testing TypeError: Cannot read properties of undefined (reading 'current'). Asked Apr 25, 2022 • 1 votes 0 answer. QUESTION ANSWERS.
Read more >The React Handbook – Learn React for Beginners
(in the past you could do this using f.apply(null, a) but that's not as nice and ... If you only have a getter,...
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
I resolved this in the following way: I modified
package.json
in accordance with the jest docs by adding this:Notice that I’ve appended a moduleNameMapper for
isomorphic-style-loader/lib/withStyles
. I then created the specified file: I created amocks
folder in the same folder as the webpack config (tools
). I created a file in that folder calledwithStyles.js
. It has the following content:Beside exporting the component with withStyles decorator, we need to export it as a stand alone.This will solve the issue
And in test component use standalone one
and in other cases use default one