Skip to main content
Empirical tests your deployed application as a black box, so coverage needs an instrumented build. Your application team instruments the build and builds the report, and Empirical collects coverage data during test runs:
  1. Instrument the application. Add Istanbul to your bundler. This needs your application source, so your team does it.
  2. Collect coverage in tests. Turn on coverage in the test fixtures. An Empirical agent can make this change.
  3. Build the report. Download the coverage files from a run and build an HTML report with nyc. This also needs your application source.

Instrument the application

This example uses Vite. Other bundlers need an equivalent Istanbul plugin. In your application repository, install the plugin:
Add the plugin to vite.config.ts. When tests run against a deployed URL rather than localhost, keep forceBuildInstrument: true so production builds include the instrumentation:

Collect coverage in tests

Coverage collection needs @empiricalrun/playwright-utils 0.43.0 or later. In the test fixture file, pass { collectCoverage: true } to baseTestFixture:
Before each page closes, the fixture reads window.__coverage__ and attaches it to the test report as an Istanbul coverage JSON file, uploaded like other artifacts such as videos.

Build the report

Download the coverage files for a test run, then build the report with nyc. Fetch the run with the Get a test run API and download the Playwright JSON report from its summary_url. Replace <TEST_RUN_ID> with the run’s ID:
Download every coverage file in the report into .nyc_output:
From your application source directory, build the HTML report:
Open /path/to/final-output-dir/index.html to see coverage by file.

Coverage per test

To see which test covered which file, map each test case ID to its coverage files: