I figured out one way to do this, but it's not too pretty. If you are an eval (I know!) Tool code, Istanbul writes a coverage object for the __coverage__ global variable. You can also specify the name of a global variable in the toolkit constructor if you wish. Here is the command line script showing how to do this:
const istanbul = require('istanbul'); const instrumenter = new istanbul.Instrumenter(); const collector = new istanbul.Collector(); const fs = require('fs'); const filename = 'file.js'; fs.readFile(filename, 'utf-8', (err, data) => { instrumenter.instrument(data, filename, (err, generatedCode) => { eval(generatedCode); console.log(JSON.stringify(global['__coverage__'])); }); });
Part of the file and console.log are for demonstration purposes only. All you really need is instrument and eval . Whether you will use eval yourself is up to you.
source share