Skip to content

Instantly share code, notes, and snippets.

@cancerberoSgx
Last active September 6, 2017 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cancerberoSgx/c4ea6edd2862af0fc229598d8531fddb to your computer and use it in GitHub Desktop.
Save cancerberoSgx/c4ea6edd2862af0fc229598d8531fddb to your computer and use it in GitHub Desktop.
istanbuljs example of programmatic instrumentation, and coverage report.
// In this example we are instrumenting JavaScript code string, executing it, and then generating a coverage report.
// instrument the code
var instrumenter = require('istanbul-lib-instrument').createInstrumenter({})
var instrumentedCode = instrumenter.instrumentSync('var a = 1; function f(){return 1}; f(); ', 'file1.js')
// execute the code
eval(instrumentedCode)
// at this point the global variable __coverage__ contains the coverage data
var globalCoverageVar = __coverage__;
// generate the coverage report
var libCoverage = require('istanbul-lib-coverage');
var map = libCoverage.createCoverageMap(globalCoverageVar);
const createReporter = require('istanbul-api').createReporter;
const reporter = createReporter();
reporter.addAll(['json', 'lcov', 'text', 'html']);
reporter.write(map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment