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.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, Jul 4, 2019
1reaction
SimenBcommented, Jun 19, 2019

You’re waiting for Babel 7.5.0 release, which will include https://github.com/babel/babel/pull/9610

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
By default, Jest runs all tests and produces all errors into the console upon completion. The bail config option can be used here...
Read more >
ts17009: 'super' must be called before accessing 'this' in the ...
facebook/create-react-appRunning jest test with coverage in typescript create-react-app (v2.1) throws super constructor error#5868.
Read more >
Typescript Super is undefined when running unit tests with jest
I use super.catch inside catch from derived class. And code works fine when I do manual testing. But when I run jest unit...
Read more >
Setup - Testing Library
In these docs we'll demonstrate configuring Jest, but you should be able to do similar things with any testing framework (React Testing ......
Read more >
typescript-cheatsheet - GitHub Pages
const numbers = [1, 3.22, 6, -1] // This variable will automatically be assigned a number[] array type. Note that declaring types is...
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