or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-v8-to-istanbul

Converts V8 coverage format to Istanbul's coverage format for JavaScript testing tools

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/v8-to-istanbul@9.3.x

To install, run

npx @tessl/cli install tessl/npm-v8-to-istanbul@9.3.0

index.mddocs/

v8-to-istanbul

v8-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.

Package Information

  • Package Name: v8-to-istanbul
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install v8-to-istanbul
  • Node.js Compatibility: >= 10.12.0

Core Imports

const 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';

Basic Usage

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));

Capabilities

Factory Function

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;

V8ToIstanbul Class

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;
}

Coverage Ignore Comments

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 */

Types

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;

Advanced Usage

Working with Source Maps

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();

Excluding Paths from Coverage

const converter = v8ToIstanbul('./src/app.js', 0, null, (path) => {
  // Exclude test files and node_modules
  return path.includes('/test/') || path.includes('node_modules');
});

ESM and File URLs

import { pathToFileURL } from 'url';

// Handle ESM file paths
const converter = v8ToIstanbul(
  pathToFileURL('./src/module.mjs').href,
  0 // ESM has no wrapper
);

Error Handling

The library throws errors for:

  • Invalid script paths (non-string values)
  • Node.js versions below 10.12.0
  • File system errors when loading source files
  • Invalid V8 coverage data structures

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');
  }
}