Converts V8 coverage format to Istanbul's coverage format for JavaScript testing tools
npx @tessl/cli install tessl/npm-v8-to-istanbul@9.3.0v8-to-istanbul converts V8 coverage format to Istanbul's coverage format, enabling developers to use V8's native code coverage data with Istanbul-compatible tools and reporters. It supports source map transformations, coverage ignores via special comments, and handles complex JavaScript module loading patterns.
npm install v8-to-istanbulconst v8ToIstanbul = require('v8-to-istanbul');For ESM:
import v8ToIstanbul from 'v8-to-istanbul';For TypeScript:
import v8ToIstanbul from 'v8-to-istanbul';
import type { Sources } from 'v8-to-istanbul';const v8ToIstanbul = require('v8-to-istanbul');
// Create converter instance for a source file
const converter = v8ToIstanbul('./path-to-file.js');
// Load the file and any source maps
await converter.load();
// Apply V8 coverage data
converter.applyCoverage([
{
"functionName": "",
"ranges": [
{
"startOffset": 0,
"endOffset": 520,
"count": 1
}
],
"isBlockCoverage": true
}
]);
// Convert to Istanbul format
const istanbulCoverage = converter.toIstanbul();
console.log(JSON.stringify(istanbulCoverage, null, 2));Creates a new V8ToIstanbul converter instance.
/**
* Factory function to create a V8ToIstanbul converter instance
* @param scriptPath - Path to the source file (supports file:// URLs)
* @param wrapperLength - Length of module wrapper code (auto-detected for CommonJS)
* @param sources - Optional sources configuration for providing content directly
* @param excludePath - Optional function to exclude certain paths from coverage
* @returns V8ToIstanbul converter instance
*/
declare function v8ToIstanbul(
scriptPath: string,
wrapperLength?: number,
sources?: Sources,
excludePath?: (path: string) => boolean
): V8ToIstanbul;Main converter class that handles the transformation process.
declare class V8ToIstanbul {
/**
* Loads and processes the source file, handles source maps
* Must be called before applying coverage
*/
load(): Promise<void>;
/**
* Applies V8 coverage data to the loaded source
* @param blocks - Array of V8 coverage blocks from Node.js inspector
*/
applyCoverage(blocks: ReadonlyArray<Profiler.FunctionCoverage>): void;
/**
* Converts processed coverage data to Istanbul format
* @returns Istanbul-compatible coverage data
*/
toIstanbul(): CoverageMapData;
/**
* Cleanup method (no longer necessary, preserved for backwards compatibility)
*/
destroy(): void;
}The library supports special comments to exclude lines from coverage tracking:
// Ignore the next line
/* v8 ignore next */
const platformSpecificCode = process.platform === 'win32' ? windowsCode() : defaultCode();
// Ignore the next N lines
/* v8 ignore next 3 */
if (process.platform === 'win32') {
console.log('Windows detected');
}
// Ignore blocks of code
/* v8 ignore start */
function debugOnlyFunction() {
// This entire function will be ignored
}
/* v8 ignore stop */
// Legacy c8 format is also supported
/* c8 ignore next */
/* c8 ignore next 2 */
/* c8 ignore start */
/* c8 ignore stop */
// Node.js native coverage comments
/* node:coverage disable */
function testHelperFunction() {
// This function will be ignored
}
/* node:coverage enable */import { Profiler } from 'inspector';
import { CoverageMapData } from 'istanbul-lib-coverage';
import { SourceMapInput } from '@jridgewell/trace-mapping';
/**
* Sources configuration for providing source content directly
*/
type Sources =
| { source: string }
| {
source: string;
originalSource: string;
sourceMap: { sourcemap: SourceMapInput };
};
/**
* V8 function coverage data from Node.js inspector
* This is the standard Profiler.FunctionCoverage interface
*/
type FunctionCoverage = Profiler.FunctionCoverage;
/**
* Istanbul coverage map data structure
* This is the standard CoverageMapData from istanbul-lib-coverage
*/
type CoverageMapDataType = CoverageMapData;const v8ToIstanbul = require('v8-to-istanbul');
// For transpiled code with source maps
const converter = v8ToIstanbul('./dist/compiled.js');
await converter.load(); // Automatically detects and loads source maps
// Providing source content directly
const converter2 = v8ToIstanbul('./src/original.ts', 0, {
source: transpiledSource,
originalSource: originalSource,
sourceMap: { sourcemap: sourceMapObject }
});
await converter2.load();const converter = v8ToIstanbul('./src/app.js', 0, null, (path) => {
// Exclude test files and node_modules
return path.includes('/test/') || path.includes('node_modules');
});import { pathToFileURL } from 'url';
// Handle ESM file paths
const converter = v8ToIstanbul(
pathToFileURL('./src/module.mjs').href,
0 // ESM has no wrapper
);The library throws errors for:
Common error scenarios:
try {
const converter = v8ToIstanbul('./non-existent-file.js');
await converter.load();
} catch (error) {
if (error.code === 'ENOENT') {
console.error('Source file not found');
}
}