JavaScript code instrumentation engine that parses source files and adds coverage tracking statements.
Main instrumentation method for adding coverage tracking to JavaScript code.
/**
* Instrument JavaScript code for coverage tracking (alias for instrumentSync)
* @param config - Instrumentation configuration
* @param config.inputFile - Source code string to instrument
* @param config.inputFileName - Filename for the source code (used in reports)
* @param next - Optional callback function receiving instrumented code
* @returns Instrumented code string (when used synchronously without callback)
*/
function instrument(config, next);Synchronous code instrumentation with optional caching support.
/**
* Synchronously instrument JavaScript code for coverage tracking
* @param config - Instrumentation configuration
* @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 string (when used synchronously without callback)
*/
function instrumentSync(config, next);Usage Examples:
// Synchronous instrumentation
const sourceCode = 'function add(a, b) { return a + b; }';
const instrumented = blanket.instrumentSync({
inputFile: sourceCode,
inputFileName: 'math.js'
});
// Asynchronous instrumentation with callback
blanket.instrument({
inputFile: sourceCode,
inputFileName: 'math.js'
}, function(instrumentedCode) {
console.log('Instrumented:', instrumentedCode);
});Blanket.js uses Esprima parser and node-falafel to:
The following JavaScript statement types are instrumented for coverage:
When branchTracking option is enabled, Blanket.js also tracks:
Instrumented code includes:
Example of instrumented code:
// Original code
function add(a, b) {
return a + b;
}
// Instrumented code (simplified)
if (typeof _$jscoverage === 'undefined') _$jscoverage = {};
if (typeof _$jscoverage['math.js'] === 'undefined') {
_$jscoverage['math.js'] = [];
_$jscoverage['math.js'].source = ['function add(a, b) {', ' return a + b;', '}'];
_$jscoverage['math.js'][1] = 0;
_$jscoverage['math.js'][2] = 0;
}
function add(a, b) {
_$jscoverage['math.js'][1]++;
_$jscoverage['math.js'][2]++;
return a + b;
}When instrumentCache option is enabled (browser only), instrumented code is cached in sessionStorage.
Cache behavior:
"blanket_instrument_store-" + filenameThe caching is handled automatically by instrumentSync, but the process:
When instrumentation fails:
ignoreScriptError: true: Logs error and continues with original codeCommon errors:
ecmaVersion option)When sourceURL option is enabled, adds source URL comments to instrumented code:
// Instrumented code...
//@ sourceURL=math.jsThis helps debuggers map instrumented code back to original files.
Control JavaScript parsing with ecmaVersion option:
blanket.options("ecmaVersion", 6); // Parse ES6 featuresBlanket.js automatically detects and preserves strict mode directives:
// Original
'use strict';
function myFunc() { }
// Instrumented (preserves strict mode)
'use strict';
// coverage setup...
function myFunc() {
// tracking code...
}Automatically removes shebang lines before parsing:
// Original
#!/usr/bin/env node
console.log('Hello');
// Processed (shebang removed for parsing)
console.log('Hello');