This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Mocha Blanket Code Coverage Cheatsheet

A quick reference guide for measuring JavaScript test code coverage with Blanket.js and Mocha.

· Published

[!NOTE] Historical Tool: Blanket.js was widely used for lightweight in-process Mocha code coverage. For modern Node.js and TypeScript codebases, c8 (native V8 coverage) or NYC / Istanbul is recommended.

Blanket.js is a seamless JavaScript code coverage library designed to run with test runners like Mocha.

Installation

npm install --save-dev blanket

Test Helper Setup

Require Blanket before requiring the files you want to cover:

// test/helper.js
if (process.env.COVERAGE) {
  require('blanket')({
    pattern: require('path').resolve('./src')
  });
}

// Require modules after blanket hook is registered
const myModule = require('../src/index');

Package Scripts

Add coverage commands to package.json:

{
  "scripts": {
    "test": "mocha test/",
    "coverage": "env COVERAGE=1 mocha -R html-cov > coverage.html && open coverage.html"
  }
}

Add the generated HTML report to your .gitignore:

echo "coverage.html" >> .gitignore

Run coverage report generation:

npm run coverage

Continuous Integration & Coveralls

Install the LCOV reporter and Coveralls adapter:

npm install --save-dev mocha-lcov-reporter coveralls

Add an execution step in CI pipelines:

mocha -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js

References

Related topics