jest
jest copied to clipboard
No coverage when running in watch mode
🐛 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)
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
also happens on ubuntu 18.04
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
@MurakamiKennzo what do you expect "correct coverage" to be? All 0? Unless tests actually execute, we have no way of knowing the coverage
@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 that's a separate issue from the OP, please include a reproduction (maybe in a new issue)
@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.
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
I agree what @villelahdenvuo said. please follow the step and you will understand what i mean.
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.
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.
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?
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 :)
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.
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.
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.
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.
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.
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).
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:
collectCoverageFromuses globs, whereascoveragePathIgnorePatternsuses 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.
Any updates on this?
It seems like jest doesn't really respect
collectCoverageFromin 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
forceCoverageMatchto gather coverage even in watch mode.
But this returns coverage for all files for every file change.
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.
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...
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.
I'm experiencing the same behavior, --watchAll shows coverage, --watch doesn't :(
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.
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
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! 😄
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)
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
}
Any updates on this ? I am still not getting coverage when combining --collectCoverage and --watch