Skip to content
tezvyn:
Menu

Code Coverage Reporting with nyc/Istanbul

Source: github.comMediumHow cards are made

Code coverage reporting asks, "Which lines of my code did my tests actually run?" Use a tool like nyc to wrap your test runner (e.g., Mocha) and generate a report. The footgun is chasing 100% coverage, which doesn't guarantee quality.

Why it exists

Code coverage tools were created to provide an objective measure of which parts of a codebase are exercised by a test suite. Without them, developers are just guessing how effective their tests are at covering different execution paths and edge cases.

The mental model

Think of code coverage reporting as a highlighter for your source code. After you run your tests, it highlights every line, branch, and function the tests touched. The un-highlighted code is your blind spot—code that could be broken without your tests ever noticing. It measures the quantity of execution, not the quality of your assertions.

How it works

The Istanbul.js library, via its command-line interface nyc, instruments your JavaScript code. This means it injects counters into your source code before the tests run. As your tests execute, these counters are incremented. After the tests complete, nyc collects the data from these counters and generates a report showing what percentage of statements, branches, functions, and lines were executed. It supports source maps, so it works correctly with transpiled code from TypeScript or Babel.

When to use it

Use nyc in your CI/CD pipeline to enforce a minimum coverage threshold and prevent regressions in test coverage. It's also useful during development to identify untested logic in new features. It integrates with most JavaScript test runners like Mocha or AVA by simply prefixing your test command, for example nyc mocha.

When not to use it

Avoid using nyc if your test runner already has Istanbul.js integrated. For example, Jest and Tap provide code coverage reporting out of the box, so adding nyc is redundant. Also, don't use coverage as the sole metric for code quality; a line can be "covered" by a test that makes no meaningful assertions about its behavior.

One canonical example

To add coverage reporting to a project using Mocha, first install nyc as a dev dependency: npm install --save-dev nyc. Then, update your package.json scripts. If your test script is "test": "mocha", you would add a new script: "coverage": "nyc npm run test". Running npm run coverage will now execute your tests and generate a coverage report in the console and as an HTML file in a ./coverage directory.

Interview question

How does nyc (Istanbul) measure which parts of your code are executed by tests?

  • a.It analyzes the runtime call stack to identify all invoked functions and lines.
  • b.It compares the test suite's output against a model of all possible execution paths.
  • c.It injects counters into the source code before execution, which increment as code runs.Correct
  • d.It uses static analysis to predict which code blocks are reachable by the tests.
Why?

nyc (Istanbul) works by instrumenting your code: it injects counters into the source code before tests run. These counters increment as the code executes, providing a direct measure of what was covered, rather than relying on runtime call stack analysis or static prediction.

Just read this? Test yourself on what you have been reading.

Read the original → github.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on testing — each one lists the topics its interview covers.

See open roles