Skip to content

Commit 89317a4

Browse files
author
wdsmini
committed
test_runner: fix coverage ignore comments to exclude BRDA entries
When using /* node:coverage ignore next */ comments, the test_runner correctly excluded DA (line coverage) entries from lcov output, but still included BRDA (branch coverage) entries as uncovered. This caused branch coverage to report lower percentages than expected. This fix ensures that branches completely covered by ignore comments are excluded from the branch coverage report, matching the behavior of c8 and user expectations. Fixes: #61586
1 parent 9fc6b64 commit 89317a4

3 files changed

Lines changed: 71 additions & 10 deletions

File tree

lib/internal/test_runner/coverage.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,23 @@ class TestCoverage {
193193
ObjectAssign(range, mapRangeToLines(range, lines));
194194

195195
if (isBlockCoverage) {
196-
ArrayPrototypePush(branchReports, {
197-
__proto__: null,
198-
line: range.lines[0]?.line,
199-
count: range.count,
200-
});
201-
202-
if (range.count !== 0 ||
203-
range.ignoredLines === range.lines.length) {
196+
// Skip branches that are completely ignored
197+
if (range.ignoredLines === range.lines.length) {
198+
totalBranches++;
204199
branchesCovered++;
200+
} else {
201+
ArrayPrototypePush(branchReports, {
202+
__proto__: null,
203+
line: range.lines[0]?.line,
204+
count: range.count,
205+
});
206+
207+
if (range.count !== 0) {
208+
branchesCovered++;
209+
}
210+
211+
totalBranches++;
205212
}
206-
207-
totalBranches++;
208213
}
209214
}
210215

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Test fixture for issue #61586
2+
// Tests that node:coverage ignore next also excludes BRDA entries
3+
4+
function getValue(condition) {
5+
if (condition) {
6+
return 'truthy';
7+
}
8+
/* node:coverage ignore next */
9+
return 'falsy';
10+
}
11+
12+
// Call only the truthy branch
13+
getValue(true);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('node:assert');
4+
const { spawnSync } = require('node:child_process');
5+
const { test } = require('node:test');
6+
const fixtures = require('../common/fixtures');
7+
const skipIfNoInspector = {
8+
skip: !process.features.inspector ? 'inspector disabled' : false
9+
};
10+
11+
test('coverage ignore next excludes BRDA for ignored branches', skipIfNoInspector, async (t) => {
12+
const fixture = fixtures.path('test-runner', 'coverage-ignore-branch.js');
13+
const args = [
14+
'--experimental-test-coverage',
15+
'--test-reporter', 'lcov',
16+
fixture,
17+
];
18+
const result = spawnSync(process.execPath, args);
19+
const lcovOutput = result.stdout.toString();
20+
21+
// Parse the LCOV output
22+
const lines = lcovOutput.split('\n');
23+
const brdaLines = lines.filter(l => l.startsWith('BRDA:'));
24+
25+
// All branches should be covered (no uncovered branches)
26+
// The branch leading to the ignored code should be excluded
27+
const uncoveredBranches = brdaLines.filter(l => l.endsWith(',0'));
28+
29+
assert.strictEqual(uncoveredBranches.length, 0,
30+
`Expected no uncovered branches, but found: ${uncoveredBranches.join(', ')}`);
31+
32+
// Verify branch coverage is 100%
33+
const brfLine = lines.find(l => l.startsWith('BRF:'));
34+
const brhLine = lines.find(l => l.startsWith('BRH:'));
35+
assert(brfLine, 'Should have BRF line');
36+
assert(brhLine, 'Should have BRH line');
37+
38+
const brf = parseInt(brfLine.split(':')[1], 10);
39+
const brh = parseInt(brhLine.split(':')[1], 10);
40+
41+
assert.strictEqual(brf, brh,
42+
`Expected branch coverage to be 100% (${brh}/${brf}), but found ${brh}/${brf}`);
43+
});

0 commit comments

Comments
 (0)