vitest
Version:
Next generation testing framework powered by Vite
1,612 lines (1,550 loc) • 99.6 kB
TypeScript
import { Task, TaskMeta, Suite, File, TestAnnotation, ImportDuration, TaskResultPack, TaskEventPack, SequenceSetupFiles, SequenceHooks, CancelReason } from '@vitest/runner';
import { b as Awaitable, U as UserConsoleLog, c as Arrayable$1, A as AfterSuiteRunMeta, L as LabelColor, f as EnvironmentOptions, P as ProvidedContext } from './environment.d.cL3nLXbE.js';
import { ParsedStack, TestError, SerializedError, ErrorWithDiff, Arrayable, Awaitable as Awaitable$1 } from '@vitest/utils';
import { Writable } from 'node:stream';
import { TransformResult as TransformResult$1, UserConfig as UserConfig$1, DepOptimizationConfig, ServerOptions, ConfigEnv, AliasOptions, ViteDevServer, ModuleNode } from 'vite';
import { Console } from 'node:console';
import { MockedModule } from '@vitest/mocker';
import { StackTraceParserOptions } from '@vitest/utils/source-map';
import { T as TestExecutionMethod } from './worker.d.tQu2eJQy.js';
import { a as SerializedConfig, F as FakeTimerInstallOpts } from './config.d.D2ROskhv.js';
import { PrettyFormatOptions } from '@vitest/pretty-format';
import { SnapshotSummary, SnapshotStateOptions } from '@vitest/snapshot';
import { SerializedDiffOptions } from '@vitest/utils/diff';
import { ViteNodeServerOptions } from 'vite-node';
import * as chai from 'chai';
import { B as BenchmarkResult } from './benchmark.d.BwvBVTda.js';
import { a as RuntimeCoverageProviderModule } from './coverage.d.S9RMNXIe.js';
import { SnapshotManager } from '@vitest/snapshot/manager';
import { Stats } from 'node:fs';
declare class TypeCheckError extends Error {
message: string;
stacks: ParsedStack[];
name: string;
constructor(message: string, stacks: ParsedStack[]);
}
interface ErrorOptions {
type?: string;
fullStack?: boolean;
project?: TestProject;
verbose?: boolean;
screenshotPaths?: string[];
task?: Task;
showCodeFrame?: boolean;
}
type Listener = () => void;
declare class Logger {
ctx: Vitest;
outputStream: NodeJS.WriteStream | Writable;
errorStream: NodeJS.WriteStream | Writable;
private _clearScreenPending;
private _highlights;
private cleanupListeners;
console: Console;
constructor(ctx: Vitest, outputStream?: NodeJS.WriteStream | Writable, errorStream?: NodeJS.WriteStream | Writable);
log(...args: any[]): void;
error(...args: any[]): void;
warn(...args: any[]): void;
clearFullScreen(message?: string): void;
clearScreen(message: string, force?: boolean): void;
private _clearScreen;
printError(err: unknown, options?: ErrorOptions): void;
deprecate(message: string): void;
clearHighlightCache(filename?: string): void;
highlight(filename: string, source: string): string;
printNoTestFound(filters?: string[]): void;
printBanner(): void;
printBrowserBanner(project: TestProject): void;
printUnhandledErrors(errors: unknown[]): void;
printSourceTypeErrors(errors: TypeCheckError[]): void;
getColumns(): number;
onTerminalCleanup(listener: Listener): void;
private addCleanupListeners;
private registerUnhandledRejection;
}
type SerializedTestSpecification = [project: {
name: string | undefined
root: string
}, file: string, options: {
pool: string
testLines?: number[] | undefined
}];
declare class ReportedTaskImplementation {
/**
* The project associated with the test or suite.
*/
readonly project: TestProject;
/**
* Unique identifier.
* This ID is deterministic and will be the same for the same test across multiple runs.
* The ID is based on the project name, module url and test order.
*/
readonly id: string;
/**
* Location in the module where the test or suite is defined.
*/
readonly location: {
line: number
column: number
} | undefined;
/**
* Checks if the test did not fail the suite.
* If the test is not finished yet or was skipped, it will return `true`.
*/
ok(): boolean;
/**
* Custom metadata that was attached to the test during its execution.
*/
meta(): TaskMeta;
}
declare class TestCase extends ReportedTaskImplementation {
#private;
readonly type = "test";
/**
* Direct reference to the test module where the test or suite is defined.
*/
readonly module: TestModule;
/**
* Name of the test.
*/
readonly name: string;
/**
* Options that the test was initiated with.
*/
readonly options: TaskOptions;
/**
* Parent suite. If the test was called directly inside the module, the parent will be the module itself.
*/
readonly parent: TestSuite | TestModule;
/**
* Full name of the test including all parent suites separated with `>`.
*/
get fullName(): string;
/**
* Test results.
* - **pending**: Test was collected, but didn't finish running yet.
* - **passed**: Test passed successfully
* - **failed**: Test failed to execute
* - **skipped**: Test was skipped during collection or dynamically with `ctx.skip()`.
*/
result(): TestResult;
/**
* Test annotations added via the `task.annotate` API during the test execution.
*/
annotations(): ReadonlyArray<TestAnnotation>;
/**
* Useful information about the test like duration, memory usage, etc.
* Diagnostic is only available after the test has finished.
*/
diagnostic(): TestDiagnostic | undefined;
}
declare class TestCollection {
#private;
constructor(task: Suite | File, project: TestProject);
/**
* Returns the test or suite at a specific index.
*/
at(index: number): TestCase | TestSuite | undefined;
/**
* The number of tests and suites in the collection.
*/
get size(): number;
/**
* Returns the collection in array form for easier manipulation.
*/
array(): (TestCase | TestSuite)[];
/**
* Filters all tests that are part of this collection and its children.
*/
allTests(state?: TestState): Generator<TestCase, undefined, void>;
/**
* Filters only the tests that are part of this collection.
*/
tests(state?: TestState): Generator<TestCase, undefined, void>;
/**
* Filters only the suites that are part of this collection.
*/
suites(): Generator<TestSuite, undefined, void>;
/**
* Filters all suites that are part of this collection and its children.
*/
allSuites(): Generator<TestSuite, undefined, void>;
[Symbol.iterator](): Generator<TestSuite | TestCase, undefined, void>;
}
type ReportedHookContext = {
readonly name: "beforeAll" | "afterAll"
readonly entity: TestSuite | TestModule
} | {
readonly name: "beforeEach" | "afterEach"
readonly entity: TestCase
};
declare abstract class SuiteImplementation extends ReportedTaskImplementation {
/**
* Collection of suites and tests that are part of this suite.
*/
readonly children: TestCollection;
/**
* Errors that happened outside of the test run during collection, like syntax errors.
*/
errors(): SerializedError[];
}
declare class TestSuite extends SuiteImplementation {
#private;
readonly type = "suite";
/**
* Name of the test or the suite.
*/
readonly name: string;
/**
* Direct reference to the test module where the test or suite is defined.
*/
readonly module: TestModule;
/**
* Parent suite. If suite was called directly inside the module, the parent will be the module itself.
*/
readonly parent: TestSuite | TestModule;
/**
* Options that suite was initiated with.
*/
readonly options: TaskOptions;
/**
* Checks if the suite has any failed tests.
* This will also return `false` if suite failed during collection.
*/
ok: () => boolean;
/**
* The meta information attached to the suite during its collection or execution.
*/
meta: () => TaskMeta;
/**
* Checks the running state of the suite.
*/
state(): TestSuiteState;
/**
* Full name of the suite including all parent suites separated with `>`.
*/
get fullName(): string;
}
declare class TestModule extends SuiteImplementation {
readonly location: undefined;
readonly type = "module";
/**
* This is usually an absolute UNIX file path.
* It can be a virtual ID if the file is not on the disk.
* This value corresponds to the ID in the Vite's module graph.
*/
readonly moduleId: string;
/**
* Checks the running state of the test file.
*/
state(): TestModuleState;
/**
* Checks if the module has any failed tests.
* This will also return `false` if module failed during collection.
*/
ok: () => boolean;
/**
* The meta information attached to the module during its collection or execution.
*/
meta: () => TaskMeta;
/**
* Useful information about the module like duration, memory usage, etc.
* If the module was not executed yet, all diagnostic values will return `0`.
*/
diagnostic(): ModuleDiagnostic;
}
interface TaskOptions {
readonly each: boolean | undefined;
readonly fails: boolean | undefined;
readonly concurrent: boolean | undefined;
readonly shuffle: boolean | undefined;
readonly retry: number | undefined;
readonly repeats: number | undefined;
readonly mode: "run" | "only" | "skip" | "todo";
}
type TestSuiteState = "skipped" | "pending" | "failed" | "passed";
type TestModuleState = TestSuiteState | "queued";
type TestState = TestResult["state"];
type TestResult = TestResultPassed | TestResultFailed | TestResultSkipped | TestResultPending;
interface TestResultPending {
/**
* The test was collected, but didn't finish running yet.
*/
readonly state: "pending";
/**
* Pending tests have no errors.
*/
readonly errors: undefined;
}
interface TestResultPassed {
/**
* The test passed successfully.
*/
readonly state: "passed";
/**
* Errors that were thrown during the test execution.
*
* **Note**: If test was retried successfully, errors will still be reported.
*/
readonly errors: ReadonlyArray<TestError> | undefined;
}
interface TestResultFailed {
/**
* The test failed to execute.
*/
readonly state: "failed";
/**
* Errors that were thrown during the test execution.
*/
readonly errors: ReadonlyArray<TestError>;
}
interface TestResultSkipped {
/**
* The test was skipped with `only` (on another test), `skip` or `todo` flag.
* You can see which one was used in the `options.mode` option.
*/
readonly state: "skipped";
/**
* Skipped tests have no errors.
*/
readonly errors: undefined;
/**
* A custom note passed down to `ctx.skip(note)`.
*/
readonly note: string | undefined;
}
interface TestDiagnostic {
/**
* If the duration of the test is above `slowTestThreshold`.
*/
readonly slow: boolean;
/**
* The amount of memory used by the test in bytes.
* This value is only available if the test was executed with `logHeapUsage` flag.
*/
readonly heap: number | undefined;
/**
* The time it takes to execute the test in ms.
*/
readonly duration: number;
/**
* The time in ms when the test started.
*/
readonly startTime: number;
/**
* The amount of times the test was retried.
*/
readonly retryCount: number;
/**
* The amount of times the test was repeated as configured by `repeats` option.
* This value can be lower if the test failed during the repeat and no `retry` is configured.
*/
readonly repeatCount: number;
/**
* If test passed on a second retry.
*/
readonly flaky: boolean;
}
interface ModuleDiagnostic {
/**
* The time it takes to import and initiate an environment.
*/
readonly environmentSetupDuration: number;
/**
* The time it takes Vitest to setup test harness (runner, mocks, etc.).
*/
readonly prepareDuration: number;
/**
* The time it takes to import the test module.
* This includes importing everything in the module and executing suite callbacks.
*/
readonly collectDuration: number;
/**
* The time it takes to import the setup module.
*/
readonly setupDuration: number;
/**
* Accumulated duration of all tests and hooks in the module.
*/
readonly duration: number;
/**
* The amount of memory used by the test module in bytes.
* This value is only available if the test was executed with `logHeapUsage` flag.
*/
readonly heap: number | undefined;
/**
* The time spent importing every non-externalized dependency that Vitest has processed.
*/
readonly importDurations: Record<string, ImportDuration>;
}
type BuiltinPool = "browser" | "threads" | "forks" | "vmThreads" | "vmForks" | "typescript";
type Pool = BuiltinPool | (string & {});
interface PoolOptions extends Record<string, unknown> {
/**
* Run tests in `node:worker_threads`.
*
* Test isolation (when enabled) is done by spawning a new thread for each test file.
*
* This pool is used by default.
*/
threads?: ThreadsOptions & WorkerContextOptions;
/**
* Run tests in `node:child_process` using [`fork()`](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
*
* Test isolation (when enabled) is done by spawning a new child process for each test file.
*/
forks?: ForksOptions & WorkerContextOptions;
/**
* Run tests in isolated `node:vm`.
* Test files are run parallel using `node:worker_threads`.
*
* This makes tests run faster, but VM module is unstable. Your tests might leak memory.
*/
vmThreads?: ThreadsOptions & VmOptions;
/**
* Run tests in isolated `node:vm`.
*
* Test files are run parallel using `node:child_process` [`fork()`](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
*
* This makes tests run faster, but VM module is unstable. Your tests might leak memory.
*/
vmForks?: ForksOptions & VmOptions;
}
interface ResolvedPoolOptions extends PoolOptions {
threads?: ResolvedThreadsOptions & WorkerContextOptions;
forks?: ResolvedForksOptions & WorkerContextOptions;
vmThreads?: ResolvedThreadsOptions & VmOptions;
vmForks?: ResolvedForksOptions & VmOptions;
}
interface ThreadsOptions {
/** Minimum amount of threads to use */
minThreads?: number | string;
/** Maximum amount of threads to use */
maxThreads?: number | string;
/**
* Run tests inside a single thread.
*
* @default false
*/
singleThread?: boolean;
/**
* Use Atomics to synchronize threads
*
* This can improve performance in some cases, but might cause segfault in older Node versions.
*
* @default false
*/
useAtomics?: boolean;
}
interface ResolvedThreadsOptions extends ThreadsOptions {
minThreads?: number;
maxThreads?: number;
}
interface ForksOptions {
/** Minimum amount of child processes to use */
minForks?: number | string;
/** Maximum amount of child processes to use */
maxForks?: number | string;
/**
* Run tests inside a single fork.
*
* @default false
*/
singleFork?: boolean;
}
interface ResolvedForksOptions extends ForksOptions {
minForks?: number;
maxForks?: number;
}
interface WorkerContextOptions {
/**
* Isolate test environment by recycling `worker_threads` or `child_process` after each test
*
* @default true
*/
isolate?: boolean;
/**
* Pass additional arguments to `node` process when spawning `worker_threads` or `child_process`.
*
* See [Command-line API | Node.js](https://nodejs.org/docs/latest/api/cli.html) for more information.
*
* Set to `process.execArgv` to pass all arguments of the current process.
*
* Be careful when using, it as some options may crash worker, e.g. --prof, --title. See https://github.com/nodejs/node/issues/41103
*
* @default [] // no execution arguments are passed
*/
execArgv?: string[];
}
interface VmOptions {
/**
* Specifies the memory limit for `worker_thread` or `child_process` before they are recycled.
* If you see memory leaks, try to tinker this value.
*/
memoryLimit?: string | number;
/** Isolation is always enabled */
isolate?: true;
/**
* Pass additional arguments to `node` process when spawning `worker_threads` or `child_process`.
*
* See [Command-line API | Node.js](https://nodejs.org/docs/latest/api/cli.html) for more information.
*
* Set to `process.execArgv` to pass all arguments of the current process.
*
* Be careful when using, it as some options may crash worker, e.g. --prof, --title. See https://github.com/nodejs/node/issues/41103
*
* @default [] // no execution arguments are passed
*/
execArgv?: string[];
}
declare class TestSpecification {
/**
* @deprecated use `project` instead
*/
readonly 0: TestProject;
/**
* @deprecated use `moduleId` instead
*/
readonly 1: string;
/**
* @deprecated use `pool` instead
*/
readonly 2: {
pool: Pool
};
/**
* The task ID associated with the test module.
*/
readonly taskId: string;
/**
* The test project that the module belongs to.
*/
readonly project: TestProject;
/**
* The ID of the module in the Vite module graph. It is usually an absolute file path.
*/
readonly moduleId: string;
/**
* The current test pool. It's possible to have multiple pools in a single test project with `poolMatchGlob` and `typecheck.enabled`.
* @experimental In Vitest 4, the project will only support a single pool and this property will be removed.
*/
readonly pool: Pool;
/**
* Line numbers of the test locations to run.
*/
readonly testLines: number[] | undefined;
constructor(project: TestProject, moduleId: string, pool: Pool, testLines?: number[] | undefined);
/**
* Test module associated with the specification.
*/
get testModule(): TestModule | undefined;
toJSON(): SerializedTestSpecification;
/**
* for backwards compatibility
* @deprecated
*/
[Symbol.iterator](): Generator<string | TestProject, void, unknown>;
}
type TestRunEndReason = "passed" | "interrupted" | "failed";
interface Reporter {
onInit?: (vitest: Vitest) => void;
/**
* Called when the project initiated the browser instance.
* project.browser will always be defined.
* @experimental
*/
onBrowserInit?: (project: TestProject) => Awaitable<void>;
/**
* @deprecated use `onTestRunStart` instead
*/
onPathsCollected?: (paths?: string[]) => Awaitable<void>;
/**
* @deprecated use `onTestRunStart` instead
*/
onSpecsCollected?: (specs?: SerializedTestSpecification[]) => Awaitable<void>;
/**
* @deprecated use `onTestModuleCollected` instead
*/
onCollected?: (files: File[]) => Awaitable<void>;
/**
* @deprecated use `onTestRunEnd` instead
*/
onFinished?: (files: File[], errors: unknown[], coverage?: unknown) => Awaitable<void>;
/**
* @deprecated use `onTestModuleQueued`, `onTestModuleStart`, `onTestModuleEnd`, `onTestCaseReady`, `onTestCaseResult` instead
*/
onTaskUpdate?: (packs: TaskResultPack[], events: TaskEventPack[]) => Awaitable<void>;
onTestRemoved?: (trigger?: string) => Awaitable<void>;
onWatcherStart?: (files?: File[], errors?: unknown[]) => Awaitable<void>;
onWatcherRerun?: (files: string[], trigger?: string) => Awaitable<void>;
onServerRestart?: (reason?: string) => Awaitable<void>;
onUserConsoleLog?: (log: UserConsoleLog) => Awaitable<void>;
onProcessTimeout?: () => Awaitable<void>;
/**
* Called when the new test run starts.
*/
onTestRunStart?: (specifications: ReadonlyArray<TestSpecification>) => Awaitable<void>;
/**
* Called when the test run is finished.
*/
onTestRunEnd?: (testModules: ReadonlyArray<TestModule>, unhandledErrors: ReadonlyArray<SerializedError>, reason: TestRunEndReason) => Awaitable<void>;
/**
* Called when the module is enqueued for testing. The file itself is not loaded yet.
*/
onTestModuleQueued?: (testModule: TestModule) => Awaitable<void>;
/**
* Called when the test file is loaded and the module is ready to run tests.
*/
onTestModuleCollected?: (testModule: TestModule) => Awaitable<void>;
/**
* Called when starting to run tests of the test file
*/
onTestModuleStart?: (testModule: TestModule) => Awaitable<void>;
/**
* Called when all tests of the test file have finished running.
*/
onTestModuleEnd?: (testModule: TestModule) => Awaitable<void>;
/**
* Called when test case is ready to run.
* Called before the `beforeEach` hooks for the test are run.
*/
onTestCaseReady?: (testCase: TestCase) => Awaitable<void>;
/**
* Called after the test and its hooks are finished running.
* The `result()` cannot be `pending`.
*/
onTestCaseResult?: (testCase: TestCase) => Awaitable<void>;
/**
* Called when annotation is added via the `task.annotate` API.
*/
onTestCaseAnnotate?: (testCase: TestCase, annotation: TestAnnotation) => Awaitable<void>;
/**
* Called when test suite is ready to run.
* Called before the `beforeAll` hooks for the test are run.
*/
onTestSuiteReady?: (testSuite: TestSuite) => Awaitable<void>;
/**
* Called after the test suite and its hooks are finished running.
* The `state` cannot be `pending`.
*/
onTestSuiteResult?: (testSuite: TestSuite) => Awaitable<void>;
/**
* Called before the hook starts to run.
*/
onHookStart?: (hook: ReportedHookContext) => Awaitable<void>;
/**
* Called after the hook finished running.
*/
onHookEnd?: (hook: ReportedHookContext) => Awaitable<void>;
onCoverage?: (coverage: unknown) => Awaitable<void>;
}
interface BaseOptions {
isTTY?: boolean;
}
declare abstract class BaseReporter implements Reporter {
start: number;
end: number;
watchFilters?: string[];
failedUnwatchedFiles: TestModule[];
isTTY: boolean;
ctx: Vitest;
renderSucceed: boolean;
protected verbose: boolean;
private _filesInWatchMode;
private _timeStart;
constructor(options?: BaseOptions);
onInit(ctx: Vitest): void;
log(...messages: any): void;
error(...messages: any): void;
relative(path: string): string;
onFinished(files?: File[], errors?: unknown[]): void;
onTestCaseResult(testCase: TestCase): void;
onTestSuiteResult(testSuite: TestSuite): void;
onTestModuleEnd(testModule: TestModule): void;
private logFailedTask;
protected printTestModule(testModule: TestModule): void;
protected printTestCase(moduleState: TestModuleState, test: TestCase): void;
private getModuleLog;
protected printTestSuite(_suite: TestSuite): void;
protected getTestName(test: Task, separator?: string): string;
protected formatShortError(error: ErrorWithDiff): string;
protected getTestIndentation(_test: Task): string;
protected printAnnotations(test: TestCase, console: "log" | "error", padding?: number): void;
protected getDurationPrefix(task: Task): string;
onWatcherStart(files?: File[], errors?: unknown[]): void;
onWatcherRerun(files: string[], trigger?: string): void;
onUserConsoleLog(log: UserConsoleLog, taskState?: TestResult["state"]): void;
onTestRemoved(trigger?: string): void;
shouldLog(log: UserConsoleLog, taskState?: TestResult["state"]): boolean;
onServerRestart(reason?: string): void;
reportSummary(files: File[], errors: unknown[]): void;
reportTestSummary(files: File[], errors: unknown[]): void;
private printErrorsSummary;
reportBenchmarkSummary(files: File[]): void;
private printTaskErrors;
}
interface BlobOptions {
outputFile?: string;
}
declare class BlobReporter implements Reporter {
start: number;
ctx: Vitest;
options: BlobOptions;
constructor(options: BlobOptions);
onInit(ctx: Vitest): void;
onFinished(files: File[] | undefined, errors: unknown[] | undefined, coverage: unknown): Promise<void>;
}
interface MergedBlobs {
files: File[];
errors: unknown[];
coverages: unknown[];
executionTimes: number[];
}
interface DefaultReporterOptions extends BaseOptions {
summary?: boolean;
}
declare class DefaultReporter extends BaseReporter {
private options;
private summary?;
constructor(options?: DefaultReporterOptions);
onTestRunStart(specifications: ReadonlyArray<TestSpecification>): void;
onTestModuleQueued(file: TestModule): void;
onTestModuleCollected(module: TestModule): void;
onTestModuleEnd(module: TestModule): void;
onTestCaseReady(test: TestCase): void;
onTestCaseResult(test: TestCase): void;
onHookStart(hook: ReportedHookContext): void;
onHookEnd(hook: ReportedHookContext): void;
onInit(ctx: Vitest): void;
onTestRunEnd(): void;
}
interface HTMLOptions {
outputFile?: string;
}
interface CoverageSummaryData {
lines: Totals;
statements: Totals;
branches: Totals;
functions: Totals;
}
declare class CoverageSummary {
constructor(data: CoverageSummary | CoverageSummaryData);
merge(obj: CoverageSummary): CoverageSummary;
toJSON(): CoverageSummaryData;
isEmpty(): boolean;
data: CoverageSummaryData;
lines: Totals;
statements: Totals;
branches: Totals;
functions: Totals;
}
interface CoverageMapData {
[key: string]: FileCoverage | FileCoverageData;
}
declare class CoverageMap {
constructor(data: CoverageMapData | CoverageMap);
addFileCoverage(pathOrObject: string | FileCoverage | FileCoverageData): void;
files(): string[];
fileCoverageFor(filename: string): FileCoverage;
filter(callback: (key: string) => boolean): void;
getCoverageSummary(): CoverageSummary;
merge(data: CoverageMapData | CoverageMap): void;
toJSON(): CoverageMapData;
data: CoverageMapData;
}
interface Location {
line: number;
column: number;
}
interface Range {
start: Location;
end: Location;
}
interface BranchMapping {
loc: Range;
type: string;
locations: Range[];
line: number;
}
interface FunctionMapping {
name: string;
decl: Range;
loc: Range;
line: number;
}
interface FileCoverageData {
path: string;
statementMap: { [key: string]: Range };
fnMap: { [key: string]: FunctionMapping };
branchMap: { [key: string]: BranchMapping };
s: { [key: string]: number };
f: { [key: string]: number };
b: { [key: string]: number[] };
}
interface Totals {
total: number;
covered: number;
skipped: number;
pct: number;
}
interface Coverage {
covered: number;
total: number;
coverage: number;
}
declare class FileCoverage implements FileCoverageData {
constructor(data: string | FileCoverage | FileCoverageData);
merge(other: FileCoverageData): void;
getBranchCoverageByLine(): { [line: number]: Coverage };
getLineCoverage(): { [line: number]: number };
getUncoveredLines(): number[];
resetHits(): void;
computeBranchTotals(): Totals;
computeSimpleTotals(): Totals;
toSummary(): CoverageSummary;
toJSON(): object;
data: FileCoverageData;
path: string;
statementMap: { [key: string]: Range };
fnMap: { [key: string]: FunctionMapping };
branchMap: { [key: string]: BranchMapping };
s: { [key: string]: number };
f: { [key: string]: number };
b: { [key: string]: number[] };
}
// for compatibility reasons, the reporter produces a JSON similar to the one produced by the Jest JSON reporter
// the following types are extracted from the Jest repository (and simplified)
// the commented-out fields are the missing ones
type Status = "passed" | "failed" | "skipped" | "pending" | "todo" | "disabled";
type Milliseconds = number;
interface Callsite {
line: number;
column: number;
}
interface JsonAssertionResult {
ancestorTitles: Array<string>;
fullName: string;
status: Status;
title: string;
meta: TaskMeta;
duration?: Milliseconds | null;
failureMessages: Array<string> | null;
location?: Callsite | null;
}
interface JsonTestResult {
message: string;
name: string;
status: "failed" | "passed";
startTime: number;
endTime: number;
assertionResults: Array<JsonAssertionResult>;
}
interface JsonTestResults {
numFailedTests: number;
numFailedTestSuites: number;
numPassedTests: number;
numPassedTestSuites: number;
numPendingTests: number;
numPendingTestSuites: number;
numTodoTests: number;
numTotalTests: number;
numTotalTestSuites: number;
startTime: number;
success: boolean;
testResults: Array<JsonTestResult>;
snapshot: SnapshotSummary;
coverageMap?: CoverageMap | null | undefined;
}
interface JsonOptions$1 {
outputFile?: string;
}
declare class JsonReporter implements Reporter {
start: number;
ctx: Vitest;
options: JsonOptions$1;
constructor(options: JsonOptions$1);
onInit(ctx: Vitest): void;
protected logTasks(files: File[], coverageMap?: CoverageMap | null): Promise<void>;
onFinished(files?: File[], _errors?: unknown[], coverageMap?: unknown): Promise<void>;
/**
* Writes the report to an output file if specified in the config,
* or logs it to the console otherwise.
* @param report
*/
writeReport(report: string): Promise<void>;
}
interface ClassnameTemplateVariables {
filename: string;
filepath: string;
}
interface JUnitOptions {
outputFile?: string;
/** @deprecated Use `classnameTemplate` instead. */
classname?: string;
/**
* Template for the classname attribute. Can be either a string or a function. The string can contain placeholders {filename} and {filepath}.
*/
classnameTemplate?: string | ((classnameVariables: ClassnameTemplateVariables) => string);
suiteName?: string;
/**
* Write <system-out> and <system-err> for console output
* @default true
*/
includeConsoleOutput?: boolean;
/**
* Add <testcase file="..."> attribute (validated on CIRCLE CI and GitLab CI)
* @default false
*/
addFileAttribute?: boolean;
}
declare class JUnitReporter implements Reporter {
private ctx;
private reportFile?;
private baseLog;
private logger;
private _timeStart;
private fileFd?;
private options;
constructor(options: JUnitOptions);
onInit(ctx: Vitest): Promise<void>;
writeElement(name: string, attrs: Record<string, any>, children: () => Promise<void>): Promise<void>;
writeLogs(task: Task, type: "err" | "out"): Promise<void>;
writeTasks(tasks: Task[], filename: string): Promise<void>;
onFinished(files?: File[]): Promise<void>;
}
declare class BasicReporter extends BaseReporter {
constructor();
onInit(ctx: Vitest): void;
reportSummary(files: File[], errors: unknown[]): void;
}
declare class DotReporter extends BaseReporter {
private renderer?;
private tests;
private finishedTests;
onInit(ctx: Vitest): void;
// Ignore default logging of base reporter
printTestModule(): void;
onWatcherRerun(files: string[], trigger?: string): void;
onFinished(files?: File[], errors?: unknown[]): void;
onTestModuleCollected(module: TestModule): void;
onTestCaseReady(test: TestCase): void;
onTestCaseResult(test: TestCase): void;
onTestModuleEnd(testModule: TestModule): void;
private createSummary;
}
interface GithubActionsReporterOptions {
onWritePath?: (path: string) => string;
}
declare class GithubActionsReporter implements Reporter {
ctx: Vitest;
options: GithubActionsReporterOptions;
constructor(options?: GithubActionsReporterOptions);
onInit(ctx: Vitest): void;
onTestCaseAnnotate(testCase: TestCase, annotation: TestAnnotation): void;
onFinished(files?: File[], errors?: unknown[]): void;
}
declare class HangingProcessReporter implements Reporter {
whyRunning: (() => void) | undefined;
onInit(): void;
onProcessTimeout(): void;
}
declare class TapReporter implements Reporter {
protected ctx: Vitest;
private logger;
onInit(ctx: Vitest): void;
static getComment(task: Task): string;
private logErrorDetails;
protected logTasks(tasks: Task[]): void;
onFinished(files?: File[]): void;
}
declare class TapFlatReporter extends TapReporter {
onInit(ctx: Vitest): void;
onFinished(files?: File[]): void;
}
declare class VerboseReporter extends DefaultReporter {
protected verbose: boolean;
renderSucceed: boolean;
printTestModule(module: TestModule): void;
onTestCaseResult(test: TestCase): void;
protected printTestSuite(testSuite: TestSuite): void;
protected getTestName(test: Task): string;
protected getTestIndentation(test: Task): string;
protected formatShortError(): string;
}
type FormattedBenchmarkResult = BenchmarkResult & {
id: string
};
declare function renderTable(options: {
tasks: Task[]
level: number
shallow?: boolean
showHeap: boolean
columns: number
slowTestThreshold: number
compare?: Record<Task["id"], FormattedBenchmarkResult>
}): string;
declare class BenchmarkReporter extends DefaultReporter {
compare?: Parameters<typeof renderTable>[0]["compare"];
onInit(ctx: Vitest): Promise<void>;
onTaskUpdate(packs: TaskResultPack[]): void;
onTestSuiteResult(testSuite: TestSuite): void;
protected printTestModule(testModule: TestModule): void;
private printSuiteTable;
onFinished(files?: File[], errors?: unknown[]): Promise<void>;
}
declare class VerboseBenchmarkReporter extends BenchmarkReporter {
protected verbose: boolean;
}
declare const BenchmarkReportsMap: {
default: typeof BenchmarkReporter
verbose: typeof VerboseBenchmarkReporter
};
type BenchmarkBuiltinReporters = keyof typeof BenchmarkReportsMap;
declare const ReportersMap: {
"default": typeof DefaultReporter
"basic": typeof BasicReporter
"blob": typeof BlobReporter
"verbose": typeof VerboseReporter
"dot": typeof DotReporter
"json": typeof JsonReporter
"tap": typeof TapReporter
"tap-flat": typeof TapFlatReporter
"junit": typeof JUnitReporter
"hanging-process": typeof HangingProcessReporter
"github-actions": typeof GithubActionsReporter
};
type BuiltinReporters = keyof typeof ReportersMap;
interface BuiltinReporterOptions {
"default": DefaultReporterOptions;
"basic": BaseOptions;
"verbose": DefaultReporterOptions;
"dot": BaseOptions;
"json": JsonOptions$1;
"blob": BlobOptions;
"tap": never;
"tap-flat": never;
"junit": JUnitOptions;
"hanging-process": never;
"html": HTMLOptions;
}
interface BrowserTesterOptions {
method: TestExecutionMethod;
files: string[];
providedContext: string;
startTime: number;
}
type ChaiConfig = Omit<Partial<typeof chai.config>, "useProxy" | "proxyExcludedKeys">;
interface TestSequencer {
/**
* Slicing tests into shards. Will be run before `sort`.
* Only run, if `shard` is defined.
*/
shard: (files: TestSpecification[]) => Awaitable<TestSpecification[]>;
sort: (files: TestSpecification[]) => Awaitable<TestSpecification[]>;
}
interface TestSequencerConstructor {
new (ctx: Vitest): TestSequencer;
}
interface WatcherTriggerPattern {
pattern: RegExp;
testsToRun: (file: string, match: RegExpMatchArray) => string[] | string | null | undefined | void;
}
interface BenchmarkUserOptions {
/**
* Include globs for benchmark test files
*
* @default ['**\/*.{bench,benchmark}.?(c|m)[jt]s?(x)']
*/
include?: string[];
/**
* Exclude globs for benchmark test files
* @default ['**\/node_modules/**', '**\/dist/**', '**\/cypress/**', '**\/.{idea,git,cache,output,temp}/**', '**\/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*']
*/
exclude?: string[];
/**
* Include globs for in-source benchmark test files
*
* @default []
*/
includeSource?: string[];
/**
* Custom reporter for output. Can contain one or more built-in report names, reporter instances,
* and/or paths to custom reporters
*
* @default ['default']
*/
reporters?: Arrayable<BenchmarkBuiltinReporters | Reporter>;
/**
* @deprecated Use `benchmark.outputJson` instead
*/
outputFile?: string | (Partial<Record<BenchmarkBuiltinReporters, string>> & Record<string, string>);
/**
* benchmark output file to compare against
*/
compare?: string;
/**
* benchmark output file
*/
outputJson?: string;
/**
* Include `samples` array of benchmark results for API or custom reporter usages.
* This is disabled by default to reduce memory usage.
* @default false
*/
includeSamples?: boolean;
}
interface Node {
isRoot(): boolean;
visit(visitor: Visitor, state: any): void;
}
interface Visitor<N extends Node = Node> {
onStart(root: N, state: any): void;
onSummary(root: N, state: any): void;
onDetail(root: N, state: any): void;
onSummaryEnd(root: N, state: any): void;
onEnd(root: N, state: any): void;
}
interface FileOptions {
file: string;
}
interface ProjectOptions {
projectRoot: string;
}
interface ReportOptions {
clover: CloverOptions;
cobertura: CoberturaOptions;
"html-spa": HtmlSpaOptions;
html: HtmlOptions;
json: JsonOptions;
"json-summary": JsonSummaryOptions;
lcov: LcovOptions;
lcovonly: LcovOnlyOptions;
none: never;
teamcity: TeamcityOptions;
text: TextOptions;
"text-lcov": TextLcovOptions;
"text-summary": TextSummaryOptions;
}
interface CloverOptions extends FileOptions, ProjectOptions {}
interface CoberturaOptions extends FileOptions, ProjectOptions {}
interface HtmlSpaOptions extends HtmlOptions {
metricsToShow: Array<"lines" | "branches" | "functions" | "statements">;
}
interface HtmlOptions {
verbose: boolean;
skipEmpty: boolean;
subdir: string;
linkMapper: LinkMapper;
}
type JsonOptions = FileOptions;
type JsonSummaryOptions = FileOptions;
interface LcovOptions extends FileOptions, ProjectOptions {}
interface LcovOnlyOptions extends FileOptions, ProjectOptions {}
interface TeamcityOptions extends FileOptions {
blockName: string;
}
interface TextOptions extends FileOptions {
maxCols: number;
skipEmpty: boolean;
skipFull: boolean;
}
type TextLcovOptions = ProjectOptions;
type TextSummaryOptions = FileOptions;
interface LinkMapper {
getPath(node: string | Node): string;
relativePath(source: string | Node, target: string | Node): string;
assetPath(node: Node, name: string): string;
}
type TransformResult = string | Partial<TransformResult$1> | undefined | null | void;
type CoverageResults = unknown;
interface CoverageProvider {
name: string;
/** Called when provider is being initialized before tests run */
initialize: (ctx: Vitest) => Promise<void> | void;
/** Called when setting coverage options for Vitest context (`ctx.config.coverage`) */
resolveOptions: () => ResolvedCoverageOptions;
/** Callback to clean previous reports */
clean: (clean?: boolean) => void | Promise<void>;
/** Called with coverage results after a single test file has been run */
onAfterSuiteRun: (meta: AfterSuiteRunMeta) => void | Promise<void>;
/** Callback called when test run fails */
onTestFailure?: () => void | Promise<void>;
/** Callback to generate final coverage results */
generateCoverage: (reportContext: ReportContext) => CoverageResults | Promise<CoverageResults>;
/** Callback to convert coverage results to coverage reports. Called with results returned from `generateCoverage` */
reportCoverage: (coverage: CoverageResults, reportContext: ReportContext) => void | Promise<void>;
/** Callback for `--merge-reports` options. Called with multiple coverage results generated by `generateCoverage`. */
mergeReports?: (coverages: CoverageResults[]) => void | Promise<void>;
/** Callback called for instrumenting files with coverage counters. */
onFileTransform?: (sourceCode: string, id: string, pluginCtx: any) => TransformResult | Promise<TransformResult>;
}
interface ReportContext {
/** Indicates whether all tests were run. False when only specific tests were run. */
allTestsRun?: boolean;
}
interface CoverageProviderModule extends RuntimeCoverageProviderModule {
/**
* Factory for creating a new coverage provider
*/
getProvider: () => CoverageProvider | Promise<CoverageProvider>;
}
type CoverageReporter = keyof ReportOptions | (string & {});
type CoverageReporterWithOptions<ReporterName extends CoverageReporter = CoverageReporter> = ReporterName extends keyof ReportOptions ? ReportOptions[ReporterName] extends never ? [ReporterName, object] : [ReporterName, Partial<ReportOptions[ReporterName]>] : [ReporterName, Record<string, unknown>];
type CoverageProviderName = "v8" | "istanbul" | "custom" | undefined;
type CoverageOptions<T extends CoverageProviderName = CoverageProviderName> = T extends "istanbul" ? {
provider: T
} & CoverageIstanbulOptions : T extends "v8" ? {
/**
* Provider to use for coverage collection.
*
* @default 'v8'
*/
provider: T
} & CoverageV8Options : T extends "custom" ? {
provider: T
} & CustomProviderOptions : {
provider?: T
} & CoverageV8Options;
/** Fields that have default values. Internally these will always be defined. */
type FieldsWithDefaultValues = "enabled" | "clean" | "cleanOnRerun" | "reportsDirectory" | "exclude" | "extension" | "reportOnFailure" | "allowExternal" | "processingConcurrency";
type ResolvedCoverageOptions<T extends CoverageProviderName = CoverageProviderName> = CoverageOptions<T> & Required<Pick<CoverageOptions<T>, FieldsWithDefaultValues>> & {
reporter: CoverageReporterWithOptions[]
};
interface BaseCoverageOptions {
/**
* Enables coverage collection. Can be overridden using `--coverage` CLI option.
*
* @default false
*/
enabled?: boolean;
/**
* List of files included in coverage as glob patterns
*
* @default ['**']
*/
include?: string[];
/**
* Extensions for files to be included in coverage
*
* @default ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte', '.marko']
*/
extension?: string | string[];
/**
* List of files excluded from coverage as glob patterns
*
* @default ['coverage/**', 'dist/**', '**\/[.]**', 'packages/*\/test?(s)/**', '**\/*.d.ts', '**\/virtual:*', '**\/__x00__*', '**\/\x00*', 'cypress/**', 'test?(s)/**', 'test?(-*).?(c|m)[jt]s?(x)', '**\/*{.,-}{test,spec}?(-d).?(c|m)[jt]s?(x)', '**\/__tests__/**', '**\/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*', '**\/vitest.{workspace,projects}.[jt]s?(on)', '**\/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}']
*/
exclude?: string[];
/**
* Whether to include all files, including the untested ones into report
*
* @default true
*/
all?: boolean;
/**
* Clean coverage results before running tests
*
* @default true
*/
clean?: boolean;
/**
* Clean coverage report on watch rerun
*
* @default true
*/
cleanOnRerun?: boolean;
/**
* Directory to write coverage report to
*
* @default './coverage'
*/
reportsDirectory?: string;
/**
* Coverage reporters to use.
* See [istanbul documentation](https://istanbul.js.org/docs/advanced/alternative-reporters/) for detailed list of all reporters.
*
* @default ['text', 'html', 'clover', 'json']
*/
reporter?: Arrayable$1<CoverageReporter> | (CoverageReporter | [CoverageReporter] | CoverageReporterWithOptions)[];
/**
* Do not show files with 100% statement, branch, and function coverage
*
* @default false
*/
skipFull?: boolean;
/**
* Configurations for thresholds
*
* @example
*
* ```ts
* {
* // Thresholds for all files
* functions: 95,
* branches: 70,
* perFile: true,
* autoUpdate: true,
*
* // Thresholds for utilities
* 'src/utils/**.ts': {
* lines: 100,
* statements: 95,
* }
* }
* ```
*/
thresholds?: Thresholds | ({
[glob: string]: Pick<Thresholds, 100 | "statements" | "functions" | "branches" | "lines">
} & Thresholds);
/**
* Watermarks for statements, lines, branches and functions.
*
* Default value is `[50,80]` for each property.
*/
watermarks?: {
statements?: [number, number]
functions?: [number, number]
branches?: [number, number]
lines?: [number, number]
};
/**
* Generate coverage report even when tests fail.
*
* @default false
*/
reportOnFailure?: boolean;
/**
* Collect coverage of files outside the project `root`.
*
* @default false
*/
allowExternal?: boolean;
/**
* Apply exclusions again after coverage has been remapped to original sources.
* This is useful when your source files are transpiled and may contain source maps
* of non-source files.
*
* Use this option when you are seeing files that show up in report even if they
* match your `coverage.exclude` patterns.
*
* @default false
*/
excludeAfterRemap?: boolean;
/**
* Concurrency limit used when processing the coverage results.
* Defaults to `Math.min(20, os.availableParallelism?.() ?? os.cpus().length)`
*/
processingConcurrency?: number;
}
interface CoverageIstanbulOptions extends BaseCoverageOptions {
/**
* Set to array of class method names to ignore for coverage
*
* @default []
*/
ignoreClassMethods?: string[];
}
interface CoverageV8Options extends BaseCoverageOptions {
/**
* Ignore empty lines, comments and other non-runtime code, e.g. Typescript types
* - Requires `experimentalAstAwareRemapping: false`
*/
ignoreEmptyLines?: boolean;
/**
* Remap coverage with experimental AST based analysis
* - Provides more accurate results compared to default mode
*/
experimentalAstAwareRemapping?: boolean;
/**
* Set to array of class method names to ignore for coverage.
* - Requires `experimentalAstAwareRemapping: true`
*
* @default []
*/
ignoreClassMethods?: string[];
}
interface CustomProviderOptions extends Pick<BaseCoverageOptions, FieldsWithDefaultValues> {
/** Name of the module or path to a file to load the custom provider from */
customProviderModule: string;
}
interface Thresholds {
/** Set global thresholds to `100` */
100?: boolean;
/** Check thresholds per file. */
perFile?: boolean;
/**
* Update threshold values automatically when current coverage is higher than earlier thresholds
*
* @default false
*/
autoUpdate?: boolean;
/** Thresholds for statements */
statements?: number;
/** Thresholds for functions */
functions?: number;
/** Thresholds for branches */
branches?: number;
/** Thresholds for lines */
lines?: number;
}
type BuiltinEnvironment = "node" | "jsdom" | "happy-dom" | "edge-runtime";
// Record is used, so user can get intellisense for builtin environments, but still allow custom environments
type VitestEnvironment = BuiltinEnvironment | (string & Record<never, never>);
type CSSModuleScopeStrategy = "stable" | "scoped" | "non-scoped";
type ApiConfig = Pick<ServerOptions, "port" | "strictPort" | "host" | "middlewareMode">;
type VitestRunMode = "test" | "benchmark";
interface ProjectName {
label: string;
color?: LabelColor;
}
interface SequenceOptions {
/**
* Class that handles sorting and sharding algorithm.
* If you only need to change sorting, you can extend
* your custom sequencer from `BaseSequencer` from `vitest/node`.
* @default BaseSequencer
*/
sequencer?: TestSequencerConstructor;
/**
* Controls the order in which this project runs its tests when using multiple [projects](/guide/projects).
*
* - Projects with the same group order number will run together, and groups are run from lowest to highest.
* - If you don’t set this option, all projects run in parallel.
* - If several projects use the same group order, they will run at the same time.
* @default 0
*/
groupOrder?: number;
/**
* Should files and tests run in random order.
* @default false
*/
shuffle?: boolean | {
/**
* Should files run in random order. Long running tests will not start
* earlier if you enable this option.
* @default false
*/
files?: boolean
/**
* Should tests run in random order.
* @default false
*/
tests?: boolean
};
/**
* Should tests run in parallel.
* @default false
*/
concurrent?: boolean;
/**
* Defines how setup files should be ordered
* - 'parallel' will run all setup files in parallel
* - 'list' will run all setup files in the order they are defined in the config file
* @default 'parallel'
*/
setupFiles?: SequenceSetupFiles;
/**
* Seed for the random number generator.
* @default Date.now()
*/
seed?: number;
/**
* Defines how hooks should be ordered
* - `stack` will order "after" hooks in reverse order, "before" hooks will run sequentially
* - `list` will order hooks in the order they are defined
* - `parallel` will run hooks in a single group in parallel
* @default 'stack'
*/
hooks?: SequenceHooks;
}
type DepsOptimizationOptions = Omit<DepOptimizationConfig, "disabled" | "noDiscovery"> & {
enabled?: boolean
};
interface TransformModePatterns {
/**
* Use SSR transform pipeline for all modules inside specified tests.
* Vite plugins will receive `ssr: true` flag when processing those files.
*
* @default tests with node or edge environment
*/
ssr?: string[];
/**
* First do a normal transform pipeline (targeting browser),
* then then do a SSR rewrite to run the code in Node.
* Vite plugins will receive `ssr: false` flag when processing those files.
*
* @default tests with jsdom or happy-dom environment
*/
web?: string[];
}
interface DepsOptions {
/**
* Enable dependency optimization. This can improve the performance of your tests.
*/
optimizer?: {
web?: DepsOptimizationOptions
ssr?: DepsOptimizationOptions
};
web?: {
/**
* Should Vitest process assets (.png, .svg, .jpg, etc) files and resolve them like Vite does in the browser.
*
* These module will have a default export equal to the path to the asset, if no query is specified.
*
* **At the moment, this option only works with `{ pool: 'vmThreads' }`.**
*
* @default true
*/
transformAssets?: boolean
/**
* Should Vitest process CSS (.css, .scss, .sass, etc) files and resolve them like Vite does in the browser.
*
* If CSS files are disabled with `css` options, this option will just silence UNKNOWN_EXTENSION errors.
*
* **At the moment, this option only works with `{ pool: 'vmThreads' }`.**
*
* @default true
*/
transformCss?: boolean
/**
* Regexp pattern to match external files that should be transformed.
*
* By default, files inside `node_modules` are externalized and not transformed.
*
* **At the moment, this option only works with `{ pool: 'vmThreads' }`.**
*
* @default []
*/
transformGlobPattern?: RegExp | RegExp[]
};
/**
* Externalize means that Vite will bypass the package to native Node.
*
* Externalized dependencies will not be applied Vite's transformers and resolvers.
* And does not support HMR on reload.
*
* Typically, packages under `node_modules` are externalized.
*
* @deprecated If you rely on vite-node directly, use `server.deps.external` instead. Otherwise, consider using `deps.optimizer.{web,ssr}.exclude`.
*/
external?: (string | RegExp)[];
/**
* Vite will process inlined modules.
*
* This could be helpful to handle packages that ship `.js` in ESM format (that Node can't handle).
*
* If `true`, every dependency will be inlined
*
* @deprecated If you rely on vite-node directly, use `server.deps.inline` instead. Otherwise, consider using `deps.optimizer.{web,ssr}.include`.
*/
inline?: (string | RegExp)[] | true;
/**
* Interpret CJS module's default as named exports
*
* @default true
*/
interopDefault?: boolean;
/**
* When a dependency is a valid ESM package, try to guess the cjs version based on the path.
* This will significantly improve the performance in huge repo, but might potentially
* cause some misalignment if a