Course contentsShow
← JavaScript
Lesson 2717 of 2,72354. Testing in JavaScriptFree lesson

Generating Coverage Reports

Using built-in test runner coverage tools and c8/nyc to generate coverage data and HTML reports.

Generating Coverage Reports

You'll learn how to produce detailed coverage reports using Node's test runner and third-party tools like c8 and nyc.

What Coverage Reports Show You

Once you understand what code coverage measures (lines, branches, functions executed during tests), the next step is visualizing that data. Coverage reports transform raw instrumentation data into readable formats—typically terminal summaries and HTML pages—that highlight exactly which code paths your tests exercise and which they miss.

Built-in Test Runner Coverage

Node's native test runner (v18+) integrates coverage through the --experimental-test-coverage flag:

node --test --experimental-test-coverage

This outputs a summary table showing percentages for each file. It's basic but built-in—no extra dependencies needed.

Using c8 for Comprehensive Reports

c8 leverages Node's V8 engine coverage natively. Install it with npm install --save-dev c8, then run:

c8 node --test

For HTML reports (browsable, color-coded files):

c8 --reporter=html node --test

Open coverage/index.html to see green (covered) and red (uncovered) line highlighting.

Using nyc (for Older Projects)

nyc is the older, battle-tested option (especially with Babel/transpiled code):

npx nyc node --test
npx nyc --reporter=html node --test

Both tools support multiple reporters simultaneously (--reporter=text --reporter=lcov), making them CI/CD friendly.

Which Tool to Choose

  • Node built-in: Quick checks, no setup
  • c8: Modern, fast, works with native ESM
  • nyc: Mature, better for complex transpilation scenarios

All three generate similar reports; c8 is generally preferred for greenfield projects.

Key Takeaway: Coverage reports turn abstract metrics into actionable visuals—use Node's built-in flag for quick checks, or c8/nyc for detailed HTML reports that pinpoint untested code paths.