[!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