question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing: TypeError: Cannot read property 'apply' of undefined at StyledComponent.componentWillMount

See original GitHub issue

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>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:17

github_iconTop GitHub Comments

15reactions
timhuffcommented, Feb 8, 2018

I resolved this in the following way: I modified package.json in accordance with the jest docs by adding this:

  "jest": {
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|less)$": "identity-obj-proxy",
      "isomorphic-style-loader/lib/withStyles": "<rootDir>/tools/mocks/withStyles.js"
    }
  }

Notice that I’ve appended a moduleNameMapper for isomorphic-style-loader/lib/withStyles. I then created the specified file: I created a mocks folder in the same folder as the webpack config (tools). I created a file in that folder called withStyles.js. It has the following content:

module.exports = () => component => component
5reactions
bhargavigundaacommented, Sep 6, 2016

Beside exporting the component with withStyles decorator, we need to export it as a stand alone.This will solve the issue

export class GridView extends Component { ... }

export default withStyles(s)(GridView);

And in test component use standalone one

import GridView from './GridView';

and in other cases use default one

import { GridView } from './GridView';
Read more comments on GitHub >

github_iconTop 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 >
React TypeError: Cannot read property function of undefined
SET YOUR LIKE THERE ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found