Minimal Code for Programming in Istanbul

I want to execute a command

istanbul cover node_modules/mocha/bin/_mocha dist/test 

using the Istanbul software API. But the docs don't say anything about this, except that everything is possible and related to the vast API documentation. I could not find a single short example on the Internet. I do not want to start a child process or use another module from NPM. I know how to run Mocha programmatically without coverage, so this is not a problem.

+5
source share
1 answer

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.

0
source

All Articles