October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Collect JavaScript Coverage in Puppeteer

Collect JavaScript coverage with Puppeteer’s Page Coverage API, understand its options, and use stop-and-restart boundaries to handle navigation reliably.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Puppeteer’s page.coverage.startJSCoverage() before the page activity you want to measure, then call page.coverage.stopJSCoverage() afterward. The returned entries contain script text and ranges recorded as executed. For coverage spanning navigations, stop and restart collection at each transition; setting resetOnNavigation: false does not guarantee that earlier data survives.

Collect JavaScript coverage on a page

Coverage is exposed on a Puppeteer Page. Start it before navigating or triggering the behavior under test, and stop it when that behavior is finished. This runnable example visits a page, collects coverage, and calculates the share of collected script bytes represented by recorded used ranges.

const jsCoverage = await page.coverage.startJSCoverage();
await page.goto('https://example.com');
const jsCoverage = await page.coverage.stopJSCoverage();

The first line above should not assign a value: startJSCoverage() starts collection, while stopJSCoverage() returns the entries. Use this corrected complete sequence:

await page.coverage.startJSCoverage();
await page.goto('https://example.com');
const jsCoverage = await page.coverage.stopJSCoverage();

let totalBytes = 0;
let usedBytes = 0;
for (const entry of jsCoverage) {
  totalBytes += entry.text.length;
  for (const range of entry.ranges) {
    usedBytes += range.end - range.start - 1;
  }
}
const percentUsed = totalBytes === 0 ? 0 : (usedBytes / totalBytes) * 100;
console.log(`Used script bytes: ${percentUsed.toFixed(2)}%`);

The byte calculation follows Puppeteer’s documented example. Interpret the result as the percentage of collected script text represented by recorded used ranges—not as proof that tests are complete or that the code is well tested. See the Puppeteer Coverage class documentation and the stopJSCoverage() method reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Measure interactions, not just initial loading

Place the start call before the actions whose scripts you want represented. For example, start coverage, navigate, click or otherwise exercise the relevant controls, then stop coverage. If collection starts after an interaction, that interaction’s earlier execution is not part of the collection.

Choose coverage options

startJSCoverage() accepts options that affect what is recorded. The documented defaults are shown below; consult the startJSCoverage() reference for the API signature and JSCoverageOptions for option details.

Option Documented default Effect
resetOnNavigation true Resets collection on navigation. A value of false is not a guarantee that data from the previous page survives.
reportAnonymousScripts false Includes scripts without an associated URL, such as code created with eval or new Function. Such scripts generally use a debugger://VM URL unless a //# sourceURL comment supplies one.
includeRawScriptCoverage false Includes raw V8 script coverage entries in the result. Turn it on when a downstream consumer needs that raw data.
useBlockCoverage true Collects block-level rather than function-level coverage.

For example, to include anonymous scripts and request function-level collection:

await page.coverage.startJSCoverage({
  reportAnonymousScripts: true,
  useBlockCoverage: false,
  includeRawScriptCoverage: false,
  resetOnNavigation: true,
});

Set options deliberately: adding anonymous or raw data may change what downstream tools receive, and consumers must be able to handle the resulting entries.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Handle page navigations reliably

Do not rely on resetOnNavigation: false to preserve coverage across page transitions. Chrome may discard the previous page’s JavaScript execution environment and its coverage data. Puppeteer documents this caveat in the JSCoverageOptions reference.

For a multi-page journey, collect each page separately and combine the reports in your own reporting step:

  1. Start coverage before the activity on the first page.
  2. Stop coverage before navigating away and save the returned entries.
  3. Navigate to the next page, start a fresh collection, and exercise that page.
  4. Stop and save that collection; repeat for each transition.
  5. Merge the saved reports in the downstream workflow.

This makes each page’s collection boundary explicit rather than assuming browser execution data will persist across navigation.

Send the result to Istanbul

Puppeteer’s Coverage documentation points to puppeteer-to-istanbul as a route for producing output consumable by Istanbul. The Puppeteer reference identifies that converter but does not prescribe a universal Istanbul configuration or pipeline, so configure the conversion for your project’s reporting setup.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot missing or unexpected coverage

  • No useful entries: Verify that coverage starts before navigation or the code path under test, and that it stops after the relevant activity.
  • Coverage appears to vanish after navigation: Chrome may discard the prior page’s execution environment. Stop before navigating, start a new collection afterward, and merge the resulting reports.
  • Dynamic scripts are absent: Enable reportAnonymousScripts if you need scripts without associated URLs, including eval or new Function code.
  • Granularity does not match your report: Check useBlockCoverage; its documented default is block-level coverage, while false requests function-level coverage.
  • A downstream consumer lacks raw entries: Set includeRawScriptCoverage: true only if that consumer needs raw V8 data.
  • The percentage is zero or misleading: Check whether entries were collected and avoid dividing by zero when total script text is empty. The percentage describes recorded used bytes, not test completeness.

Or skip the browser setup

ScreenshotNeo captures screenshots or PDFs; it does not collect Puppeteer JavaScript coverage. If the task is to capture a page rather than measure executed code, its one-request API is an alternative:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Learn about ScreenshotNeo, or sign up for 1,000 free screenshots a month with no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.