Skip to content

Commit 8b75237

Browse files
authored
fix(coverage): handle empty branch locations in lcov generation (#44114)
## Summary - Use optional chaining when accessing `location.start?.line` in `CoverageReporter.lcovReport` to prevent `TypeError` when Istanbul coverage data contains empty branch locations. - Fallback safely to `branch.loc?.start?.line ?? branch.line ?? 0`. Fixes #44113
1 parent 354e6fa commit 8b75237

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎packages/playwright/src/reporters/coverage.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function lcovReport(mergedJson: IstanbulCoverage): string {
191191
const counts: number[] = fileCov.b[key] || [];
192192
branch.locations.forEach((location, i) => {
193193
const count = counts[i] || 0;
194-
out.push(`BRDA:${location.start.line ?? branch.loc.start.line},${key},${i},${count}`);
194+
out.push(`BRDA:${location.start?.line ?? branch.loc?.start?.line ?? branch.line ?? 0},${key},${i},${count}`);
195195
++branchesFound;
196196
if (count > 0)
197197
++branchesCovered;

‎tests/playwright-test/coverage.spec.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ const coverageHelper = `
3030
'0': { name: 'foo', decl: { start: { line: 1, column: 0 }, end: { line: 1, column: 3 } }, loc: { start: { line: 1, column: 0 }, end: { line: 3, column: 1 } }, line: 1 },
3131
},
3232
branchMap: {
33-
'0': { loc: { start: { line: 2, column: 0 }, end: { line: 2, column: 20 } }, type: 'if', locations: [{ start: { line: 2, column: 0 }, end: { line: 2, column: 10 } }, { start: { line: 2, column: 10 }, end: { line: 2, column: 20 } }], line: 2 },
33+
'0': { loc: { start: { line: 2, column: 0 }, end: { line: 2, column: 20 } }, type: 'if', locations: [{ start: { line: 2, column: 0 }, end: { line: 2, column: 10 } }, {} as any], line: 2 },
34+
'1': { type: 'if', locations: [{} as any], line: 3 },
3435
},
3536
s: { '0': s0, '1': 0 },
3637
f: { '0': 1 },
37-
b: { '0': [1, 0] },
38+
b: { '0': [1, 0], '1': [1] },
3839
},
3940
});
4041
const coverageScript = (file, s0) => '<script>window.__coverage__ = ' + JSON.stringify(makeCoverage(file, s0)) + '</script>';
@@ -79,10 +80,13 @@ test('should collect istanbul coverage into the trace', async ({ runInlineTest }
7980
expect(data['src/app.js'].s['1']).toBe(0);
8081
expect(data['src/app.js'].f['0']).toBe(2);
8182
expect(data['src/app.js'].b['0']).toEqual([2, 0]);
83+
expect(data['src/app.js'].b['1']).toEqual([2]);
8284
expect(data['src/frame.js'].s['0']).toBe(3);
8385
});
8486

85-
test('should aggregate coverage with the coverage reporter', async ({ runInlineTest }) => {
87+
test('should aggregate coverage with the coverage reporter', {
88+
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/44113' },
89+
}, async ({ runInlineTest }) => {
8690
const result = await runInlineTest({
8791
'playwright.config.ts': `
8892
export default {
@@ -117,12 +121,15 @@ test('should aggregate coverage with the coverage reporter', async ({ runInlineT
117121
expect(final['src/app.js'].s['1']).toBe(0);
118122
expect(final['src/app.js'].f['0']).toBe(2);
119123
expect(final['src/app.js'].b['0']).toEqual([2, 0]);
124+
expect(final['src/app.js'].b['1']).toEqual([2]);
120125
expect(final['src/second.js'].s['0']).toBe(4);
121126

122127
const lcov = fs.readFileSync(test.info().outputPath('coverage-report', 'lcov.info'), 'utf8');
123128
expect(lcov).toContain('SF:src/app.js');
124129
expect(lcov).toContain('DA:1,7');
125130
expect(lcov).toContain('FNDA:2,foo');
131+
expect(lcov).toContain('BRDA:2,0,1,0');
132+
expect(lcov).toContain('BRDA:3,1,0,2');
126133
expect(lcov).toContain('SF:src/second.js');
127134

128135
expect(result.output).toContain('Code coverage (2 files)');

0 commit comments

Comments
 (0)