DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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 Now×
Skip to content
World desk4 min

How to Measure JavaScript Code Coverage in Puppeteer

Use Puppeteer’s coverage API to record JavaScript execution ranges and calculate the byte-based share of script text used in your tested session.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start Puppeteer’s JavaScript coverage before the navigation or interaction you want to observe, exercise the page, then stop coverage and divide the executed script bytes by the total script bytes. The result is a byte-based measure of code observed during that browser session—not a test-pass rate or a measure of every path your application could take.

Collect and calculate JavaScript coverage

Puppeteer’s Coverage API returns script entries and the ranges observed as executed. This runnable example starts collection before navigation, leaves room for your test interactions, then calculates the used-byte percentage:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();

  await page.coverage.startJSCoverage();
  await page.goto('https://example.com');

  // Exercise the interactions or flows whose code you want to measure here.

  const entries = await page.coverage.stopJSCoverage();
  let totalBytes = 0;
  let usedBytes = 0;

  for (const entry of entries) {
    totalBytes += entry.text.length;
    for (const range of entry.ranges) {
      usedBytes += range.end - range.start - 1;
    }
  }

  const percent = totalBytes === 0 ? 0 : (usedBytes / totalBytes) * 100;
  console.log(`Bytes used: ${percent}%`);
} finally {
  await browser.close();
}

Install Puppeteer in your project before running the script. Replace the example URL with your application and put the user actions under the navigation. The zero-total guard handles a session in which no script text is returned.

Choose what the coverage session includes

startJSCoverage() accepts options that affect which scripts and execution ranges appear in the report. The current API reference lists these defaults:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Option Default When to change it
resetOnNavigation true Navigation resets coverage by default. For a journey spanning pages, stop and restart collection per page, then merge the reports downstream rather than relying on this option being false.
reportAnonymousScripts false Set to true if dynamically generated scripts such as eval or new Function matter. They are named with debugger://VM-style URLs unless a //# sourceURL comment supplies a URL.
useBlockCoverage true Set to false to request function-level rather than block-level coverage.
includeRawScriptCoverage false Enable only when a downstream workflow needs V8’s raw script coverage entries.

These defaults are documented in Puppeteer’s startJSCoverage options reference. The options reference is versioned; check it against the Puppeteer release installed in your project.

Handle navigation without losing the report

Coverage collection brackets runtime activity. If you need results from multiple pages, stop coverage before leaving the current page, start a new collection on the next page, and merge the returned reports in your reporting workflow. Puppeteer warns that setting resetOnNavigation to false does not guarantee coverage survives navigation, because Chrome may discard the previous page’s execution environment. See the JSCoverageOptions interface.

Interpret the percentage carefully

The example totals each entry’s script text and adds the lengths of its executed ranges, then calculates usedBytes / totalBytes × 100. It measures observed execution against script text in the collected session. It does not report what fraction of tests passed, how many branches were specified, or how much of all theoretically reachable application code was exercised.

A low percentage can indicate that the scenarios did not exercise much code, but the number alone cannot tell whether uncovered code is dead, intentionally conditional, or simply outside the tested route. Treat it as a signal to investigate alongside the flows your tests cover, not as a stand-alone quality score.

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

Troubleshoot common coverage gaps

  • No entries or a zero total: Confirm that collection starts before the page activity and that scripts load during the session. The example returns zero rather than dividing by zero if no script text is collected.
  • Coverage disappears after a page change: Navigation resets coverage by default, and disabling the reset does not ensure the old execution environment remains available. Stop before navigation and start a fresh collection on the next page.
  • Generated scripts are missing: Anonymous scripts are excluded by default. Start coverage with reportAnonymousScripts: true if those scripts are in scope.
  • The report is too coarse for the question: Block-level ranges are the default. Set useBlockCoverage: false when function-level data is more appropriate.
  • A reporting tool cannot consume Puppeteer’s entries: Puppeteer’s guide points to puppeteer-to-istanbul to convert the output into a format consumable by Istanbul.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Capture a screenshot without managing Puppeteer

For a screenshot rather than a JavaScript coverage report, ScreenshotNeo offers a one-request screenshot API. This does not replace Puppeteer’s coverage collection; it is an option when the task is capturing a page image or PDF.

Or skip the browser setup

Make one GET request for a screenshot (replace the URL with the page you need):

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

See the ScreenshotNeo API documentation for request options. Before capture, it accepts cookie or consent banners as a visitor and removes 60+ known consent platforms, newsletter popups and chat widgets; each step can be turned off. Bot checks, blank pages, timeouts, failed loads and cache hits are not billed, and response headers identify the page verdict and billing status. An MCP server provides take_screenshot, get_page_info and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan to try it without a card.

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

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. 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…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.