Skip to content

V8 collection ​

Step 1: Prepare the build ​

If your source code is compiled or bundled, enable sourcemaps in your build tool so runtime coverage can be mapped back to the original source. Disabling minification and tree shaking is recommended to simplify coverage debugging and retain code you want to measure.

  • webpack: devtool: 'source-map' and mode: 'development'; see webpack.config-v8.js.
  • rollup: sourcemap: true and treeshake: false.
  • esbuild: sourcemap: true, treeShaking: false, and minify: false.
  • vite: sourcemap: true and minify: false.

Keep the original sources, generated files, and sourcemaps from the exact build used in your tests. See Sourcemaps and debugging for details.

Step 2: Collect coverage ​

Choose one of the following three approaches based on your runtime and integration.

1. Node.js ​

You usually do not need to collect coverage manually. Wrap an existing Node.js command with mcr, or run node:test tests with mcr test. Both collect coverage and generate reports for you.

For a custom workflow that needs control over when or where coverage is collected, choose one of these six approaches:

  1. Write on exit: NODE_V8_COVERAGE. Set the variable to a dedicated directory before starting Node.js. After the process exits normally, use mcr.addFromDir(dir) to read the coverage JSON files. See the collection example and report generation example.
  2. Write on demand from inside the process: v8.takeCoverage(). Start with NODE_V8_COVERAGE set, then call v8.takeCoverage() in the tested process after exercising the code you want to measure. There is no need to wait for exit. Each write resets the execution counters, so keep and import all snapshots if you want coverage for the whole run. See the Node.js V8 API example.
  3. Collect over a test window in the same process: Inspector. Open a node:inspector session, start precise coverage before the tests, and take a snapshot after they finish. Attach the exact executed source to the entries before passing them to mcr.add(). If code executed in a vm has a wrapper prefix, use scriptOffset to tell MCR its length. See the Inspector example and VM example.
  4. Collect directly through a debugging port: CDP. Start the target process with --inspect. From another process, use CDPClient to start and stop JavaScript coverage and pass the entries to mcr.add(). This does not require NODE_V8_COVERAGE. See the Node.js CDP example.
  5. Write an on-demand snapshot from a running server: CDP + NODE_V8_COVERAGE. If the server stays running and you cannot call v8.takeCoverage() in its code, enable both the environment variable and a debugging port. After exercising the server, call writeCoverage() on the CDP client. It uses CDP to call v8.takeCoverage() inside the target process. Import the snapshot with mcr.addFromDir(dir) without waiting for the server to stop. See the Koa server example.
  6. Collect coverage from child processes: child_process + NODE_V8_COVERAGE. Pass the coverage directory in the environment when starting each Node.js child. Use the same absolute path if working directories differ. Wait for all children to exit normally and finish writing before importing the files in the parent. To wrap an existing command without writing this plumbing, use the MCR CLI.

2. Browser ​

Native V8 coverage collection in browsers is supported only in Chromium-based browsers. Use a browser automation tool to start collection before your tests and retrieve JavaScript and CSS coverage afterward:

Playwright ​

Here, page is an existing Playwright Chromium page. Replace your page url with the tested page URL, and run and await your test actions after navigation and before stopping coverage.

js
await Promise.all([
    page.coverage.startJSCoverage({
        // reportAnonymousScripts: true,
        resetOnNavigation: false
    }),
    page.coverage.startCSSCoverage({
        // Note, anonymous styles (without sourceURLs) are not supported, alternatively, you can use CDPClient
        resetOnNavigation: false
    })
]);

await page.goto('your page url');

const [jsCoverage, cssCoverage] = await Promise.all([
    page.coverage.stopJSCoverage(),
    page.coverage.stopCSSCoverage()
]);

const coverageData = [... jsCoverage, ... cssCoverage];

For Playwright Test automatic fixtures, see fixtures.js in the complete playwright-coverage example. The best-practices guide explains the integration. For more examples, see ./test/test-v8.js, css

For more integration examples, see Playwright.

Puppeteer ​

Here, page is a Puppeteer page, not the Playwright page object above; see the browser and page initialization in the Puppeteer example. Replace your page url with the tested page URL, and run and await your test actions after navigation and before stopping coverage.

Use Puppeteer’s coverage API with includeRawScriptCoverage: true to retrieve native V8 script coverage, then combine it with the collected CSS coverage as shown below.

Depending on your package manager, Puppeteer may not download a browser during installation. If no browser is available, install one manually:

sh
npx puppeteer browsers install chrome
js
await Promise.all([
    page.coverage.startJSCoverage({
        // reportAnonymousScripts: true,
        resetOnNavigation: false,
        // provide raw v8 coverage data
        includeRawScriptCoverage: true
    }),
    page.coverage.startCSSCoverage({
        resetOnNavigation: false
    })
]);

await page.goto('your page url');

const [jsCoverage, cssCoverage] = await Promise.all([
    page.coverage.stopJSCoverage(),
    page.coverage.stopCSSCoverage()
]);

// to raw V8 script coverage
const coverageData = [... jsCoverage.map((it) => {
    return {
        source: it.text,
        ... it.rawScriptCoverage
    };
}), ... cssCoverage];

Example: ./test/test-puppeteer.js

For more integration examples, see Puppeteer.

3. General-purpose CDP ​

The Chrome DevTools Protocol (CDP) can connect to a Node.js process or Chromium browser with debugging enabled. MCR provides CDPClient to start and stop collection and retrieve coverage through a debugging port or an existing CDP session.

See CDPClient for integration details and examples.

Step 3: Generate a report ​

Use the quick-start API example and choose the input that matches your collection method:

  1. Entries returned by a collection API: Pass coverageData from the browser, Inspector or CDP to mcr.add(coverageData). Close any browser or CDP client you created after collection.
  2. Snapshots written to disk by Node.js: Once the coverage files have been written, use await mcr.addFromDir('./v8-coverage') instead. Use the actual NODE_V8_COVERAGE directory, separate from the report output. Use inputDir for raw reports exported by MCR instead.

When copying or combining Node.js coverage:

  • Keep matching inputs. Preserve the tested source and sourcemaps, the original coverage-*.json filenames and any source-*.json captures with their execution metadata.
  • Check source matching. MCR uses a lone capture directly. For multiple captures of the same URL, it prefers the matching process and thread with the latest start before or at the snapshot time. If there is no match, it prefers legacy captures without metadata, then falls back to filename order. The same rules apply to dataDir.
  • Separate incompatible runs. Use separate collection directories for different builds, machines or loaders. For example, if one run captures native TS and another runs transformed JS at the same URL without capturing its source, sharing a directory can make MCR apply the TS capture to both. Import the directories separately or use raw reports.