Last active
September 6, 2017 09:08
-
-
Save cancerberoSgx/c4ea6edd2862af0fc229598d8531fddb to your computer and use it in GitHub Desktop.
istanbuljs example of programmatic instrumentation, and coverage report.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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