类型声明
本页直接读取 lib/index.d.ts 中的公共 TypeScript 声明并自动分节,与源文件保持同步。可通过本页目录或小节链接定位具体声明。中英文页面共用同一份声明,并保留原始注释。
- 源码:GitHub 上的 lib/index.d.ts
- 配置说明:配置选项
V8CoverageEntry
ts
export interface V8CoverageEntry {
url: string;
/** entry type */
type?: "js" | "css";
/** css only */
text?: string;
/** css only */
ranges?: any[];
/** js only */
source?: string;
/** js only */
scriptId?: string;
/** js only */
functions?: any[];
/** js only */
sourceMap?: any;
/** js only */
scriptOffset?: number;
/** js only */
distFile?: string;
/** empty coverage */
empty?: boolean;
/** fake source */
fake?: boolean;
/** Collected from Node native TypeScript; ingestion supplies stripped JS and a map to the original TS. */
nativeTypeScript?: boolean;
[key: string]: any;
}Watermarks
ts
export type Watermarks = [number, number] | {
/** V8 only */
bytes?: [number, number];
statements?: [number, number];
branches?: [number, number];
functions?: [number, number];
lines?: [number, number];
}ReportDescription
ts
export type ReportDescription =
['v8'] | ["v8", {
/**
* defaults to `index.html`
*/
outputFile?: string;
inline?: boolean;
assetsPath?: string;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
}] |
['v8-json'] | ["v8-json", {
/**
* defaults to `coverage-report.json`
*/
outputFile?: string;
}] |
['clover'] | ['clover', {
file?: string;
}] |
['cobertura'] | ['cobertura', {
file?: string;
timestamp?: string;
projectRoot?: string;
}] |
['html'] | ['html', {
subdir?: string;
verbose?: boolean;
linkMapper?: any;
skipEmpty?: boolean;
}] |
['html-spa'] | ['html-spa', {
subdir?: string;
verbose?: boolean;
linkMapper?: any;
skipEmpty?: boolean;
metricsToShow?: Array<"statements" | "branches" | "functions" | "lines">;
}] |
['json'] | ['json', {
file?: string;
}] |
['json-summary'] | ['json-summary', {
file?: string;
}] |
['lcov'] | ['lcov', {
file?: string;
projectRoot?: string;
}] |
['lcovonly'] | ['lcovonly', {
file?: string;
projectRoot?: string;
}] |
['none'] |
['teamcity'] | ['teamcity', {
file?: string;
blockName?: string;
}] |
['text'] | ['text', {
file?: string;
maxCols?: number;
skipEmpty?: boolean;
skipFull?: boolean;
}] |
['text-lcov'] | ['text-lcov', {
projectRoot?: string;
}] |
['text-summary'] | ['text-summary', {
file?: string;
}] |
['codecov'] | ["codecov", {
/**
* defaults to `codecov.json`
*/
outputFile?: string;
}] |
['codacy'] | ["codacy", {
/**
* defaults to `codacy.json`
*/
outputFile?: string;
}] |
['console-summary'] | ['console-summary', {
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
}] |
['console-details'] | ['console-details', {
maxCols?: number;
skipPercent?: number;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
filter?: string | {
[pattern: string]: boolean;
} | ((file: CoverageFile) => boolean);
}] |
['markdown-summary'] | ['markdown-summary', {
color?: 'unicode' | 'html' | 'tex' | string;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
/**
* defaults to `coverage-summary.md`
*/
outputFile?: string;
}] |
['markdown-details'] | ['markdown-details', {
baseUrl?: string;
color?: 'unicode' | 'html' | 'tex' | string;
maxCols?: number;
skipPercent?: number;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
filter?: string | {
[pattern: string]: boolean;
} | ((file: CoverageFile) => boolean);
/**
* defaults to `coverage-details.md`
*/
outputFile?: string;
}] |
['raw'] | ['raw', {
merge?: boolean;
zip?: boolean;
outputDir?: string;
}] |
[string] | [string, {
type?: "v8" | "istanbul" | "both" | string;
[key: string]: any;
}];AddedResults
ts
export type AddedResults = {
id: string;
path: string;
type: "v8" | "istanbul";
data: any;
};MetricsSummary
ts
export interface MetricsSummary {
covered: number;
uncovered?: number;
total: number;
pct: number | "";
status: "low" | "medium" | "high" | "unknown";
/** V8 lines only */
blank?: number;
/** V8 lines only */
comment?: number;
}CoverageSummary
ts
export interface CoverageSummary {
/** V8 only */
bytes?: MetricsSummary;
statements: MetricsSummary;
branches: MetricsSummary;
functions: MetricsSummary;
lines: MetricsSummary;
}CoverageRange
ts
/** V8 only */
export interface CoverageRange {
start: number;
end: number;
count: number;
/** ignored by special comment which starts with `v8 ignore` */
ignored?: boolean;
/**
* branch only, for example:
* there is only `if` branch but no `else` branch, then `none` will be true, it shows `else path uncovered`
*/
none?: boolean;
/** function only, function name */
name?: string;
}IgnoredRange
ts
export interface IgnoredRange {
start: number;
end: number;
/** ignore type: `next` or `range` */
type: string;
/** n lines for `next` type */
n?: number;
}CoverageFile
ts
export interface CoverageFile {
sourcePath: string;
summary: CoverageSummary;
/** V8 only */
url?: string;
/** V8 only */
id?: string;
/** V8 only */
type?: string;
/** V8 only */
source?: string;
/** V8 only */
distFile?: string;
/** V8 only */
js?: boolean;
/** V8 only */
data?: {
bytes?: CoverageRange[];
statements?: CoverageRange[];
branches?: CoverageRange[];
functions?: CoverageRange[];
ignores?: IgnoredRange[];
lines?: {
/**
* key: line number;
* number: hits (0 means uncovered);
* string: partial covered
*/
[key: string]: number | string;
};
/**
* V8 lines excluded from line coverage, keyed by 1-based line number.
* `b` = blank, `c` = comment-only, `i` = excluded by an ignore directive.
* Blank and comment-only lines keep their classification inside ignored ranges.
*/
extras?: {
[key: string]: "b" | "c" | "i";
}
}
}CoverageResults
ts
export type CoverageResults = {
type: "v8" | "istanbul";
reportPath: string;
version: string;
name: string;
watermarks: Watermarks;
summary: CoverageSummary;
files: CoverageFile[];
};TestError
ts
export interface TestError {
/** Fixed string fields; defaults are "Error" and "" for unavailable/non-string values. */
name: string;
message: string;
/** Own enumerable data properties only: strings, booleans, finite numbers,
* and null. BigInts become strings; objects, accessors and unsupported values
* are omitted. Missing properties read as undefined. Values may be sensitive.
*/
[key: string]: string | number | boolean | null | undefined;
}TestResult
ts
export interface TestResult {
name: string;
type: "test" | "suite";
nesting: number;
testNumber: number;
file?: string;
line?: number;
column?: number;
/** Duration in milliseconds. */
duration: number;
status: "passed" | "failed" | "cancelled" | "skipped" | "todo";
skip?: string | boolean;
todo?: string | boolean;
error?: TestError;
}TestSummary
ts
export interface TestSummary {
tests: number;
suites: number;
passed: number;
failed: number;
cancelled: number;
skipped: number;
todo: number;
/** Duration in milliseconds. */
duration: number;
success: boolean;
}TestResults
ts
export interface TestResults {
/** Unique collection ID, also used in test-results-<id>.json. */
id: string;
framework: "node:test";
/** Whether the runner emitted its final summary (not whether tests passed). */
complete: boolean;
/** Final runner counts, excluding suites from the test count; null if incomplete. */
summary: TestSummary | null;
/** Completed test and suite records, including nested tests. */
tests: TestResult[];
}LoggingType
ts
export type LoggingType = "off" | "error" | "info" | "debug";CoverageReportOptions
ts
export interface CoverageReportOptions {
/** {string} logging levels: off, error, info, debug */
logging?: LoggingType;
/** {string} Report name. Defaults to "Coverage Report". */
name?: string;
/**
*
* {string} 'v8', 'v8,console-details'
*
* {array} ['v8'], ['v8', ['console-details', { skipPercent: 80 }]]
*
* By default, `v8` for V8 data, `html` for Istanbul data
*/
reports?: string | (string | ReportDescription)[];
/** {string} output dir */
outputDir?: string;
/** {string|string[]} input raw dir(s) */
inputDir?: string | string[];
/** {string} base dir for normalizing the relative source path, defaults to cwd */
baseDir?: string;
/** {string} coverage data dir, alternative to method `addFromDir()`, defaults to null */
dataDir?: string;
/** (V8 only)
*
* {string} `minimatch` pattern for entry url;
* {object} multiple patterns;
* {function} A filter function for each entry file in the V8 list.
*/
entryFilter?: string | {
[pattern: string]: boolean;
} | ((entry: V8CoverageEntry) => boolean);
/** (V8 only)
*
* {string} `minimatch` pattern for source path;
* {object} multiple patterns;
* {function} A filter function for each source path when the source is unpacked from the source map.
*/
sourceFilter?: string | {
[pattern: string]: boolean;
} | ((sourcePath: string) => boolean);
/** (V8 only) Filter final files after sourcemap conversion and adding untested files.
* Applies to V8 reports and converted Istanbul reports. Defaults to null (disabled).
* Strings and object keys match sourcePath using the same rules as entryFilter and sourceFilter.
*/
outputFilter?: string | {
[pattern: string]: boolean;
} | ((entry: CoverageFile) => boolean) | null;
/**
* The combined filter for entryFilter and sourceFilter
*/
filter?: string | {
[pattern: string]: boolean;
} | ((input: string | V8CoverageEntry) => boolean);
/**
* {function} Source path handler.
*
* {object} Replace key with value.
* */
sourcePath?: ((filePath: string, info: {
/** the related dist file of current source file */
distFile?: string;
[key: string]: any;
}) => string) | {
[key: string]: string;
};
/** (V8 only) {string} Output [sub dir/]filename. Defaults to "index.html" */
outputFile?: string;
/** (V8 only) {boolean} Inline all scripts to the single HTML file. Defaults to false. */
inline?: boolean;
/** (V8 only) {string} Assets path if not inline. Defaults to "./assets" */
assetsPath?: string;
/** (Istanbul only) defaultSummarizer, sourceFinder */
/** {boolean} Generate lcov.info file, same as lcovonly report. Defaults to false. */
lcov?: boolean;
/**
* options for adding empty coverage for all files
*/
all?: string | string[] | {
/** the dir(s) of all files */
dir: string | string[];
/**
* the file filter is triggered before `sourceFilter`, no need to use it in normal case
* the filter could return the file type, if true, defaults to css if ".css" otherwise is js
* {string} `minimatch` pattern for file path; {object} multiple patterns; {function} A filter function for file path;
*/
filter?: string | {
[pattern: string]: "js" | "css" | boolean;
} | ((filePath: string) => "js" | "css" | boolean);
/**
* the file transformer for source and sourceMap
* some of untested files like .ts/.jsx/.vue can not be parsed to AST directly by acorn
* so this is the function which can transform the original source to generated source and sourceMap
*/
transformer?: (entry: any) => Promise<void>;
};
/** (V8 only) {boolean} Enable/Disable ignoring uncovered codes with the special comments: v8 ignore next/next N/start/stop */
v8Ignore?: boolean;
/** {string|function} Specify the report path, especially when there are multiple reports. Defaults to outputDir/index.html. */
reportPath?: string | (() => string);
/** {array} watermarks for low/medium/high. Defaults to [50, 80]
* {object} { bytes:[50,80], statements:[50,80], branches:[50,80], functions:[50,80], lines:[50,80] } */
watermarks?: Watermarks;
/**
* {boolean} Indicates whether to clean previous reports in output dir before generating new reports. Defaults to true.
*
* If true, the API `clean()` will execute automatically.
* */
clean?: boolean;
/**
* {boolean} Indicates whether to clean previous cache in output dir on start. Defaults to false.
*
* If true, the API `cleanCache()` will execute automatically.
*/
cleanCache?: boolean;
/**
* {number} JavaScript heapUsed threshold in MB
* for example: sets gc to 1024 means that force gc when the memory > 1024M at certain critical stages
* https://nodejs.org/docs/latest/api/v8.html#v8setflagsfromstringflags
*/
gc?: number;
/**
* {boolean} Indicates whether to save source and sourcemap file for debug (require logging="debug")
*/
sourceMap?: boolean;
/**
* {function} Custom resolver for sourcemap content
*/
sourceMapResolver?: (url: string, defaultResolver: Function) => Promise<any>;
/** (V8 only) {function} onEntry hook */
onEntry?: (entry: V8CoverageEntry) => void | Promise<void>;
/** {function} onEnd hook */
onEnd?: (coverageResults: CoverageResults | undefined) => void | Promise<void>;
[key: string]: any;
}McrCliOptions
ts
export interface McrCliOptions extends CoverageReportOptions {
/** Only used by `mcr test`. Test file paths or glob patterns accepted by node --test.
* A string is one positional argument; an array supplies multiple arguments unchanged.
* Node handles parsing and discovery relative to cwd; null is not passed to Node.
* Positional CLI targets override this option.
* Defaults to null (skip tests unless CLI targets are supplied).
* Ignored by other commands and the API.
*/
test?: string | string[] | null;
/** Optional hook for `mcr test`; results are collected even when omitted.
* Runs before publishing the test results cache, onReady and coverage generation,
* even when assertions fail. Mutate messages/names here to redact persisted raw/debug
* results. Hook errors prevent publication of test results but not coverage generation.
* Not called for skipped runs.
* Missing/invalid reports fail the command without calling this hook. An incomplete
* report has complete=false and summary=null, and also fails the command.
* Hook errors fail the command but do not prevent coverage generation.
*/
onTestEnd?: (testResults: TestResults) => void | Promise<void>;
/** (CLI only) {function} onStart hook */
onStart?: (coverageReport: CoverageReport) => void | Promise<void>;
/** (CLI only) {function} onReady hook before adding coverage data.
*
* Sometimes, the child process has not yet finished writing the coverage data, and it needs to wait here.
*/
onReady?: (coverageReport: CoverageReport, nodeV8CoverageDir: string, subprocess: any) => void | Promise<void>;
}CoverageReport
ts
export class CoverageReport {
/**
* @param options coverage report options
*/
constructor(options?: CoverageReportOptions);
/** add coverage data
*
* @param coverageData {array} V8 format, {object} Istanbul format */
add: (coverageData: any[] | any) => Promise<AddedResults | undefined>;
/**
* add V8 coverage from a dir
* Uses a single captured source directly, or matches multiple versions to Node processes and worker threads.
* Always selects one source for an existing URL; unresolved ties use the first candidate (best effort).
* Preserve the original Node coverage filenames and captured execution metadata when copying inputs.
* @param dir node v8 coverage dir
*/
addFromDir: (dir: string) => Promise<void>;
/** generate report */
generate: () => Promise<CoverageResults | undefined>;
/** check if cache exists */
hasCache: () => boolean;
/** clean previous cache, return `false` if no cache */
cleanCache: () => boolean;
/** clean previous reports except cache dir and v8 coverage dir */
clean: () => void;
/** get entry filter handler, it can be used to filter the coverage data list before adding it. */
getEntryFilter: () => ((entry: V8CoverageEntry) => boolean);
/** load config file.
*
* @param configFile custom config file path
* Throws if a custom config is missing or a discovered config fails to load.
* If no custom config is specified and no default config is found, returns normally.
*
* Supports loading default config file if no custom config specified:
'mcr.config.js',
'mcr.config.cjs',
'mcr.config.mjs',
'mcr.config.json',
'mcr.config.ts'
*/
loadConfig: (configFile?: string) => Promise<void>;
}CoverageSnapshot
ts
export interface CoverageSnapshot {
type: "v8" | "istanbul";
summary: {
bytes?: string;
statements: string;
branches: string;
functions: string;
lines: string;
},
files: {
[sourcePath: string]: {
bytes?: string;
statements: string;
branches: string;
functions: string;
lines: string;
uncoveredLines: string;
/**
* Comma-separated 1-based line numbers with their `CoverageFile.data.extras` codes.
* For example, `2b,12c,16i` means line 2 is blank, line 12 is comment-only,
* and line 16 is ignored. Empty when there are no entries (including Istanbul).
*/
extras: string;
}
}
}getSnapshot
ts
/** get snapshot from coverage report data */
export function getSnapshot(coverageResults: CoverageResults): CoverageSnapshot;diffSnapshot
ts
/** Diff two snapshots, including per-file uncovered lines and line classifications (`extras`). */
export function diffSnapshot(oldData: CoverageSnapshot, newData: CoverageSnapshot, diffOptions: {
skipEqual?: boolean;
showSummary?: boolean;
maxCols?: number;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
}): {
change: boolean;
results: any[];
message: string;
};CoverageClient
ts
export class CoverageClient {
/** Best-effort command: logs failures at debug level and resolves to null instead of rejecting. */
safeSend: (method: string, params?: any) => Promise<any>;
/** start js coverage */
startJSCoverage: () => Promise<void>;
/** stop and return js coverage */
stopJSCoverage: () => Promise<V8CoverageEntry[]>;
/** start css coverage */
startCSSCoverage: () => Promise<void>;
/** stop and return css coverage */
stopCSSCoverage: () => Promise<V8CoverageEntry[]>;
/** start both js and css coverage */
startCoverage: () => Promise<void>;
/** stop and return both js and css coverage */
stopCoverage: () => Promise<V8CoverageEntry[]>;
/** write the coverage started by NODE_V8_COVERAGE to disk on demand, returns v8 coverage dir */
writeCoverage: () => Promise<string>;
/** get istanbul coverage data
* @param coverageKey defaults to `__coverage__`
*/
getIstanbulCoverage: (coverageKey?: string) => Promise<any>;
close: () => Promise<void>
}CDPSession
ts
/** Adapt to the CDPSession of Playwright or Puppeteer */
export interface CDPSession {
send: (method: string, params?: any) => Promise<any>;
on: (type: string, handler: (e: any) => void) => void;
detach: () => Promise<void>;
}WSSession
ts
/** custom websocket CDPSession */
export class WSSession implements CDPSession {
/** Request timeout defaults to 30000ms; 0 disables the deadline. */
constructor(ws: any, options?: { timeout?: number });
send: (method: string, params?: any) => Promise<any>;
on: (type: string, handler: (e: any) => void) => void;
detach: () => Promise<void>;
}CDPOptions
ts
export interface CDPOptions {
/** Adapt to the CDPSession of Playwright or Puppeteer */
session?: CDPSession;
/** websocket debugger url */
url?: string;
/** debugger port, defaults to 9222 */
port?: number;
/** debugger host, defaults to localhost */
host?: string;
/** enable https, defaults to false (http) */
secure?: boolean;
/** target filter, defaults to first page */
target?: (targets: any) => any;
/** websocket options */
ws?: any;
/** HTTP discovery / WebSocket connection timeout, defaults to 10000ms; 0 disables it. */
timeout?: number;
/** Per-command timeout for built-in WebSocket sessions, defaults to 30000ms; 0 disables it. */
requestTimeout?: number;
}CDPClient
ts
export function CDPClient(cdpOptions: CDPOptions): Promise<CoverageClient | undefined>;Util
ts
export const Util: {
initLoggingLevel: (logging: LoggingType) => string;
};MCR
ts
/** create coverage report */
declare function MCR(options?: MCR.CoverageReportOptions): MCR.CoverageReport;