20

When using @vue/cli-plugin-unit-jest, I am receiving coverage reports each time I run my unit tests, regardless of whether I have the --coverage flag in the execution line or not. I do not want to receive coverage reports on all of my untested files. When searching for an answer online, there are numerous questions about how to turn that feature on, not turn it off. I can't find it in the documentation either.

How do you disable the Coverage on Untested Files feature in Jest?

kyle
  • 691
  • 1
  • 7
  • 17
Chasen Bettinger
  • 7,194
  • 2
  • 14
  • 30
  • you can specify a single file to run coverage on, i put my answer together with the information here: https://stackoverflow.com/a/66492413/769780 – worc Oct 04 '21 at 23:48

6 Answers6

15

Disabling coverage similar to enabling it, just prefix the pattern with an ! like so:

{
  "collectCoverageFrom": [
    "**/*.{js,jsx}",
    "!**/node_modules/**",
    "!**/folder-with-untested-files/**"
  ]
}

Or disable coverage all together with "collectCoverage": false. If that does not work, then you have this params overridden somewhere in your code.

Herman Starikov
  • 2,636
  • 15
  • 26
  • You can also ignore files/folders using [`coveragePathIgnorePatterns `](https://jestjs.io/docs/configuration#coveragepathignorepatterns-arraystring). – chipit24 Jul 09 '21 at 17:44
12

You can also suppress coverage from the command line. The package I'm working with provides a test script, and I was able to pass the collectCoverage option in as a flag. The relative path here works because my test runner is called by npm and that should set the working directory to the root of my project:

npm run test -- path/to/your.spec.js --collectCoverage=false

And the other way around, you can specific a single file to collect coverage from. It'll override any broad-ranging glob you may have already defined in your project's test config files. One reminder, you collect coverage from your source file, not your spec file. And one other reminder, you can list pretty much any file you want in that coverage option, so make sure you get it right:

npm run test -- path/to/your.spec.js --collectCoverageFrom=path/to/your/source/file.js

worc
  • 3,654
  • 3
  • 31
  • 35
8
"collectCoverage": false

in jest.config.js

Irteza Asad
  • 1,145
  • 12
  • 4
  • 5
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Igor F. Mar 10 '20 at 12:38
3

Package.json

testw": "jest --watch --collectCoverage=false"

watches the test files for change

npm command

npm run testw Yourfilename.js
Ashish Singh Rawat
  • 1,419
  • 16
  • 30
  • It can also be helpful to include `--coverageThreshold '{}'`, to ignore the thresholds you have set in your jest config file. Otherwise, you'll see a bunch of errors that your coverage is very low because `watch` only collects coverage on a handful of files. – blwinters Mar 16 '23 at 02:05
2
"collectCoverage": false

in package.json, will disable coverage, collection,

As mentioned by @Herman

you can also put ! before file pattern in value of property
collectCoverageFrom in package.json

Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
0

In my case, in package.json I have this statement collectCoverage:false and still I was getting errors. Then I realized I also have collectCoverageFrom line and I removed it since I did not need it. After removing the below line it worked as a charm.

"collectCoverageFrom": [
 ...,
 ...
]
chety
  • 136
  • 4