c8
c8 copied to clipboard
Clarify config
- Version: 7.10.0
- Platform: macOS 12.1
- Node: 17.1
Configuration options for "include" and "src" are very unclear. Without specifying either, several extraneous files are included in the report and some are actually not included at all (despite their tests running).
package.json
{
"scripts": {
"test": "NODE_ENV=test NODE_OPTIONS='--no-warnings --loader=./loader.mjs' mocha './lib'",
"test:coverage": "c8 --check-coverage npm test"
},
"c8": {
"branches": 100,
"exclude": [
"**/*.fixture.js",
"**/*.spec.{js,cjs,mjs,ts,tsx,jsx}" // c8 is expecting .test.*
]
},
}
file directory
./lib
├ compose.js
├ compose.spec.js
├ diff.js
├ diff.spec.js
├ Form.jsx
├ Form.spec.js
c8 coverage report
// passing mocha spec output for compose.spec.js, diff.spec.js, and Form.spec.js
-----------------|---------|----------|---------|---------|------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------------|---------|----------|---------|---------|------------------
All files | 92.57 | 95.31 | 66.66 | 92.57 |
PACKAGE | 100 | 84.61 | 54.54 | 100 |
loader.mjs | 100 | 87.5 | 100 | 100 | 33
mocha.setup.js | 100 | 80 | 28.57 | 100 | 13
PACKAGE/lib | 88.94 | 98.03 | 85.71 | 88.94 |
<stdin> | 73.41 | 85.71 | 80 | 73.41 | 51-71
compose.js | 100 | 100 | 100 | 100 |
diff.js | 100 | 100 | 100 | 100 |
-----------------|---------|----------|---------|---------|------------------
Unexpectedly, ./loader.mjs
and ./mocha.setup.js
are included, yet Form.spec.js
is not.
Specifying anything for "include"
seems to cause the coverage report to be empty, and "src"
seems to have no affect whatsoever.
yeah I'm struggling to understand how I'm supposed to use --src
and --include
.
Specifying anything for "include" seems to cause the coverage report to be empty, and "src" seems to have no affect whatsoever.
That's exactly the behavior I found as well.
Okay I think I figured it out.
- In the report.js file we see that the cli arguments are passed directly to test-exclude
- In test-exclude docs it's said that the
include
andexclude
parameters should be an array of strings - Again in the report.js file we see that they're using a package called yargs to parse the cli arguments
- In yargs docs it's said that a parameter is converted to array when it's specified multiple times
So I was able to make it work specifying --include
multiple times. Example:
npx c8 --include "src/handlers/*/handler.ts" --include "src/stubs/*.ts" ava
The coverage report correctly shows the files I expected to see.
Hope it helps!