Jest watch with code coverage not working

Viewed 2533

I'm trying to run jest --coverage --watch so that I only get the coverage for whatever component I'm writing tests for and I can see the increased coverage as I write more tests. But while the modified tests are successfully picked up and run, the coverage report comes up empty.

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|


Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        5.599s
Ran all test suites related to changed files.

The documentation on this is almost zero, but it seems like it should work according to this pr: https://github.com/facebook/jest/pull/5601

2 Answers

Here is a solution Create a jest.config.js and add the following content

  • jest.config.js
module.exports = {
    collectCoverage: true,
    coverageReporters: [
        "html"
    ]
};

And in package.json:

"scripts": {
    "test": "jest --watchAll"
}

I would you use jest --watchAll --coverage rather then set it in a config file because you may not want to collect coverage every time you run jest. You can add it as a run script or a bash alias as well if you use it frequently.

Related