Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No coverage when running in watch mode #7331

Open
MurakamiKennzo opened this issue Nov 6, 2018 · 45 comments
Open

No coverage when running in watch mode #7331

MurakamiKennzo opened this issue Nov 6, 2018 · 45 comments

Comments

@MurakamiKennzo
Copy link

🐛 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 
@gabrielbs
Copy link
Contributor

also happens on ubuntu 18.04

@villelahdenvuo
Copy link

villelahdenvuo commented Nov 19, 2018

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

@SimenB
Copy link
Member

SimenB commented Nov 19, 2018

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

@villelahdenvuo
Copy link

@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.

@SimenB
Copy link
Member

SimenB commented Nov 19, 2018

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

@villelahdenvuo
Copy link

@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.

@SimenB
Copy link
Member

SimenB commented Nov 19, 2018

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

@MurakamiKennzo
Copy link
Author

MurakamiKennzo commented Nov 20, 2018

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

@rickhanlonii rickhanlonii changed the title watch and coverage bug? No coverage when running in watch mode Nov 21, 2018
@stipsan
Copy link
Contributor

stipsan commented Nov 23, 2018

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.

@coxdn
Copy link

coxdn commented Jan 25, 2019

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?

@GuillaumeAmat
Copy link

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 :)

@kmarple1
Copy link

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.

@miikaah
Copy link

miikaah commented May 7, 2019

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.

@abierbaum
Copy link

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.

@mguida22
Copy link

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.

@abierbaum
Copy link

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.

@ghost
Copy link

ghost commented Jul 17, 2019

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).

@EmilyRosina
Copy link

EmilyRosina commented Jul 24, 2019

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 👍

⚠️ 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 🤷‍♀️.
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.

@basickarl
Copy link

Any updates on this?

@jlenglain
Copy link

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
Copy link

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.

@EmilyRosina
Copy link

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 🤷‍♀️
Maybe try changing my command to --watchAll instead as abierbaum suggested and see what happens...

@EmilyRosina
Copy link

Seems like it's back to not working for me, unsure if a release broke it or something, no idea 🤷‍♀️.

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

@idmick
Copy link

idmick commented Dec 18, 2019

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

adamcaron added a commit to raft-tech/TANF-app that referenced this issue Jul 30, 2020
Add scripts for CI and for local code coverage
- test:ci for CI
- test:cov to see code coverage locally. Because running with coverage
  slows down the test suite, this was added as an optional script.
  The --watchAll flag was added to `test:cov` because there is an issue
  where coverage reports 0/0% when tests run in interactive mode.
  jestjs/jest#7331
  jest-community/vscode-jest#433

Have ESLint and Git ignore the coverage output

Add tests for the App component

Update README with unit test instructions
RafterGit pushed a commit to raft-tech/TANF-app that referenced this issue Aug 6, 2020
Add scripts for CI and for local code coverage
- test:ci for CI
- test:cov to see code coverage locally. Because running with coverage
  slows down the test suite, this was added as an optional script.
  The --watchAll flag was added to `test:cov` because there is an issue
  where coverage reports 0/0% when tests run in interactive mode.
  jestjs/jest#7331
  jest-community/vscode-jest#433

Have ESLint and Git ignore the coverage output

Add tests for the App component

Update README with unit test instructions
RafterGit pushed a commit to raft-tech/TANF-app that referenced this issue Aug 6, 2020
Add scripts for CI and for local code coverage
- test:ci for CI
- test:cov to see code coverage locally. Because running with coverage
  slows down the test suite, this was added as an optional script.
  The --watchAll flag was added to `test:cov` because there is an issue
  where coverage reports 0/0% when tests run in interactive mode.
  jestjs/jest#7331
  jest-community/vscode-jest#433

Have ESLint and Git ignore the coverage output

Add tests for the App component

Update README with unit test instructions
adamcaron added a commit to raft-tech/TANF-app that referenced this issue Aug 21, 2020
Add scripts for CI and for local code coverage
- test:ci for CI
- test:cov to see code coverage locally. Because running with coverage
  slows down the test suite, this was added as an optional script.
  The --watchAll flag was added to `test:cov` because there is an issue
  where coverage reports 0/0% when tests run in interactive mode.
  jestjs/jest#7331
  jest-community/vscode-jest#433

Have ESLint and Git ignore the coverage output

Add tests for the App component

Update README with unit test instructions
RafterGit pushed a commit to raft-tech/TANF-app that referenced this issue Aug 26, 2020
Add scripts for CI and for local code coverage
- test:ci for CI
- test:cov to see code coverage locally. Because running with coverage
  slows down the test suite, this was added as an optional script.
  The --watchAll flag was added to `test:cov` because there is an issue
  where coverage reports 0/0% when tests run in interactive mode.
  jestjs/jest#7331
  jest-community/vscode-jest#433

Have ESLint and Git ignore the coverage output

Add tests for the App component

Update README with unit test instructions
@blopez-dev
Copy link

In order to generate the coverage report, the test script must be: test --watchAll --collect-coverage

@hayzey
Copy link

hayzey commented Jun 17, 2021

All of these comments saying to use the --watchAll flag are missing the point. The issue is that some of us want to generate coverage without having to run the entire test suite. There are of course issues with this, namely that you wouldn't get an accurate picture of your app's test coverage if you're only generating coverage for changed files.

For example, let's say some files have been changed a few commits ago. You run your unit tests and generate coverage for changed files (i.e, using --watch and not --watchAll). The tests for those files won't be run because they haven't been changed since the last commit. Your coverage report will therefore be wrong.

Perhaps what would be useful is a flag to tell jest to generate coverage for files that have changed since the last time a coverage report was generated. Perhaps in this mode, jest could associate a coverage report with a commit hash. The next time you generate a coverage report, it will do a diff between your current HEAD and that older commit, and rerun tests for any files that have changed since then. If there is no coverage report at all, we run the entire test suite. The flag could be something like:

jest --watch --changedSinceLastCoverageReport

The name could probably be something better but you get the idea. This would fit my use case but I'm not sure about everyone else.

The motivation behind it for me is that some of our bigger test suites can take a long time to run. It's annoying to have to run all of your tests to check the coverage of the files you've been working on.

@EmilyRosina
Copy link

I don't prefer having to resort to --watchAll btw 😅 just so it's not misconstrued 😜.

I agree that we should just be able to watch specific file changes only for coverage. Alas, Jest don't allow for this.
So please don't assume I've missed the point, I totally get the point - I want it to work that way too!! 🙏

My comments are just to aid those that need coverage and watch to work for them given the current Jest api/options, that's all.

@reader00
Copy link

Previously I also had the problem of coverage not showing. So my folder structure was like this:

D:/some/path/[Submissions]/1/Submission 1

I've done various configurations as suggested above, as well as what I found on Stack Over Flow, but the coverage results are still not showing up.

After a day looking for a way and couldn't find it, I took the initiative to publish the repo to Github, then clone it to D:/, and Voillaaaaa. the coverage result appears.

Then out of curiosity, I cut the clone results to the Submission 1 folder. But the coverage result is gone again.

I did a few experiments, and it turns out that all the code in the [Submissions] folder does not display the coverage result. Then I remember that previously I changed the name of the folder from Submissions to [Submissions], which from that moment the coverage result never shows or is always empty.

After I changed it again to Submissions, everything works.

May this issue occur because of the directory that contains a square bracket([]).

@tomavic
Copy link

tomavic commented Apr 23, 2022

If it is that simple, by using --watchAll instead of --watch, why is this issue still opened?

@ice-blaze
Copy link

@tomavic You don't want to run all the tests every time you execute the tests for the first time. I guess it's fine if your tests suits executes in a few seconds. If it's more than 10 secondes it's start to be annoying.

@EmilyRosina
Copy link

@tomavic I personally don't care about coverage for all files when i'm working on just one or a select few. Thus why --watch only generating coverage for specific files you're working on, would be better than --watchAll taking ALL files in your codebase into account.

@tomavic
Copy link

tomavic commented Apr 24, 2022

@ice-blaze @EmilyRosina

Yes I see. Now I can understand why we should consider --watch over --watchAll

Although, it's weird that it is an opened issue since 2018!!

P.S

What make me came here at first place, I am migrating from Jasmine to Jest, I have 296 test case 😅

I've tried

Jest --coverage --watch

But it didn't generate the coverage.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Apr 24, 2023
@firebait
Copy link

Don't think this should be closed. The workflow of test coverage with --watchAll is pretty terrible. Is this prioritized?

@github-actions github-actions bot removed the Stale label May 18, 2023
@rafaelrozon
Copy link

Any updates on this? My team recently stopped using --watchAll because it was doing way too much stuff and then we lost the ability to quickly check coverage for changed/watched files. It would be great if coverage worked for both watch flags.

@ice-blaze
Copy link

@rafaelrozon As a workaround you can use --watch --test-path-pattern my-current-feature-folder which will run the tests only for a specific scope. It's not ideal because each time you have to specify where you want jest to run but it does the job. No need to wait minutes after each change.

Copy link

github-actions bot commented Oct 3, 2024

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 3, 2024
@villelahdenvuo
Copy link

Don't think this should be closed. The workflow of test coverage with --watchAll is pretty terrible. Is this prioritized?

@github-actions github-actions bot removed the Stale label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests