Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running jest test with coverage in typescript create-react-app (v2.1) throws super constructor error #5868

Closed
rsuk opened this issue Nov 21, 2018 · 7 comments

Comments

@rsuk
Copy link

rsuk commented Nov 21, 2018

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

Jest, test, coverage, extends

Environment

npm create-react-app --info actually throws an error (tried on 2 clean installs on Windows 10, 64bit latest version of node). I have reproduced on 2.1.1 of create-react-app.

Error when running npx create-react-app --info :

Environment Info:
(node:9568) UnhandledPromiseRejectionWarning: Error: The system cannot find the path specified.

Steps to Reproduce

Create a new react app with typescript support (create-react-app v2.1 or higher required):

npx create-create-app jest-test --typescript

Add a new test file named "classes.tsx" to the src/ folder containing this code:

class Base {
  public counter: number = 0;
  increment() {
  
  }
}

class Derived extends Base {
  constructor(public anotherCount: number) {
    super();
  }
}

export { Base, Derived };

Edit the existing src/App.test.tsx file, adding this import:

import { Base, Derived } from "./classes";

.. and this extra test:

it('coverage trigger', () => {
  const derivedInstance = new Derived(1);
  derivedInstance.increment();
});

Edit package.json and add a new script below "scripts" to run tests with coverage:

"test:coverage": "react-scripts test --coverage"

From a command line in the project directory, run npm run test:coverage

Expected Behavior

The tests should complete without error with the --coverage option enabled.

Actual Behavior

The following error appears:

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

       8 | class Derived extends Base {
    >  9 |   constructor(public anotherCount: number) {
         |                                            ^
      10 |     super();
      11 |   }

Note that running the tests without the --coverage option works fine.

Reproducible Demo

https://github.com/rsuk/repros/tree/master/jest-typescript-coverage-issue

From a command line in the project directory, run npm run test:coverage

@rsuk
Copy link
Author

rsuk commented Nov 21, 2018

The issue appears to fit the description of an open issue with istanbul: Issue 237

.. which is the plugin used for coverage by jest in create-react-app

@stale
Copy link

stale bot commented Dec 21, 2018

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Dec 21, 2018
@stale stale bot removed the stale label Dec 23, 2018
@Timer Timer added this to the 2.1.x milestone Dec 23, 2018
@iansu iansu modified the milestones: 2.1.x, 3.x Mar 10, 2019
@ysfaran
Copy link

ysfaran commented Apr 26, 2019

This bug is still present in version 3.0.0. Is there any workarround?

@goodwin64
Copy link

Are there any news?

@SimenB
Copy link
Contributor

SimenB commented Jun 19, 2019

You're waiting for Babel 7.5.0 release, which will include babel/babel#9610

@SimenB
Copy link
Contributor

SimenB commented Jul 4, 2019

https://babeljs.io/blog/2019/07/03/7.5.0

@iansu
Copy link
Contributor

iansu commented Jun 30, 2021

This has been fixed in Babel and we've upgraded to a version that includes this fix.

@iansu iansu closed this as completed Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants