react-starter-kit: Testing: TypeError: Cannot read property 'apply' of undefined at StyledComponent.componentWillMount
After 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>
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 17
I resolved this in the following way: I modified
package.jsonin 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 amocksfolder 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
For now, I patched it with
If you want to add storybook to the project:
inside .storybook/ create a new file called ContextProvider.js
update .storybook/webpack.config.js
and finally, inside .storybook/config.js
does anyone have a working Storybook example with this boilerplate?
@bhargavigundaa’s approach works for me fine on classes, but I on functional components where I
export const GridView = ({ foo, bar }) =>I still run into the same apply of undefined.
This worked for me: