jest icon indicating copy to clipboard operation
jest copied to clipboard

No coverage when running in watch mode

Open MurakamiKennzo opened this issue 7 years ago • 47 comments
trafficstars

🐛 Bug Report

when I run jest --watch, I see unknown coverage.

To Reproduce

see Link repo.

Expected behavior

show correct coverage.

Link to repl or repo (highly encouraged)

jest-watch-coverage-bug

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS 10.14
    CPU: x64 Intel(R) Core(TM) m3-6Y30 CPU @ 0.90GHz
  Binaries:
    Node: 9.10.1 - /usr/local/bin/node
    npm: 6.4.1 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0 

MurakamiKennzo avatar Nov 06 '18 00:11 MurakamiKennzo

also happens on ubuntu 18.04

gabrielbs avatar Nov 06 '18 15:11 gabrielbs

Also hitting this issue even though there are changed tests that ran:

 PASS  src/app/product/reducers/product.reducer.spec.ts (6.041s)
 PASS  src/app/shared/services/resolve-store.service.spec.ts (6.706s)

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

Test Suites: 2 passed, 2 total
Tests:       37 passed, 37 total
Snapshots:   0 total
Time:        10.329s
Ran all test suites related to changed files.
  System:
    OS: macOS 10.14.2
    CPU: (4) x64 Intel(R) Core(TM) i5-6360U CPU @ 2.00GHz
  Binaries:
    Node: 11.1.0 - ~/n/bin/node
    Yarn: 1.7.0 - ~/n/bin/yarn
    npm: 6.4.1 - ~/n/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0

villelahdenvuo avatar Nov 19 '18 09:11 villelahdenvuo

@MurakamiKennzo what do you expect "correct coverage" to be? All 0? Unless tests actually execute, we have no way of knowing the coverage

SimenB avatar Nov 19 '18 10:11 SimenB

@SimenB I updated my comment above to be more clear. Tests are ran and coverage works when all tests are ran, but in watch more coverage is not collected even for the files that are affected.

villelahdenvuo avatar Nov 19 '18 12:11 villelahdenvuo

@villelahdenvuo that's a separate issue from the OP, please include a reproduction (maybe in a new issue)

SimenB avatar Nov 19 '18 12:11 SimenB

@SimenB How is it different from the OP? According to the example repo included in the OP the issue appears in watch mode after adding a new test. That is my case as well, at first I had no coverage as expected, but then I added a new tests to those two files shown and it still says coverage Unknown.

villelahdenvuo avatar Nov 19 '18 12:11 villelahdenvuo

Ah, sorry, I missed the step editing the test while watch mode was running.

@stipsan thoughts on how to fix this? You're my goto "coverage in watch mode" guy :D

SimenB avatar Nov 19 '18 12:11 SimenB

I agree what @villelahdenvuo said. please follow the step and you will understand what i mean.

MurakamiKennzo avatar Nov 20 '18 02:11 MurakamiKennzo

I'll have to look into it more but on top of my head it sounds like the problem is related to the collectCoverageFrom patterns that are added in the runner: https://github.com/facebook/jest/blob/c01b4c75a2f65e9fa122adf69b0ac11875f1fc74/packages/jest-cli/src/runJest.js#L163-L188

So my theory is that it's generating a too exclusive pattern on the first run that is persisted through the second run with the added test.

stipsan avatar Nov 23 '18 08:11 stipsan

I have the same problem. When I delete a folder .git and reinit it (git init), then watch mode displays normally coverage. On repo from topic starter it works too. Demo In my case problem manifests on the project a bit larger and wouldn't want to hide this folder every time when I starting npm run test:watch. Maybe there are some workarounds?

coxdn avatar Jan 25 '19 08:01 coxdn

Same issue here. When I launch Jest with jest --coverage --watch, without any modified files, the coverage is Unknown% ( 0/0 ).

If I press a, all the tests run, I can see each fail/success state, but the coverage remains Unknown% ( 0/0 ).

If I edit a file, the tests run and again, the coverage is Unknown% ( 0/0 ).

But! If I hit o, the tests run and I see the right coverage! I can then hit a or whatever and all is good :)

GuillaumeAmat avatar Mar 23 '19 18:03 GuillaumeAmat

I'm getting the same bug, or a variant of it. When I first run npm run test, code coverage for changed files will be correct. However, if I press a or modify additional files, the appropriate tests will be run, but the files won't be added to the coverage report.

kmarple1 avatar Mar 28 '19 17:03 kmarple1

It seems like jest doesn't really respect collectCoverageFrom in watch mode and instead overrides it with files that have changed. If there are no changes, no code coverage is gathered, even if new tests have stepped over new lines. It's a major drag when one has to write tests to a codebase afterwards.

As a workaround I found that you can use forceCoverageMatch to gather coverage even in watch mode.

miikaah avatar May 07 '19 14:05 miikaah

Recently converted to Jest, and this is the only thing missing from my old Karma / jasmine setup. Not having coverage is a pain point for interactive test development. Being able to watch the coverage and gradually increase it through additional tests and IDE extensions that show coverage inline is a huge win if it could work like it does in Karma.

abierbaum avatar May 09 '19 12:05 abierbaum

It'd be great to have this, is there any update? Is anyone working on a fix?

With a little guidance I'd be happy to contribute to a solution for this.

mguida22 avatar Jul 12 '19 21:07 mguida22

I was able to get everything working fine by simply starting jest with watchAll. Then if I want to see the coverage for a single test I just use 'p' to match the pattern of the file name. Works great. Just confusing to know to use watchAll.

abierbaum avatar Jul 13 '19 13:07 abierbaum

For those who may actually have a different problem:

I fixed mine by simply changing rootDir in package.json to my project directory. It wasn't checking my js files since the default value of rootDir is where jest's config file is (inside test folder in my case).

ghost avatar Jul 17 '19 06:07 ghost

Just in case this helps anyone else who does still want to run tests using --watch and compile the code coverage with --coverage...

i.e.

--watch --coverage

I tried moving the unwanted glob file patterns from collectCoverageFrom to coveragePathIgnorePatterns in the jest config file. Needed a bit of tweaking but I can now see coverage without any changes to the npm script :+1:

:warning: collectCoverageFrom uses globs, whereas coveragePathIgnorePatterns uses regex.

before

  collectCoverageFrom: [
    'src/**/*.{js,vue}',
    '!src/main.js',
    '!**/node_modules/**',
    '!src/vendor/**/*'
  ]

after

  collectCoverageFrom: [
    'src/**/*.{js,vue}'
  ],
  coveragePathIgnorePatterns: [
    'src/main.js',
    '/node_modules/',
    'src/vendor/'
  ]

This should probably be in the Jest docs somewhere due to some change that is causing the problem but :woman_shrugging:. I haven't managed to find anything about it, nor a way to only show the coverage for just the 1 suite I am working on/or just files changed. I had also previously tried adding forceCoverageMatch, but this caused all test scripts to collect coverage, which was not desired. Our rootDir was already provided in the config file too, so wasn't that for us either - but thank you for the idea :)


In addition, if this helps anyone on linux: we use Docker - and sometimes it makes root take ownership of the coverage directory and files. So i periodically run

sudo chown -R user:group test/unit/coverage

swap out user and group with your own information.

EmilyRosina avatar Jul 24 '19 11:07 EmilyRosina

Any updates on this?

basickarl avatar Oct 03 '19 08:10 basickarl

It seems like jest doesn't really respect collectCoverageFrom in watch mode and instead overrides it with files that have changed. If there are no changes, no code coverage is gathered, even if new tests have stepped over new lines. It's a major drag when one has to write tests to a codebase afterwards.

As a workaround I found that you can use forceCoverageMatch to gather coverage even in watch mode.

But this returns coverage for all files for every file change.

jlenglain avatar Oct 18 '19 08:10 jlenglain

I was able to get everything working fine by simply starting jest with watchAll. Then if I want to see the coverage for a single test I just use 'p' to match the pattern of the file name. Works great. Just confusing to know to use watchAll.

Can you outline the exact order of the commands you're typing in order to get this to work? I tried your workaround but I get the same result: unknown coverage report.

jlenglain avatar Oct 18 '19 08:10 jlenglain

I was able to get everything working fine by simply starting jest with watchAll. Then if I want to see the coverage for a single test I just use 'p' to match the pattern of the file name. Works great. Just confusing to know to use watchAll.

yeah.. but i guess that's the limitation with jest, watch and coverage :woman_shrugging: Maybe try changing my command to --watchAll instead as abierbaum suggested and see what happens...

EmilyRosina avatar Oct 18 '19 11:10 EmilyRosina

Seems like it's back to not working for me, unsure if a release broke it or something, no idea :woman_shrugging:.

I can confirm what others say though --watch doesn't work, --watchAll does. It's a pita, but at least it works.

EmilyRosina avatar Nov 15 '19 17:11 EmilyRosina

I'm experiencing the same behavior, --watchAll shows coverage, --watch doesn't :(

idmick avatar Dec 18 '19 09:12 idmick

I'm not seeing code coverage calculated correctly at all, regardless of watch mode or not. I have created another issue for this problem here.

JimLynchCodes avatar Dec 23 '19 13:12 JimLynchCodes

my problem was the coverage didn't appear after commit test files. in my case, this bug fixed using this command: yarn test --coverage --watchAll

ammoradi avatar Dec 25 '19 17:12 ammoradi

My issue has been fixed, and it has to do with the config settings working only in jest.config.js but not under "jest" in package.json. When it works as expected jest it pretty nice! 😄

JimLynchCodes avatar Dec 27 '19 17:12 JimLynchCodes

In documentation,

jest --watch #runs jest -o by default
jest --watchAll #runs all tests

jest -o : Run tests related to changed files based on hg/git (uncommitted files)

yomybaby avatar Jan 03 '20 09:01 yomybaby

I finally got a working configuration.

When the configuration is set in the package.json it won't work. Therefore, I had to create jest.config.js file with the following contents:

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

And in package.json:

"scripts": {
    "test": "jest --watchAll" // It doesn't work with --watch
}

Abadii avatar Mar 16 '20 13:03 Abadii

Any updates on this ? I am still not getting coverage when combining --collectCoverage and --watch

ps011 avatar May 13 '20 10:05 ps011