mvp-ai's picture
download
raw
7.45 kB
// Copyright (c) 2026 the Zuke contributors
// SPDX-License-Identifier: MIT
/**
* `VitestTasks` — typed task functions for the `vitest` test runner, in the
* same settings-lambda style as the other Zuke tool wrappers: configure a
* fluent settings object in a lambda, and the task function builds the command
* line and executes it.
*
* Vitest defaults to watch mode when invoked bare; this wrapper emits the
* one-shot `run` subcommand by default (CI-friendly) and switches to `watch`
* via {@link VitestSettings.watch}.
*
* ```ts
* import { VitestTasks } from "@zuke/vitest";
* await VitestTasks.run((s) => s.coverage().reporter("dot"));
* ```
*
* Arguments stay a discrete argv array end-to-end — never a concatenated shell
* string — so command construction is injection-free.
*
* @module
*/
import {
type Configure,
type PathLike,
runSettings,
type ToolResolution,
ToolSettings,
} from "@zuke/core/tooling";
import type { CommandOutput } from "@zuke/core/shell";
import { reportTestCounts } from "@zuke/core";
import { parseTestSummary } from "./test_summary.ts";
/** The Vitest worker pool implementation (`--pool`). */
export type VitestPool = "threads" | "forks" | "vmThreads" | "vmForks";
/** Settings for a `vitest` run. */
export class VitestSettings extends ToolSettings {
#filters: string[] = [];
#watch = false;
#config?: string;
#root?: string;
#dir?: string;
#coverage = false;
#ui = false;
#update = false;
#run = false;
#pool?: VitestPool;
#maxWorkers?: number;
#minWorkers?: number;
#bail?: number;
#retry?: number;
#shard?: string;
#reporters: string[] = [];
#outputFile?: string;
#testNamePattern?: string;
#environment?: string;
#globals = false;
#passWithNoTests = false;
#silent = false;
/** The underlying tool binary (`vitest`). */
protected override defaultTool(): string {
return "vitest";
}
/** Resolve the binary from `node_modules/.bin` by default — vitest is an npm-distributed tool. */
protected override defaultResolution(): ToolResolution {
return "node_modules";
}
/** Filename filters matched against test files (positional); repeatable. */
filters(...values: string[]): this {
this.#filters.push(...values);
return this;
}
/** Use watch mode (`watch`) instead of the default one-shot `run`. */
watch(): this {
this.#watch = true;
return this;
}
/** Use an explicit config file (`-c`/`--config`). */
config(path: PathLike): this {
this.#config = String(path);
return this;
}
/** Project root (`--root`). */
root(path: PathLike): this {
this.#root = String(path);
return this;
}
/** Restrict the scanned directory (`--dir`). */
dir(path: PathLike): this {
this.#dir = String(path);
return this;
}
/** Collect test coverage (`--coverage`). */
coverage(): this {
this.#coverage = true;
return this;
}
/** Open the Vitest UI (`--ui`). */
ui(): this {
this.#ui = true;
return this;
}
/** Update snapshots (`-u`/`--update`). */
update(): this {
this.#update = true;
return this;
}
/** Force one-shot mode even under watch (`--run`). */
forceRun(): this {
this.#run = true;
return this;
}
/** Choose the worker pool implementation (`--pool`). */
pool(name: VitestPool): this {
this.#pool = name;
return this;
}
/** Cap the number of parallel workers (`--maxWorkers`). */
maxWorkers(count: number): this {
this.#maxWorkers = count;
return this;
}
/** Set the minimum number of parallel workers (`--minWorkers`). */
minWorkers(count: number): this {
this.#minWorkers = count;
return this;
}
/** Stop after N failed tests (`--bail`). */
bail(count: number): this {
this.#bail = count;
return this;
}
/** Retry failed tests up to N times (`--retry`). */
retry(count: number): this {
this.#retry = count;
return this;
}
/** Run a shard of the suite, e.g. `1/4` (`--shard`). */
shard(value: string): this {
this.#shard = value;
return this;
}
/** Use the named reporters (`--reporter`); repeatable. */
reporter(...names: string[]): this {
for (const name of names) this.#reporters.push("--reporter", name);
return this;
}
/** Write report output to a file (`--outputFile`). */
outputFile(path: PathLike): this {
this.#outputFile = String(path);
return this;
}
/** Run only tests whose name matches the pattern (`-t`/`--testNamePattern`). */
testNamePattern(pattern: string): this {
this.#testNamePattern = pattern;
return this;
}
/** Test environment, e.g. `jsdom`, `node` (`--environment`). */
environment(value: string): this {
this.#environment = value;
return this;
}
/** Enable global test APIs (`--globals`). */
globals(): this {
this.#globals = true;
return this;
}
/** Pass when no tests are found (`--passWithNoTests`). */
passWithNoTests(): this {
this.#passWithNoTests = true;
return this;
}
/** Suppress test console output (`--silent`). */
silent(): this {
this.#silent = true;
return this;
}
/** Report the run's counts onto the build summary (see the module docs). */
protected override onOutput(output: CommandOutput): void {
const counts = parseTestSummary(output);
if (counts !== undefined) reportTestCounts(counts);
}
/** Assemble the `vitest run`/`vitest watch` argv. */
protected override buildArgs(): string[] {
const argv = [this.#watch ? "watch" : "run"];
if (this.#config !== undefined) argv.push("-c", this.#config);
if (this.#root !== undefined) argv.push("--root", this.#root);
if (this.#dir !== undefined) argv.push("--dir", this.#dir);
if (this.#coverage) argv.push("--coverage");
if (this.#ui) argv.push("--ui");
if (this.#update) argv.push("-u");
if (this.#run) argv.push("--run");
if (this.#pool !== undefined) argv.push("--pool", this.#pool);
if (this.#maxWorkers !== undefined) {
argv.push("--maxWorkers", String(this.#maxWorkers));
}
if (this.#minWorkers !== undefined) {
argv.push("--minWorkers", String(this.#minWorkers));
}
if (this.#bail !== undefined) argv.push("--bail", String(this.#bail));
if (this.#retry !== undefined) argv.push("--retry", String(this.#retry));
if (this.#shard !== undefined) argv.push("--shard", this.#shard);
argv.push(...this.#reporters);
if (this.#outputFile !== undefined) {
argv.push("--outputFile", this.#outputFile);
}
if (this.#testNamePattern !== undefined) {
argv.push("-t", this.#testNamePattern);
}
if (this.#environment !== undefined) {
argv.push("--environment", this.#environment);
}
if (this.#globals) argv.push("--globals");
if (this.#passWithNoTests) argv.push("--passWithNoTests");
if (this.#silent) argv.push("--silent");
argv.push(...this.#filters);
return argv;
}
}
/** The shape of {@link VitestTasks}. */
export interface VitestTasksApi {
/** Run tests with `vitest` (one-shot `run` unless {@link VitestSettings.watch}). */
run(configure?: Configure<VitestSettings>): Promise<CommandOutput>;
}
/** Typed task functions for the `vitest` test runner. */
export const VitestTasks: VitestTasksApi = {
run(configure?: Configure<VitestSettings>): Promise<CommandOutput> {
return runSettings(new VitestSettings(), configure);
},
};

Xet Storage Details

Size:
7.45 kB
·
Xet hash:
3dd798de0b532f660ca4da8d7f678c0c063cb71a5efae19fbb85136087b6ebf1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.