From 24e597996d18cc21ef2f8423e5de454b508b7a8b Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 16 Apr 2018 10:22:40 -0700 Subject: [PATCH] feat: allow 0-line files to be ignored in coverage output (#808) --- index.js | 6 ++++-- lib/commands/report.js | 6 ++++++ lib/config-util.js | 6 ++++++ test/fixtures/cli/empty.js | 0 test/nyc-bin.js | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/cli/empty.js diff --git a/index.js b/index.js index 6370a12c2..3bf3fdaeb 100755 --- a/index.js +++ b/index.js @@ -451,8 +451,10 @@ NYC.prototype.report = function () { tree = libReport.summarizers.pkg(map) - this.reporter.forEach(function (_reporter) { - tree.visit(reports.create(_reporter), context) + this.reporter.forEach((_reporter) => { + tree.visit(reports.create(_reporter, { + skipEmpty: this.config.skipEmpty + }), context) }) if (this._showProcessTree) { diff --git a/lib/commands/report.js b/lib/commands/report.js index 8289980b9..ff89da3f8 100644 --- a/lib/commands/report.js +++ b/lib/commands/report.js @@ -29,6 +29,12 @@ exports.builder = function (yargs) { default: false, type: 'boolean' }) + .option('skip-empty', { + describe: 'don\'t show empty files (no lines of code) in report', + default: false, + type: 'boolean', + global: false + }) .example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage') } diff --git a/lib/config-util.js b/lib/config-util.js index f826a8490..170e1d701 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -215,6 +215,12 @@ Config.buildYargs = function (cwd) { default: './.nyc_output', global: false }) + .option('skip-empty', { + describe: 'don\'t show empty files (no lines of code) in report', + default: false, + type: 'boolean', + global: false + }) .pkgConf('nyc', cwd) .example('$0 npm test', 'instrument your tests with coverage') .example('$0 --require babel-core/register npm test', 'instrument your tests with coverage and transpile with Babel') diff --git a/test/fixtures/cli/empty.js b/test/fixtures/cli/empty.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/nyc-bin.js b/test/nyc-bin.js index 8e0ee4cfd..fb7752328 100644 --- a/test/nyc-bin.js +++ b/test/nyc-bin.js @@ -937,6 +937,41 @@ describe('the nyc cli', function () { }) }) }) + + describe('skip-empty', () => { + it('does not display 0-line files in coverage output', (done) => { + const args = [ + bin, + '--cache', 'false', + '--skip-empty', 'true', + process.execPath, './empty.js' + ] + + const proc = spawn(process.execPath, args, { + cwd: fixturesCLI, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.stdout.on('error', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + stdoutShouldEqual(stdout, ` + ----------|----------|----------|----------|----------|-------------------| + File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | + ----------|----------|----------|----------|----------|-------------------| + ----------|----------|----------|----------|----------|-------------------|`) + done() + }) + }) + }) }) function stdoutShouldEqual (stdout, expected) {