Seamless JavaScript code coverage library for both browser and Node.js environments
npx @tessl/cli install tessl/npm-blanket@1.2.0Blanket.js is a seamless JavaScript code coverage library that aims to be easy to install, use, and understand. It works by loading source files, parsing and instrumenting the code using Esprima and node-falafel to add code tracking lines, then connecting to test runner hooks to output coverage details after tests complete.
npm install blanketNode.js (CommonJS):
const blanket = require("blanket");Browser (global variable):
<script src="blanket.js"></script>
<script>
// blanket is now available globally
</script>const blanket = require("blanket");
// Configure coverage options
blanket.options({
filter: "src/",
antifilter: "test/"
});
// Setup coverage tracking
blanket.setupCoverage();
// Your tests run here...
// Blanket will automatically instrument and track coverage
// Generate report after tests complete
blanket.onTestsDone();<script src="blanket.js"></script>
<script>
// Configure before loading source files
blanket.options({
filter: "//src/",
reporter: "./path/to/reporter.js"
});
</script>
<!-- Your source files will be automatically instrumented -->
<script src="src/myfile.js" data-cover></script>
<!-- Test files -->
<script src="test/tests.js"></script>Blanket.js operates in a three-step process:
Key components:
Core configuration methods for setting up coverage options, filters, and behaviors.
/**
* Get or set configuration options
* @param key - Option key (string) or options object
* @param value - Option value (when key is string)
* @returns Option value (when used as getter)
*/
function options(key, value);JavaScript code instrumentation engine that adds coverage tracking to source files.
/**
* Instrument JavaScript code for coverage tracking
* @param config - Configuration object
* @param config.inputFile - Source code string to instrument
* @param config.inputFileName - Filename for the source code
* @param next - Optional callback function receiving instrumented code
* @returns Instrumented code (when used synchronously)
*/
function instrument(config, next);
/**
* Synchronous code instrumentation
* @param config - Configuration object
* @param next - Optional callback function
* @returns Instrumented code string
*/
function instrumentSync(config, next);Methods for integrating with test frameworks and tracking test execution.
/**
* Initialize coverage tracking with default stats
*/
function setupCoverage();
/**
* Mark the beginning of a test
*/
function onTestStart();
/**
* Mark test completion with results
* @param total - Total number of assertions
* @param passed - Number of passed assertions
*/
function onTestDone(total, passed);
/**
* Mark the beginning of a test suite/module
*/
function onModuleStart();
/**
* Mark completion of all tests and trigger reporting
*/
function onTestsDone();File pattern matching utilities for determining which files to instrument.
/**
* Match filename against pattern
* @param filename - File path to test
* @param pattern - Pattern to match (string, array, regex, or function)
* @returns Whether filename matches pattern
*/
function matchPattern(filename, pattern);
/**
* Normalize backslashes in file paths
* @param str - File path string
* @returns Normalized path with forward slashes
*/
function normalizeBackslashes(str);Browser-specific functionality for script loading, CORS handling, and manual file selection.
/**
* Track files being loaded for coverage
* @param filename - File being loaded (optional)
* @param done - Mark file as loaded (optional)
*/
function requiringFile(filename, done);
/**
* Check if all required files have been loaded
* @returns Whether all files are loaded
*/
function requireFilesLoaded();
/**
* Setup coverage before test runner starts
* @param opts - Configuration options
* @param opts.callback - Completion callback
* @param opts.coverage - Enable/disable coverage (default: true)
*/
function beforeStartTestRunner(opts);Coverage report generation and custom reporter support.
/**
* Generate coverage report using configured reporter
* @param coverage_data - Coverage statistics and file data
*/
function report(coverage_data);
/**
* Get coverage variable name
* @returns Coverage variable name string
*/
function getCovVar();// Configuration options object
interface BlanketOptions {
filter?: string | string[] | RegExp | Function;
antifilter?: string | string[] | RegExp | Function;
reporter?: string | Function;
adapter?: string;
loader?: string | string[];
ignoreScriptError?: boolean;
autoStart?: boolean;
branchTracking?: boolean;
debug?: boolean;
engineOnly?: boolean;
customVariable?: string;
timeout?: number;
ecmaVersion?: number;
reporter_options?: object;
}
// Instrumentation configuration
interface InstrumentConfig {
inputFile: string;
inputFileName: string;
}
// Test runner setup options
interface TestRunnerOptions {
callback?: Function;
coverage?: boolean;
condition?: Function;
bindEvent?: Function;
checkRequirejs?: boolean;
}
// Coverage data structure
interface CoverageData {
files: {
[filename: string]: {
source: string[];
[lineNumber: number]: number;
branchData?: object;
};
};
stats: {
suites: number;
tests: number;
passes: number;
pending: number;
failures: number;
start: Date;
end: Date;
};
}