SupercovBlog

Guide

Playwright code coverage of your server: Express, measured

Two Playwright tests against an Express app, and the three settings c8 needed before it reported a single server line.

Playwright clicked. Did the server's code run? A brushed-aluminum server unit with five white indicator tiles, one violet.

Playwright’s coverage API measures JavaScript in the browser, in Chromium. Your server’s code, the Express routes the tests call, needs a coverage tool around the server process, and that tool only writes its data when the server exits normally. By default Playwright stops the webServer with a signal, so c8 reported nothing in our runs until we set gracefulShutdown and added a SIGTERM handler that calls process.exit().

A diagram: playwright test starts the webServer, node src/server.js. When it is stopped by a signal, Node writes no coverage. With process.exit() on SIGTERM, c8 reports the server. c8 needs gracefulShutdown SIGTERM in the config and a SIGTERM handler; Supercov needs neither and splits the server's coverage by test.
How the server's coverage gets written.

Supercov, which we make, measures the server under npx supercov -- npx playwright test with no change to the server or the config, and splits its coverage by Playwright test. See below.

At a glance

SetupServer coveragePer test
page.coverageNone: browser JavaScript only, Chromium onlyYes, if you start and stop it per test
c8 around the webServer commandNo reportNo
+ gracefulShutdown: { signal: 'SIGTERM' }A report with 0 of 0 linesNo
+ process.on('SIGTERM', () => process.exit(0))YesNo
npx supercov -- npx playwright testYes, with branches and MC/DCYes

We measured with Playwright 1.65.1, Express 5.2.1, c8 11.0.0 and Node 24: an Express app with a /api/price route and a discount function, a static page that calls it, and two Playwright tests.

A grid of four setups against server lines, per test and conditions. c8 around webServer: no report. Plus gracefulShutdown SIGTERM: 0 of 0 lines. Plus process.exit on SIGTERM: server lines, 21 of 25. supercov -- npx playwright test: server lines, per test and conditions, 14 of 17 lines.
What each setup reported for the Express server.

c8 around the webServer: three settings, all needed

Put c8 in front of the server command in playwright.config.js:

webServer: {
  command: 'c8 --reporter=text --include="src/**" node src/server.js',
  url: 'http://localhost:3100',
  gracefulShutdown: { signal: 'SIGTERM', timeout: 2000 },
},

Node writes V8 coverage when the process exits, not when a signal kills it.

  • Without gracefulShutdown, Playwright kills the server and c8 with it. No report was written.
  • With gracefulShutdown and SIGTERM, c8 reported, but 0 of 0 lines. The server still died from the signal, so its coverage file never appeared.
  • With a SIGTERM handler that exits, c8 reported the server: 21 of 25 lines, 6 of 8 branches. We loaded the handler with node --import so the app stayed unchanged:
// exit-on-sigterm.mjs
process.on('SIGTERM', () => process.exit(0));
c8 --reporter=text --include="src/**" node --import ./exit-on-sigterm.mjs src/server.js

The report covers the whole Playwright run. It can’t say which test ran a route, and to combine it with unit-test coverage you merge the two reports yourself. See merge coverage reports.

Supercov: no changes, per test

npx supercov -- npx playwright test
npx supercov runs latest file src/server.js
Gaps
  NOT COVERED = line never executed; PARTIAL = line executed but some behavior remains untested
 LINE  STATUS        SOURCE
    9  PARTIAL       !Number.isInteger(cents) || cents < 0
       Unobserved: decision never true
       Unobserved: `!Number.isInteger(cents)` was never true; needs `!Number.isInteger(cents)` true, decision true
       Unobserved: `cents < 0` was never true; needs `!Number.isInteger(cents)` false, `cents < 0` true, decision true
       Inspect: npx supercov runs 'run_c05697f6caa2565a' line 'src/server.js:9'
   10  NOT COVERED   res.status(400).json({ error: 'cents must be a whole number' });
   11  NOT COVERED   return;

The run reported the server and discount.js at 14 of 17 lines, 5 of 8 branches and 1 of 5 MC/DC conditions. Both tools found the same untested code: the 400 response and the return cents; no test reached. They count lines differently, so the totals differ. supercov runs latest line src/discount.js:6 named the one test that ran it, TENOFF takes ten off orders of ten or more.

The same works for servers in Python, Ruby, Go, Java and Kotlin, and Rust Workers, under browser tests: see runner support. For the same question in unit tests, see which test covers this line.

More coverage guides