Istanbul collection
Step 1: Prepare the build
Before collecting Istanbul coverage, instrument your source code with Istanbul counters. Choose an integration for your build tool:
- webpack with babel loader: babel-plugin-istanbul; see webpack.config-istanbul.js.
- CLI: nyc instrument, or API: istanbul-lib-instrument.
- vite: vite-plugin-istanbul.
- rollup: rollup-plugin-istanbul.
- swc: swc-plugin-coverage-instrument.
Step 2: Collect coverage
Run your tests against the instrumented code, then retrieve the coverage data using one of the following approaches.
1. Node.js
Read coverage data from global.__coverage__.
2. Browser
Read coverage data from window.__coverage__; see test-istanbul.js.
3. General-purpose CDP
Use getIstanbulCoverage() to retrieve Istanbul coverage through CDP; see CDPClient.
Step 3: Generate a report
Here, page is an existing page that has loaded your instrumented application and completed the test actions. For browser and page initialization, see the Playwright or Puppeteer examples; Istanbul collection does not require the native V8 / CSS coverage calls in those examples. Retrieve the page's coverage object into Node.js:
const coverageData = await page.evaluate(() => window.__coverage__);
if (!coverageData || !Object.keys(coverageData).length) {
throw new Error('No Istanbul coverage was collected; check instrumentation');
}Continue with the quick-start API example, changing reports to ['html', 'console-summary']. See Istanbul reports for other compatible formats.
In Node.js, replace page.evaluate() with global.__coverage__ in the same process that executes the instrumented code; a separate report script cannot directly read another process's globals.