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.

TypeError: Class constructor App cannot be invoked without 'new'

See original GitHub issue

If you are reporting a bug, please fill in below. Otherwise feel free to remove this template entirely.

Can you reproduce the problem with latest npm?

Yes Yes

Description

Trying to use React CSS Modules inside create-react-app, only during Jest tests, leads to:

TypeError: Class constructor App cannot be invoked without 'new'

Expected behavior

The code should work fine, like it happens when I use yarn start

Actual behavior

I get the error reported above.

Environment

Run these commands in the project folder and fill in their results:

  1. npm ls react-scripts (if you haven’t ejected): 0.9.4
  2. node -v: 7.5.0
  3. npm -v: 4.1.2

Then, specify:

  1. Operating system: macOS Sierra
  2. Browser and version: no browser

Reproducible Demo

This is a super simple repository created with create-react-app: https://github.com/FezVrasta/create-react-app-css-modules-bug

Just run yarn test to see the error. The only edited file is: https://github.com/FezVrasta/create-react-app-css-modules-bug/blob/master/src/App.js

Where I added react-css-modules

Note that this error happens only when I use create-react-app as base, in other repositories setup from scratch it works fine, but I can’t understand why.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
gaearoncommented, Mar 7, 2017

The problem here is likely that react-css-modules uses extends via Babel, but AFAIK you can’t extend a native class with a Babel-defined class due to the code Babel generates. Since we use native Node classes in Jest configuration, and your component is a native class, extending from it via Babel breaks.

While we could fix it by always applying the class transform, we gotta move to real ES classes at some point, so it’s still a time bomb. I would just suggest avoiding libraries that try to inherit from your classes, or bringing it up with those libraries to not use the Babel class transform.

I hope this helps!

1reaction
gaearoncommented, Mar 7, 2017

Also, now that I think of it, I don’t really see why you’d want to apply react-css-modules in tests. So you might as well mock it to return component untouched:

jest.mock('react-css-modules', () => Component => Component);

This would probably circumvent the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript ES6 TypeError: Class constructor Client cannot be ...
When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'.
Read more >
Class constructor cannot be invoked without 'new' in TS
The "Class constructor cannot be invoked without new" error occurs when the target property in tsconfig.json is set to lower than es6 or...
Read more >
`TypeError: Class constructor cannot be invoked without 'new ...
Bug report. Getting the following error in my project: TypeError: Class constructor cannot be invoked without 'new'.
Read more >
Reactjs " Class constructor upload cannot be invoked without ...
Im trying to activate a function with reactjs But i keep getting the same error: Uncaught TypeError: Class constructor Upload cannot be ...
Read more >
Class constructor SweetAlert cannot be invoked without 'new ...
Uncaught TypeError: Class constructor SweetAlert cannot be invoked without 'new' ... Hi,. After updating meteor version, I can not use the ...
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