diff --git a/.gitignore b/.gitignore index af4049f46..8bd04cdad 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ coverage/ dist/ .cache/ .vscode/ +cypress-coverage/ diff --git a/README.md b/README.md index 7dcf84993..1c11cf26e 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,36 @@ if (global.__coverage__) { That should be enough - the code coverage from the server will be requested at the end of the test run and merged with the client-side code coverage, producing a combined report +## Custom report folder + +You can specify custom report folder by adding `nyc` object to the `package.json` file. For example to save reports to `cypress-coverage` folder, use: + +```json +{ + "nyc": { + "report-dir": "cypress-coverage" + } +} +``` + +## Custom reporters + +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: + +```json +{ + "nyc": { + "report-dir": "cypress-coverage", + "reporter": [ + "text", + "json" + ] + } +} +``` + +**Tip:** find list of reporters [here](https://istanbul.js.org/docs/advanced/alternative-reporters/) + ## Exclude code 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 --git a/package-lock.json b/package-lock.json index 0a33797cb..923d736e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3258,27 +3258,6 @@ "parse-json": "^4.0.0" } }, - "cp-file": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/cp-file/-/cp-file-6.2.0.tgz", - "integrity": "sha512-fmvV4caBnofhPe8kOcitBwSn2f39QLjnAnGq3gO9dfd75mUytzKNZB1hde6QHunW2Rt+OwuBOMc3i1tNElbszA==", - "dev": true, - "requires": { - "graceful-fs": "^4.1.2", - "make-dir": "^2.0.0", - "nested-error-stacks": "^2.0.0", - "pify": "^4.0.1", - "safe-buffer": "^5.0.1" - }, - "dependencies": { - "pify": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", - "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==", - "dev": true - } - } - }, "create-ecdh": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.3.tgz", @@ -10897,6 +10876,27 @@ "uuid": "^3.3.2", "yargs": "^13.2.2", "yargs-parser": "^13.0.0" + }, + "dependencies": { + "cp-file": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/cp-file/-/cp-file-6.2.0.tgz", + "integrity": "sha512-fmvV4caBnofhPe8kOcitBwSn2f39QLjnAnGq3gO9dfd75mUytzKNZB1hde6QHunW2Rt+OwuBOMc3i1tNElbszA==", + "dev": true, + "requires": { + "graceful-fs": "^4.1.2", + "make-dir": "^2.0.0", + "nested-error-stacks": "^2.0.0", + "pify": "^4.0.1", + "safe-buffer": "^5.0.1" + } + }, + "pify": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==", + "dev": true + } } }, "oauth-sign": { diff --git a/task.js b/task.js index b6b79d851..be1aa3ba7 100644 --- a/task.js +++ b/task.js @@ -2,14 +2,26 @@ const istanbul = require('istanbul-lib-coverage') const { join } = require('path') const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs') const execa = require('execa') -const debug = require('debug')('code-coverage') +const fs = require('fs') const { fixSourcePathes } = require('./utils') +const debug = require('debug')('code-coverage') + // these are standard folder and file names used by NYC tools const outputFolder = '.nyc_output' const coverageFolder = join(process.cwd(), outputFolder) const nycFilename = join(coverageFolder, 'out.json') +// there might be custom "nyc" options in the user package.json +// see https://github.com/istanbuljs/nyc#configuring-nyc +// potentially there might be "nyc" options in other configuration files +// it allows, but for now ignore those options +const pkgFilename = join(process.cwd(), 'package.json') +const pkg = fs.existsSync(pkgFilename) + ? JSON.parse(fs.readFileSync(pkgFilename, 'utf8')) + : {} +const nycOptions = pkg.nyc || {} + function saveCoverage (coverage) { if (!existsSync(coverageFolder)) { mkdirSync(coverageFolder) @@ -72,10 +84,21 @@ module.exports = { console.warn('Skipping coverage report') return null } - const command = 'nyc' - const args = ['report', '--reporter=lcov'] - debug('saving coverage report using command: %s %s', command, args) + + const reportDir = nycOptions['report-dir'] || './coverage' + const reporter = nycOptions['reporter'] || ['lcov', 'clover', 'json'] + const reporters = Array.isArray(reporter) + ? reporter.map(name => `--reporter=${name}`) + : `--reporter=${reporter}` + // should we generate report via NYC module API? + const command = 'nyc' + const args = ['report', '--report-dir', reportDir].concat(reporters) + debug( + 'saving coverage report using command: "%s %s"', + command, + args.join(' ') + ) return execa(command, args, { stdio: 'inherit' }) } }