Measure test coverage on a JavaScript project by wrapping your test command with nyc to see which lines are untested.
Enforce minimum coverage thresholds in CI so test quality cannot silently degrade over time.
Generate lcov or HTML coverage reports for upload to services like Codecov or Coveralls.
Track coverage across TypeScript or Babel projects using built-in preset support.
Install with npm install --save-dev nyc, then prefix your test script in package.json with nyc (e.g. nyc mocha). No other config needed to get a basic terminal coverage report.
nyc is the command-line tool for Istanbul, a JavaScript code coverage library. Code coverage tells you which lines of your code actually ran during your tests, and which lines were never touched. This is useful for spotting parts of your code that your tests do not exercise, and for setting minimum thresholds to ensure test quality does not slip over time. To use nyc, you wrap your existing test command with it. If you normally run your tests with mocha, you would instead run nyc mocha, and nyc will automatically track which lines ran and generate a report when the tests finish. It works with most JavaScript test frameworks including mocha, tap, and AVA. If you use jest or tap, those tools already include Istanbul's coverage engine and you would configure coverage through them directly rather than through nyc. The tool instruments your JavaScript files by inserting counters into the code before running it. By default it only tracks files that actually get loaded during the test run, but you can turn on an --all flag to include every file in the project in the report, even ones no test touched. You can include or exclude specific files and folders using glob patterns. Reports can be output in several formats. The default prints a summary table in the terminal. Other formats include lcov (a widely supported format that most CI tools and code coverage services can consume) and HTML (which produces a browsable page showing which lines are covered). Multiple formats can be requested at once. Configuration can be done through command-line flags, through a dedicated config file in JSON or YAML format, or through a nyc section in package.json. There is preset support for TypeScript and Babel projects. nyc also handles projects that spawn child processes during tests, tracking coverage across all spawned processes.
← istanbuljs on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.