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.

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:closed
  • Created 5 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
alberthuynh91commented, Oct 8, 2018

Ran into an issue with the antd Select component. Jest errors with ReferenceError: Select is not defined when running test runner with --coverage flag jest --coverage but all my tests pass fine when I run jest 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 on jest --coverage and not on jest.

18reactions
mydiemhocommented, Aug 27, 2019

@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 and jest --coverage should produce similar results.

Read more comments on GitHub >

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

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