Base reporting library for istanbul providing core utilities for generating coverage reports across different formats
npx @tessl/cli install tessl/npm-istanbul-lib-report@3.0.0Istanbul Lib Report is the foundational reporting library for the Istanbul code coverage ecosystem. It provides essential infrastructure for generating coverage reports across different formats including JSON, HTML, and text outputs. The library offers a flexible API for creating reporting contexts with customizable options such as output directories, watermark thresholds for coverage metrics, and different summarization strategies.
npm install istanbul-lib-reportconst libReport = require('istanbul-lib-report');const libReport = require('istanbul-lib-report');
// Create a context for report generation
const context = libReport.createContext({
dir: 'coverage',
watermarks: {
statements: [50, 80],
functions: [50, 80],
branches: [50, 80],
lines: [50, 80]
},
coverageMap, // from istanbul-lib-coverage
defaultSummarizer: 'nested'
});
// Get default watermarks
const defaultWatermarks = libReport.getDefaultWatermarks();
// Use the context to write reports
const writer = context.getWriter();
const contentWriter = writer.writeFile('report.txt');
contentWriter.println('Coverage Report');
contentWriter.close();Istanbul Lib Report is built around several key components:
Context class that provides report generation environment with configurable optionsFileWriter and ContentWriter classes for handling file and console outputXMLWriter utility for generating XML-based reportsPath class for normalized file path operations across platformsSummarizerFactory providing different tree organization strategies (flat, nested, pkg)Core reporting context creation and configuration. The Context class serves as the central hub for all reporting operations, managing output directories, watermarks, source code access, and tree summarization strategies.
function createContext(opts) {
// opts.dir - output directory (default: "coverage")
// opts.watermarks - coverage thresholds
// opts.sourceFinder - function to retrieve source code
// opts.coverageMap - coverage data
// opts.defaultSummarizer - summarization strategy
}
function getDefaultWatermarks() {
// Returns default watermark thresholds
}File and console output management with support for directory-based organization and content writing abstractions.
class FileWriter {
constructor(baseDir);
writeFile(file); // Returns ContentWriter
writerForDir(subdir); // Returns FileWriter
copyFile(source, dest, header);
}
class ContentWriter {
write(str);
println(str);
colorize(str, clazz);
close();
}Visitor pattern implementation for traversing coverage trees and generating reports with different organizational strategies.
class Visitor {
onStart(root, state);
onSummary(node, state);
onDetail(node, state);
onSummaryEnd(node, state);
onEnd(root, state);
}
class BaseTree {
visit(visitor, state);
}XML writing utilities for generating structured XML reports with proper indentation and tag management.
class XMLWriter {
constructor(contentWriter);
openTag(name, attrs);
closeTag(name);
inlineTag(name, attrs, content);
closeAll();
}// Context options interface
interface ContextOptions {
dir?: string;
watermarks?: WatermarkOptions;
sourceFinder?: (filePath: string) => string;
coverageMap: Object;
defaultSummarizer?: string;
}
// Watermark configuration
interface WatermarkOptions {
statements?: [number, number];
functions?: [number, number];
branches?: [number, number];
lines?: [number, number];
}
// Base class for all reports
class ReportBase {
constructor(opts?: { summarizer?: string });
execute(context: Context): void;
}