istanbul-js
Menu
Tutorials

webpack coverage: collect from the browser build you test

Instrument the browser build that your tests actually load.

Source passes through a transformation before its coverage is mapped back.
Test the instrumented browser build, preserve source maps, and export counters before teardown.

Is the browser executing the intended bundle? Who exports counters before navigation clears them? A nyc wrapper around a Node controller does not collect code running inside a separate browser by itself. Define those responsibilities before adding flags.

Give each stage one job

StageResponsibility
Coverage-only buildInsert counters into application modules.
Browser harnessExecute the instrumented asset and assertions.
Collection bridgeRetrieve the page’s coverage object.
Host report stepCombine compatible maps and render output.

If webpack already transforms source through Babel, babel-plugin-istanbul can own instrumentation. Its README states that it does not persist data or generate reports. Keep it in the test environment so ordinary production builds do not acquire counters.

Instrument through the test transform

This fragment assumes application source already passes through Babel in webpack, and the plugin is installed as a development dependency. Preserve existing presets and loader settings.

Babel configuration fragment

{
  "env": {
    "test": {
      "plugins": [["istanbul", {
        "exclude": ["**/*.test.js", "**/*.spec.js"]
      }]]
    }
  }
}

Set NODE_ENV=test for the coverage build with your project's environment mechanism. Do not add another instrumenter to the same source. Preserve maps through transformation and bundling, then verify locations in the report.

Export before page teardown

Retrieve counters after the tested actions finish. This snippet describes the browser-side value your existing automation should transfer; it is not a browser-runner API.

Inside the instrumented browser page

const snapshot = window.__coverage__;
if (!snapshot) {
  throw new Error('The page did not expose Istanbul counters');
}
const serializedCoverage = JSON.stringify(snapshot);

Use the runner's documented bridge to transfer JSON to the host. Save a distinct snapshot for each completed page session. Collect before closing the page, navigating away or replacing global state.

Use the raw-object workflow to render snapshots. Raw V8 ranges and Istanbul file maps are different formats; do not place one in the other's input directory without conversion.

Verify the collection path

ObservationNext check
The object is undefinedLoaded bundle and Babel test environment.
Only the controller is coveredBrowser export and transfer.
Bundle locations appearMaps through each build stage.
Results change after navigationSnapshot timing and page contexts.

Keep a branch probe in the test build: one asserted outcome should leave the alternative uncovered. This validates build, bridge and reporting together before you interpret a large report.

Primary sources

Check official documentation against the tools installed in your project. These explanations and examples are independent.