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

🐛 Bug Report

When running jest tests with coverage enabled in a create-react-app react project (v2.1, which uses Babel 7 I believe) that contains typescript classes using inheritance (extends), this error is thrown:

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 |   }

Running the tests without the --coverage option works fine.

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.

Link to repl or repo (highly encouraged)

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

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Windows 10
    CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
  Binaries:
    npm: 5.6.0 - C:\Program Files\nodejs\npm.CMD

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
Joshproxycommented, Jul 8, 2019

From the original error, I believe you can fix this by removing the parameter property.

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

Do this instead:

class Derived extends Base {
    constructor(anotherCount: number) {    
        super();
        this.anotherCount = anotherCount;
    }
0reactions
github-actions[bot]commented, May 11, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running jest test with coverage in typescript create-react-app ...
Jest, test, coverage, extends Environment npm create-react-app ... typescript create-react-app (v2.1) throws super constructor error #5868.
Read more >
Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... By default, Jest runs all tests and produces all errors into the console...
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 >

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