V8 coverage provider for Vitest that enables code coverage collection using Chrome's V8 JavaScript engine native profiler capabilities
npx @tessl/cli install tessl/npm-vitest--coverage-v8@4.0.1@vitest/coverage-v8 is a V8-based code coverage provider for the Vitest testing framework. It enables high-performance coverage collection using Chrome's V8 JavaScript engine native profiler capabilities, without requiring source code instrumentation. The provider collects coverage data directly from V8's runtime profiler and converts it to Istanbul format for comprehensive reporting.
npm install -D @vitest/coverage-v8 or pnpm add -D @vitest/coverage-v8vitest (required), @vitest/browser (optional, for browser mode)Important: This package is typically not imported directly in user code. Instead, it's configured in your Vitest configuration file and loaded automatically by Vitest.
The package provides two main entry points for different environments (used internally by Vitest):
Node.js environment (internal):
import coverageModule from '@vitest/coverage-v8';Browser environment (internal):
import coverageModule from '@vitest/coverage-v8/browser';These modules are loaded automatically by Vitest based on your configuration - you don't need to import them manually.
Configure in your Vitest configuration file:
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
provider: 'v8',
enabled: true,
reporter: ['text', 'json', 'html'],
include: ['src/**/*.ts'],
exclude: ['node_modules/', 'test/'],
},
},
});Run tests with coverage:
vitest run --coverageThe @vitest/coverage-v8 package is structured around several key components:
CoverageProviderModule interface from Vitest, providing lifecycle methods for coverage collection in both Node.js and browser environmentsThe coverage collection workflow:
Coverage provider module implementation for Node.js environments that manages the V8 profiler lifecycle and coverage data collection using the Node.js Inspector API.
interface CoverageProviderModule {
startCoverage(options: { isolate?: boolean }): Promise<void>;
takeCoverage(options?: {
moduleExecutionInfo?: Map<string, { startOffset: number }>;
}): Promise<{ result: ScriptCoverageWithOffset[] }>;
stopCoverage(options: { isolate?: boolean }): Promise<void>;
getProvider(): Promise<V8CoverageProvider>;
}Coverage Provider Module (Node.js)
Coverage provider module implementation for browser environments that manages the V8 profiler lifecycle using Chrome DevTools Protocol for browser-based testing.
interface CoverageProviderModule {
startCoverage(): Promise<void>;
takeCoverage(): Promise<{ result: any[] }>;
stopCoverage(): void;
getProvider(): Promise<V8CoverageProvider>;
}Coverage Provider Module (Browser)
The main coverage provider class that implements the complete coverage workflow including data collection, conversion to Istanbul format, source map handling, and report generation.
class V8CoverageProvider extends BaseCoverageProvider<ResolvedCoverageOptions<'v8'>> {
name: 'v8';
version: string;
initialize(ctx: Vitest): void;
createCoverageMap(): CoverageMap;
generateCoverage(options: ReportContext): Promise<CoverageMap>;
generateReports(coverageMap: CoverageMap, allTestsRun?: boolean): Promise<void>;
parseConfigModule(configFilePath: string): Promise<ProxifiedModule<any>>;
}
interface ReportContext {
allTestsRun?: boolean;
}Extended script coverage type that includes offset information for module wrapper code.
interface ScriptCoverageWithOffset extends Profiler.ScriptCoverage {
/**
* Offset for wrapper code added during module execution.
* Used to adjust coverage ranges to account for code added by the module system.
*/
startOffset: number;
}V8 script coverage data from the Node.js inspector API:
interface ScriptCoverage {
/** Unique script identifier */
scriptId: string;
/** URL of the script */
url: string;
/** Coverage entries for functions in the script */
functions: FunctionCoverage[];
}
interface FunctionCoverage {
/** Name of the function */
functionName: string;
/** Coverage ranges within the function */
ranges: Range[];
/** Indicates whether this function was executed at least once */
isBlockCoverage: boolean;
}
interface Range {
/** Start offset in the script */
startOffset: number;
/** End offset in the script */
endOffset: number;
/** Execution count for this range */
count: number;
}Istanbul coverage map type from istanbul-lib-coverage:
interface CoverageMap {
/** Get list of covered file paths */
files(): string[];
/** Get coverage data for a specific file */
fileCoverageFor(filename: string): FileCoverage;
/** Merge another coverage map into this one */
merge(other: CoverageMap): void;
/** Filter coverage map based on predicate */
filter(predicate: (filename: string) => boolean): void;
}The provider inherits all configuration options from Vitest's coverage configuration (ResolvedCoverageOptions<'v8'>):
./coverage)ast-v8-to-istanbulistanbul-lib-source-maps