Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve console coverage report readability #871

Merged
merged 4 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions lib/reporters/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,53 @@ internals.Reporter.prototype.end = function (notebook) {

const line = file.source[lineNumber];
if (line.miss) {
missingLines.push(lineNumber);
missingLines.push(parseInt(lineNumber, 10));
}
});

if (missingLines.length) {
output += red('\n' + file.filename + ' missing coverage on line(s): ' + missingLines.join(', '));
const lineReportLimit = 8;

// Lines missing coverage are reported as a list of
// spans, e.g. "1, 3-8, 10, 13-15".
const missingLinesReport = [];
const span = {
start: missingLines[0],
end: missingLines[0]
};
for (let i = 1; i <= missingLines.length; ++i) {
const line = missingLines[i];
if (line === span.end + 1) {
// Extend the current span.
span.end = line;
}
else {
// Flush current span to output.
if (span.start === span.end) {
missingLinesReport.push(span.start);
}
else if (span.start + 1 === span.end) {
missingLinesReport.push(span.start);
missingLinesReport.push(span.end);
}
else {
missingLinesReport.push(span.start + '-' + span.end);
}

// Start a new span.
span.start = line;
span.end = line;
}

// If the report gets too long, truncate it.
if (missingLinesReport.length >= lineReportLimit) {
const remainingLines = missingLines.length - i;
missingLinesReport.push('and ' + remainingLines + ' more');
break;
}
}

output += yellow('\n' + file.filename + ' missing coverage on line(s): ' + missingLinesReport.join(', '));
}
}
});
Expand Down
69 changes: 69 additions & 0 deletions test/coverage/console-large-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

// Load modules


// Declare internals

const internals = {};


exports.method = function (value) {

if (value) {
value += 1;
}
else {
value -= 1;
}

if (value) {
value += 1;
value += 1;
}
else {
value -= 1;
value -= 1;
}

if (value) {
value += 1;
value += 1;
value += 1;
}
else {
value -= 1;
value -= 1;
value -= 1;
}

if (value) {
value += 1;
value += 1;
value += 1;
value += 1;
}
else {
value -= 1;
value -= 1;
value -= 1;
value -= 1;
}

if (value) {
value += 1;
value += 1;
value += 1;
value += 1;
value += 1;
}
else {
value -= 1;
value -= 1;
value -= 1;
value -= 1;
value -= 1;
}

return value;
};
4 changes: 3 additions & 1 deletion test/coverage/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ exports.method = function (a, b, c) {
}
else if (c > 10) {
d += 1;
d += 1;
}

return d + (a || b || c);
const e = d ? a : b;
return d + (a || b || c) - e;
};
11 changes: 7 additions & 4 deletions test/reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,15 +829,17 @@ describe('Reporter', () => {

it('generates a coverage report (verbose)', async () => {

const Test = require('./coverage/console');
const Test1 = require('./coverage/console');
const Test2 = require('./coverage/console-large-file');
const Full = require('./coverage/console-full');

const script = Lab.script();
script.experiment('test', () => {

script.test('something', () => {

Test.method(1, 2, 3);
Test1.method(1, 2, 3);
Test2.method(1);
Full.method(1);

});
Expand All @@ -849,8 +851,9 @@ describe('Reporter', () => {
});

const { output } = await Lab.report(script, { reporter: 'console', coverage: true, coveragePath: Path.join(__dirname, './coverage/console'), output: false });
expect(output).to.contain('Coverage: 76.47% (4/17)');
expect(output).to.contain('test/coverage/console.js missing coverage on line(s): 14, 17, 18, 21');
expect(output).to.contain('Coverage: 64.86% (26/74)');
expect(output).to.contain('test/coverage/console.js missing coverage on line(s): 14, 17-19, 22, 23');
expect(output).to.contain('test/coverage/console-large-file.js missing coverage on line(s): 13, 17, 20, 25, 26, 29, 35-37, 40, and 10 more');
expect(output).to.not.contain('console-full');
});

Expand Down