Skip to content
Open
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
29 changes: 26 additions & 3 deletions lib/internal/test_runner/reporter/lcov.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,38 @@ class LcovReporter extends Transform {
// Taken is either '-' if the basic block containing the branch was
// never executed or a number indicating how often that branch was
// taken.
let branchCount = 0;
let coveredBranchCount = 0;

for (let j = 0; j < file.branches.length; j++) {
lcov += `BRDA:${file.branches[j].line},${j},0,${file.branches[j].count}\n`;
const branch = file.branches[j];
let lineReported = false;

for (let k = 0; k < file.lines.length; k++) {
if (file.lines[k].line === branch.line) {
lineReported = true;
break;
}
}

if (!lineReported) {
continue;
}

lcov += `BRDA:${branch.line},${branchCount},0,${branch.count}\n`;

branchCount++;

if (branch.count !== 0) {
coveredBranchCount++;
}
}

// Branch coverage summaries are stored in two lines:
// ## BRF:\<number of branches found\>
// ## BRH:\<number of branches hit\>
lcov += `BRF:${file.totalBranchCount}\n`;
lcov += `BRH:${file.coveredBranchCount}\n`;
lcov += `BRF:${branchCount}\n`;
lcov += `BRH:${coveredBranchCount}\n`;

// Then there is a list of execution counts for each instrumented line
// (i.e. a line which resulted in executable code):
Expand Down
39 changes: 38 additions & 1 deletion test/parallel/test-runner-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common');
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const { readdirSync } = require('node:fs');
const { readdirSync, writeFileSync } = require('node:fs');
const { test } = require('node:test');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
Expand Down Expand Up @@ -77,6 +77,43 @@ function getSpecCoverageFixtureReport() {
return report;
}

test('lcov reporter excludes BRDA entries for ignored lines', skipIfNoInspector, () => {
const fixture = tmpdir.resolve('lcov-ignore-branch.test.js');

writeFileSync(fixture, `
'use strict';

const test = require('node:test');

test('ignored branch', () => {
// node:coverage ignore next
if (false) {
throw new Error('ignored');
}

if (true) {
// Covered branch to ensure LCOV still reports branch data.
}
});
`);

const child = spawnSync(process.execPath, [
'--test',
'--experimental-test-coverage',
'--test-reporter',
'lcov',
fixture,
]);

assert.strictEqual(child.stderr.toString(), '');
assert.strictEqual(child.status, 0);

const stdout = child.stdout.toString();

assert(!stdout.includes('BRDA:8,'));
assert.match(stdout, /BRDA:/);
});

test('test coverage report', async (t) => {
await t.test('handles the inspector not being available', (t) => {
if (process.features.inspector) {
Expand Down
Loading