What is v8-to-istanbul?
The v8-to-istanbul package is a tool that converts V8 coverage information into a format that is compatible with Istanbul's coverage reports. It allows developers to generate coverage reports for JavaScript code executed in the V8 JavaScript engine, which is used by Node.js and Chrome.
What are v8-to-istanbul's main functionalities?
Convert V8 coverage to Istanbul format
This feature allows you to convert V8 coverage information into the Istanbul format. You can then use this data to generate reports with Istanbul-compatible tools.
const v8toIstanbul = require('v8-to-istanbul');
const scriptPath = './path/to/script.js';
const v8Coverage = require('./path/to/v8-coverage.json');
const converter = v8toIstanbul(scriptPath);
converter.load();
converter.applyCoverage(v8Coverage);
const istanbulCoverage = converter.toIstanbul();
console.log(JSON.stringify(istanbulCoverage, null, 2));
Exclude specific functions or lines from coverage
This feature allows you to exclude specific functions or ranges of lines from the coverage report. This is useful for ignoring code that you do not want to include in your coverage metrics.
const v8toIstanbul = require('v8-to-istanbul');
const scriptPath = './path/to/script.js';
const converter = v8toIstanbul(scriptPath, {
exclude: ['**/node_modules/**', '**/test/**']
});
converter.exclude('functionName');
converter.excludeRange([startLine, endLine]);
Other packages similar to v8-to-istanbul
nyc
nyc is a command-line tool and library for instrumenting code with Istanbul's coverage tracking and reporting. It is a more comprehensive tool that includes v8-to-istanbul's functionality as part of its coverage processing pipeline.
c8
c8 is a higher-level tool that uses v8-to-istanbul under the hood to collect and report V8 code coverage. It provides a simple command-line interface to generate coverage reports without needing to manually convert V8 coverage data.
istanbul-lib-instrument
istanbul-lib-instrument is part of the Istanbul API and is responsible for instrumenting JavaScript code. It differs from v8-to-istanbul in that it modifies the code to track coverage, rather than converting coverage data from the V8 engine.
v8-to-istanbul
converts from v8 coverage format to istanbul's coverage format.
Usage
const v8toIstanbul = require('v8-to-istanbul')
const converter = v8toIstanbul('./path-to-instrumented-file.js')
await converter.load()
converter.applyCoverage([
{
"functionName": "",
"ranges": [
{
"startOffset": 0,
"endOffset": 520,
"count": 1
}
],
"isBlockCoverage": true
},
])
console.info(JSON.stringify(converter.toIstanbul()))
Ignoring Uncovered Lines
Sometimes you might find yourself wanting to ignore uncovered lines
in your application (for example, perhaps you run your tests in Linux, but
there's code that only executes on Windows).
To ignore lines, use the special comment /* c8 ignore next */
.
ignoring the next line
const myVariable = 99
if (process.platform === 'win32') console.info('hello world')
ignoring the next N lines
const myVariable = 99
if (process.platform === 'win32') {
console.info('hello world')
}
ignoring all lines until told
function dontMindMe() {
}
const myVariable = 99
const os = process.platform === 'darwin' ? 'OSXy' : 'Windowsy'
Testing
To execute tests, simply run:
npm test