CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-istanbul-lib-coverage

Data library for istanbul coverage objects providing read-only API with merge and summarize capabilities

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

file-coverage.mddocs/

File Coverage

File coverage objects provide detailed coverage analysis for individual files, including statement, function, and branch coverage with hit counts and location mapping.

Capabilities

FileCoverage Constructor

Creates a coverage object for a single file from a path or existing coverage data.

/**
 * Creates a FileCoverage instance
 * @param {string|Object|FileCoverage} pathOrObj - File path, coverage data, or existing FileCoverage
 * @param {boolean} [reportLogic=false] - Enable branch truthiness tracking
 */
constructor(pathOrObj, reportLogic)

Usage Examples:

const { createFileCoverage } = require('istanbul-lib-coverage');

// Create empty coverage for file path
const fc = createFileCoverage('/src/utils.js');

// Create from existing coverage data
const fc = createFileCoverage(existingCoverageData);

// Enable branch truthiness tracking
const fc = createFileCoverage('/src/utils.js', true);

Get Line Coverage

Returns computed line coverage from statement coverage as a map of hit counts keyed by line number.

/**
 * Returns computed line coverage from statement coverage
 * @returns {Object} Map of hits keyed by line number
 */
getLineCoverage(): Object

Usage Examples:

const fc = createFileCoverage(coverageData);
const lineCoverage = fc.getLineCoverage();

console.log('Line coverage:');
Object.entries(lineCoverage).forEach(([line, hits]) => {
  console.log(`Line ${line}: ${hits} hits`);
});

Get Uncovered Lines

Returns an array of line numbers that have zero hits.

/**
 * Returns array of uncovered line numbers
 * @returns {string[]} Line numbers with zero hits
 */
getUncoveredLines(): string[]

Usage Examples:

const fc = createFileCoverage(coverageData);
const uncoveredLines = fc.getUncoveredLines();

if (uncoveredLines.length > 0) {
  console.log('Uncovered lines:', uncoveredLines.join(', '));
} else {
  console.log('All lines covered!');
}

Get Branch Coverage by Line

Returns branch coverage organized by source line number with coverage statistics.

/**
 * Returns branch coverage organized by source line
 * @returns {Object} Keyed by line number with coverage stats
 */
getBranchCoverageByLine(): Object

Usage Examples:

const fc = createFileCoverage(coverageData);
const branchCoverage = fc.getBranchCoverageByLine();

Object.entries(branchCoverage).forEach(([line, stats]) => {
  console.log(`Line ${line}: ${stats.covered}/${stats.total} branches (${stats.coverage}%)`);
});

Merge File Coverage

Merges another file coverage object into this one, combining hit counts for the same file.

/**
 * Merges another coverage object into this one
 * @param {FileCoverage} other - Coverage object for same file to merge
 */
merge(other): void

Usage Examples:

const fc1 = createFileCoverage(coverage1);
const fc2 = createFileCoverage(coverage2);

// Merge fc2 into fc1 (both must be for same file)
fc1.merge(fc2);

console.log('Merged coverage:', fc1.toSummary());

Reset Hit Counts

Resets all hit counts to zero, resulting in zero coverage.

/**
 * Resets all hit counts to zero
 */
resetHits(): void

Usage Examples:

const fc = createFileCoverage(coverageData);

console.log('Before reset:', fc.toSummary());
fc.resetHits();
console.log('After reset:', fc.toSummary());

Get Coverage Summary

Returns a coverage summary object for this file with aggregated statistics.

/**
 * Returns coverage summary for this file
 * @returns {CoverageSummary} Coverage summary with statistics
 */
toSummary(): CoverageSummary

Usage Examples:

const fc = createFileCoverage(coverageData);
const summary = fc.toSummary();

console.log('File coverage summary:');
console.log(`Lines: ${summary.lines.covered}/${summary.lines.total} (${summary.lines.pct}%)`);
console.log(`Statements: ${summary.statements.covered}/${summary.statements.total} (${summary.statements.pct}%)`);
console.log(`Functions: ${summary.functions.covered}/${summary.functions.total} (${summary.functions.pct}%)`);
console.log(`Branches: ${summary.branches.covered}/${summary.branches.total} (${summary.branches.pct}%)`);

JSON Serialization

Returns a JSON-serializable representation of the file coverage data.

/**
 * Returns JSON-serializable POJO
 * @returns {Object} Raw coverage data
 */
toJSON(): Object

Usage Examples:

const fc = createFileCoverage(coverageData);
const jsonData = fc.toJSON();

// Access raw coverage data
console.log('File path:', jsonData.path);
console.log('Statement map:', jsonData.statementMap);
console.log('Statement hits:', jsonData.s);
console.log('Function hits:', jsonData.f);
console.log('Branch hits:', jsonData.b);

Data Properties

File coverage objects expose coverage data through getter properties:

// File and mapping properties
path: string                     // File path
statementMap: Object             // Statement location mapping
fnMap: Object                    // Function metadata mapping
branchMap: Object                // Branch metadata mapping

// Hit count properties  
s: Object                        // Statement hit counts
f: Object                        // Function hit counts
b: Object                        // Branch hit counts
bT: Object                       // Branch truthiness (optional)

// Special properties
all: boolean                     // Indicates if represents all files

Usage Examples:

const fc = createFileCoverage(coverageData);

console.log('File path:', fc.path);
console.log('Statements:', Object.keys(fc.s).length);
console.log('Functions:', Object.keys(fc.f).length);
console.log('Branches:', Object.keys(fc.b).length);

// Check if branch truthiness is enabled
if (fc.bT) {
  console.log('Branch truthiness tracking enabled');
}

Coverage Data Structure

The file coverage data follows this structure:

interface FileCoverageData {
  path: string;                  // File path
  statementMap: {                // Statement locations by index
    [index: string]: {
      start: { line: number, column: number };
      end: { line: number, column: number };
    };
  };
  fnMap: {                       // Function metadata by index
    [index: string]: {
      name: string;
      decl: { start: LineCol, end: LineCol };
      loc: { start: LineCol, end: LineCol };
      line: number;
    };
  };
  branchMap: {                   // Branch metadata by index
    [index: string]: {
      loc: { start: LineCol, end: LineCol };
      type: string;
      locations: Array<{ start: LineCol, end: LineCol }>;
      line: number;
    };
  };
  s: { [index: string]: number }; // Statement hit counts
  f: { [index: string]: number }; // Function hit counts  
  b: { [index: string]: number[] }; // Branch hit counts (array per branch)
  bT?: { [index: string]: number[] }; // Branch truthiness (optional)
}

interface LineCol {
  line: number;
  column: number;
}

docs

coverage-maps.md

coverage-summaries.md

file-coverage.md

index.md

tile.json