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:
- Created 5 years ago
- Comments:6
From the original error, I believe you can fix this by removing the parameter property.
Do this instead:
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.