Skip to content

Commit 02489d2

Browse files
committed
feat: configure reporters, default includes json, close #49
1 parent 5fe76d0 commit 02489d2

File tree

5 files changed

+103
-20
lines changed

5 files changed

+103
-20
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage/
66
dist/
77
.cache/
88
.vscode/
9+
cypress-coverage/

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ You can specify custom report folder by adding `nyc` object to the `package.json
144144
}
145145
```
146146

147+
## Custom reporters
148+
149+
You can specify custom coverage reporter(s) to use. For example to output text summary and save JSON report in `cypress-coverage` folder set in your `package.json` folder:
150+
151+
```json
152+
{
153+
"nyc": {
154+
"report-dir": "cypress-coverage",
155+
"reporter": [
156+
"text",
157+
"json"
158+
]
159+
}
160+
}
161+
```
162+
163+
**Tip:** find list of reporters [here](https://istanbul.js.org/docs/advanced/alternative-reporters/)
164+
147165
## Exclude code
148166

149167
You can exclude parts of the code or entire files from the code coverage report. See [Istanbul guide](https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md). Common cases:

Diff for: package-lock.json

+56-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"private": false,
4545
"dependencies": {
4646
"@cypress/browserify-preprocessor": "2.1.1",
47+
"cp-file": "7.0.0",
4748
"debug": "4.1.1"
4849
},
4950
"devDependencies": {

Diff for: task.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@ const istanbul = require('istanbul-lib-coverage')
22
const { join } = require('path')
33
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
44
const execa = require('execa')
5-
const debug = require('debug')('code-coverage')
5+
const fs = require('fs')
66
const { fixSourcePathes } = require('./utils')
77

8+
const debug = require('debug')('code-coverage')
9+
810
// these are standard folder and file names used by NYC tools
911
const outputFolder = '.nyc_output'
1012
const coverageFolder = join(process.cwd(), outputFolder)
1113
const nycFilename = join(coverageFolder, 'out.json')
1214

15+
// there might be custom "nyc" options in the user package.json
16+
// see https://github.com/istanbuljs/nyc#configuring-nyc
17+
// potentially there might be "nyc" options in other configuration files
18+
// it allows, but for now ignore those options
19+
const pkgFilename = join(process.cwd(), 'package.json')
20+
const pkg = fs.existsSync(pkgFilename)
21+
? JSON.parse(fs.readFileSync(pkgFilename, 'utf8'))
22+
: {}
23+
const nycOptions = pkg.nyc || {}
24+
1325
function saveCoverage (coverage) {
1426
if (!existsSync(coverageFolder)) {
1527
mkdirSync(coverageFolder)
@@ -72,10 +84,21 @@ module.exports = {
7284
console.warn('Skipping coverage report')
7385
return null
7486
}
75-
const command = 'nyc'
76-
const args = ['report', '--reporter=lcov']
77-
debug('saving coverage report using command: %s %s', command, args)
87+
88+
const reportDir = nycOptions['report-dir'] || './coverage'
89+
const reporter = nycOptions['reporter'] || ['lcov', 'clover', 'json']
90+
const reporters = Array.isArray(reporter)
91+
? reporter.map(name => `--reporter=${name}`)
92+
: `--reporter=${reporter}`
93+
7894
// should we generate report via NYC module API?
95+
const command = 'nyc'
96+
const args = ['report', '--report-dir', reportDir].concat(reporters)
97+
debug(
98+
'saving coverage report using command: "%s %s"',
99+
command,
100+
args.join(' ')
101+
)
79102
return execa(command, args, { stdio: 'inherit' })
80103
}
81104
}

0 commit comments

Comments
 (0)