playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: CoverageReporter throws TypeError on empty branch locations in lcov generation

Open halcyon-past opened this issue 2 weeks ago • 3 comments

Version

1.51.0 (main)

Steps to reproduce

  1. Enable trace with _coverage: true and configure the coverage reporter in playwright.config.ts.
  2. Instrument code that contains an if statement without an else branch (or any branch structure where Istanbul leaves implicit branch locations empty {}).
  3. Run test with @playwright/test.

Expected behavior

CoverageReporter should safely read the line number and generate lcov.info without crashing.

Actual behavior

CoverageReporter crashes with:

TypeError: Cannot read properties of undefined (reading 'line')

When an implicit branch has an empty location object {} in Istanbul's branch.locations, accessing location.start.line throws because location.start is undefined.

Additional context

I have reproduced the issue and have a local fix ready with regression tests. Please assign this to me so I can open the pull request.

Environment

System: OS: macOS 26.5.2 CPU: (8) arm64 Apple M3 Binaries: Node: 26.10.0 - /Users/aritrosaha/.nvm/versions/node/v26.10.0/bin/node npm: 11.19.1 - /Users/aritrosaha/.nvm/versions/node/v26.10.0/bin/npm

halcyon-past avatar Nov 14 '26 02:11 halcyon-past

I have submitted a pull request with the fix and regression coverage in #44114.

halcyon-past avatar Nov 14 '26 02:11 halcyon-past

Hi, I'm the Playwright bot. I started triaging this issue on macOS: agent run

Hi, I'm the Playwright bot and I reproduced this on macOS.

Reproduced on current main (826ce42b8664ea940424e77ce51f1cdf96dcd37f, 1.66.0-next) in Chromium, Firefox, and WebKit. The reporter reads location.start.line before it can use the enclosing branch's line number, so an empty location throws.

#44114 fixes the crash at beb4d939a4d57cb2416863383d3876fc4f130011. My independent reproduction passes with that fix on all three browsers. Its regression test also fails without the source change and passes with it. No separate fix is needed.

Reproduction and checks

I extended should aggregate coverage with the coverage reporter in tests/playwright-test/coverage.spec.ts, removing start and end from an existing branch location before serializing the coverage data. The test checks that lcov.info contains BRDA:2,0,1,0.

Minimal equivalent configuration:

export default {
  reporter: 'coverage',
  use: { trace: { mode: 'on', _coverage: true } },
};

Test:

import { test } from '@playwright/test';

test('empty branch location', async ({ page }) => {
  const loc = {
    start: { line: 2, column: 0 },
    end: { line: 2, column: 20 },
  };
  const coverage = {
    'app.js': {
      path: 'app.js',
      statementMap: { '0': loc },
      fnMap: {},
      branchMap: {
        '0': { type: 'if', line: 2, loc, locations: [loc, {}] },
      },
      s: { '0': 1 },
      f: {},
      b: { '0': [1, 0] },
    },
  };
  await page.setContent(
    '<script>window.__coverage__ = ' + JSON.stringify(coverage) + '</script>'
  );
});
Check Result
Independent reproduction on main, Chromium 159.0.8154.6, Firefox 159.0, WebKit 27.2 (r2394), headless Same TypeError in all three
Independent reproduction on #44114, same Chromium and Firefox, WebKit 27.2 (r2393), headless Passes in all three
PR's regression test applied to main without its source fix Fails with the reported TypeError
PR's complete coverage-reporter test file 3 pass per browser
Related coverage-collection tests, tests/library/coverage.spec.ts 39 pass across all three browsers
Neighboring reporter lifecycle and attachment tests 19 pass; browser-independent
npm run build, then npm run flint on the PR Pass
Merge with current main No conflicts

I used Node.js 24.22.2. I did not test the reported 1.51.0 or headed mode because the exact crash reproduces on current main. Initial setup attempts failed because dependencies and browser binaries were absent; those passed after installing the pinned dependencies and browsers.

The PR's only reported GitHub check is the successful CLA check; no test CI jobs have run. GitHub reports its merge state as blocked, so this local verification does not bypass the repository's merge requirements.