jest --coverage fails while jest passes
See original GitHub issue🐛 Bug Report
When using coverage with jest, the toString method of the child class is lost.
To Reproduce
Create and error class with it’s own toString method.
export default class ExampleError extends Error {
constructor(status, statusText, url, headers, response) {
super('test message');
}
toString() {
return `replacement serialization`;
}
}
Try testing the above file.
import ExampleError from './ExampleError';
describe('ExampleError', () => {
it('throws the error with the properties attached', () => {
const error = new ExampleError();
expect(String(error)).toEqual('replacement serialization');
});
});
Use this jest config (thought it probably isn’t really required)
module.exports = {
verbose: true,
testURL: 'http://localhost',
};
Test just running jest with no values.
project $ ./node_modules/.bin/jest
PASS src/ExampleError.test.js
ExampleError
✓ throws the error with the properties attached (4ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.799s, estimated 1s
Ran all test suites.
Try running with coverage.
project $ ./node_modules/.bin/jest --coverage
FAIL src/ExampleError.test.js
ExampleError
✕ throws the error with the properties attached (10ms)
● ExampleError › throws the error with the properties attached
expect(received).toEqual(expected)
Expected value to equal:
"replacement serialization"
Received:
"Error: test message"
5 | const error = new ExampleError();
6 |
> 7 | expect(String(error)).toEqual('replacement serialization');
| ^
8 | });
9 | });
10 |
at Object.<anonymous> (src/ExampleError.test.js:7:31)
-----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files | 50 | 100 | 50 | 50 | |
ExampleError.js | 50 | 100 | 50 | 50 | 7 |
-----------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.044s
Ran all test suites.
Expected behavior
I expect both jest
and jest --coverage
to output similar results.
Run npx envinfo --preset jest
Paste the results here:
npx envinfo --preset jest
npx: installed 1 in 1.958s
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
Binaries:
Node: 10.8.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.2.0 - /usr/local/bin/npm
npmPackages:
jest: ^23.4.2 => 23.4.2
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How to resolve Jest issues: tests passing, but code coverage ...
Basically the assertion cannot be verified because it's no longer there, the render phase has passed. Although why this results in passing ...
Read more >Jest test passes, but coverage fails - Stack Overflow
I put Jest Spy in my Class for it to simulate the return. The test passes, but my coverage doesn't. What am I...
Read more >Configuring code coverage in Jest, the right way
In this brief tutorial we see how to configure code coverage for Jest, the right way.
Read more >How to resolve Jest issues: tests passing ... - Delicious Reverie
How to resolve Jest issues: tests passing, but code coverage fails! Today I'm continuing with my trend of making silly mistakes so you...
Read more >Jest CLI Options
If you run Jest via yarn test , you can pass the command line ... Use this flag to re-record every snapshot that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ran into an issue with the
antd
Select component. Jest errors withReferenceError: Select is not defined
when running test runner with --coverage flagjest --coverage
but all my tests pass fine when I runjest
without the flag. Does anyone know how I could fix this? Been googling around with no luck.Edit (update): Turns out I was trying to destructure
const { Option } = Select
and when I refactored to use dot notation<Select.Option>
the error was no longer throwing. Very weird that this only happens onjest --coverage
and not onjest
.@rickhanlonii can you explain why you believe this is not a Jest issue?
I am seeing the same thing and I’m not using Istanbul. I believe this is a Jest issue because regardless of what is used,
jest
andjest --coverage
should produce similar results.