简体   繁体   中英

Disable Coverage on Untested Files - Jest

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?

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.

"collectCoverage": false

jest.config.js 中

Package.json

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

watches the test files for change

npm command

npm run testw Yourfilename.js
"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

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

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": [
 ...,
 ...
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM