mvp-ai's picture
download
raw
7.81 kB
// Copyright (c) 2026 the Zuke contributors
// SPDX-License-Identifier: MIT
/**
* `PlaywrightTasks` — typed task functions for the Playwright CLI, in the
* settings-lambda style: configure a fluent settings object in a lambda, and
* the task function builds the command line and executes it.
*
* ```ts
* import { PlaywrightTasks } from "@zuke/playwright";
* await PlaywrightTasks.install((s) => s.withDeps());
* await PlaywrightTasks.test((s) => s.project("chromium").grep("@smoke"));
* ```
*
* The binary is `playwright` from PATH (install it as a project dependency and
* expose it, or use a wrapper that resolves `npx playwright`). On Windows a
* `.cmd` shim is resolved and spawned directly by the shared tooling base.
*/
import {
type Configure,
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";
/** Base for all Playwright subcommand settings: binary is `playwright`. */
export abstract class PlaywrightSettings extends ToolSettings {
/** The tool binary invoked by all Playwright subcommands: `playwright`. */
protected override defaultTool(): string {
return "playwright";
}
/** Resolve the binary from `node_modules/.bin` by default — playwright is an npm-distributed tool. */
protected override defaultResolution(): ToolResolution {
return "node_modules";
}
}
/** Settings for `playwright test`. */
export class PlaywrightTestSettings extends PlaywrightSettings {
#projects: string[] = [];
#grep?: string;
#headed = false;
#ui = false;
#workers?: number;
#reporter?: string;
#updateSnapshots = false;
#config?: string;
#paths: string[] = [];
/** Restrict to the named project(s) (`--project=`); repeatable. */
project(...names: string[]): this {
this.#projects.push(...names);
return this;
}
/** Only run tests matching the pattern (`--grep`). */
grep(pattern: string): this {
this.#grep = pattern;
return this;
}
/** Run in headed browsers (`--headed`). */
headed(): this {
this.#headed = true;
return this;
}
/** Open the interactive UI mode (`--ui`). */
ui(): this {
this.#ui = true;
return this;
}
/** Set the number of parallel workers (`--workers=`). */
workers(count: number): this {
this.#workers = count;
return this;
}
/** Choose the reporter (`--reporter=`). */
reporter(name: string): this {
this.#reporter = name;
return this;
}
/** Update snapshots instead of failing on a mismatch (`--update-snapshots`). */
updateSnapshots(): this {
this.#updateSnapshots = true;
return this;
}
/** Use a specific config file (`--config=`). */
config(path: string): this {
this.#config = path;
return this;
}
/** Test file or directory filters to run; omit to run all tests. */
paths(...filters: string[]): this {
this.#paths.push(...filters);
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 `playwright test` argv. */
protected override buildArgs(): string[] {
const argv = ["test"];
for (const p of this.#projects) argv.push(`--project=${p}`);
if (this.#grep !== undefined) argv.push("--grep", this.#grep);
if (this.#headed) argv.push("--headed");
if (this.#ui) argv.push("--ui");
if (this.#workers !== undefined) argv.push(`--workers=${this.#workers}`);
if (this.#reporter !== undefined) argv.push(`--reporter=${this.#reporter}`);
if (this.#updateSnapshots) argv.push("--update-snapshots");
if (this.#config !== undefined) argv.push(`--config=${this.#config}`);
argv.push(...this.#paths);
return argv;
}
}
/** Settings for `playwright install` (browser binaries). */
export class PlaywrightInstallSettings extends PlaywrightSettings {
#browsers: string[] = [];
#withDeps = false;
/** Browsers to install (e.g. `chromium`); omit to install all. */
browsers(...names: string[]): this {
this.#browsers.push(...names);
return this;
}
/** Also install the OS dependencies (`--with-deps`). */
withDeps(): this {
this.#withDeps = true;
return this;
}
/** Assemble the `playwright install` argv. */
protected override buildArgs(): string[] {
const argv = ["install"];
if (this.#withDeps) argv.push("--with-deps");
argv.push(...this.#browsers);
return argv;
}
}
/** Settings for `playwright show-report`. */
export class PlaywrightShowReportSettings extends PlaywrightSettings {
#dir?: string;
/** The report directory to open; omit for the default. */
dir(path: string): this {
this.#dir = path;
return this;
}
/** Assemble the `playwright show-report` argv. */
protected override buildArgs(): string[] {
const argv = ["show-report"];
if (this.#dir !== undefined) argv.push(this.#dir);
return argv;
}
}
/** Settings for `playwright codegen`. */
export class PlaywrightCodegenSettings extends PlaywrightSettings {
#url?: string;
#target?: string;
#output?: string;
/** The URL to open for recording; omit to start blank. */
url(value: string): this {
this.#url = value;
return this;
}
/** The output language (`--target=`, e.g. `javascript`, `python`). */
target(language: string): this {
this.#target = language;
return this;
}
/** Write the generated script to a file (`--output=`). */
output(path: string): this {
this.#output = path;
return this;
}
/** Assemble the `playwright codegen` argv. */
protected override buildArgs(): string[] {
const argv = ["codegen"];
if (this.#target !== undefined) argv.push(`--target=${this.#target}`);
if (this.#output !== undefined) argv.push(`--output=${this.#output}`);
if (this.#url !== undefined) argv.push(this.#url);
return argv;
}
}
/** The shape of {@link PlaywrightTasks}. */
export interface PlaywrightTasksApi {
/** Run the test suite: `playwright test`. */
test(configure?: Configure<PlaywrightTestSettings>): Promise<CommandOutput>;
/** Install browser binaries: `playwright install`. */
install(
configure?: Configure<PlaywrightInstallSettings>,
): Promise<CommandOutput>;
/** Open the HTML report: `playwright show-report`. */
showReport(
configure?: Configure<PlaywrightShowReportSettings>,
): Promise<CommandOutput>;
/** Record interactions into a script: `playwright codegen`. */
codegen(
configure?: Configure<PlaywrightCodegenSettings>,
): Promise<CommandOutput>;
}
/** Typed task functions for the Playwright CLI. */
export const PlaywrightTasks: PlaywrightTasksApi = {
/** Run the test suite: `playwright test`. */
test(configure?: Configure<PlaywrightTestSettings>): Promise<CommandOutput> {
return runSettings(new PlaywrightTestSettings(), configure);
},
/** Install browser binaries: `playwright install`. */
install(
configure?: Configure<PlaywrightInstallSettings>,
): Promise<CommandOutput> {
return runSettings(new PlaywrightInstallSettings(), configure);
},
/** Open the HTML report: `playwright show-report`. */
showReport(
configure?: Configure<PlaywrightShowReportSettings>,
): Promise<CommandOutput> {
return runSettings(new PlaywrightShowReportSettings(), configure);
},
/** Record interactions into a script: `playwright codegen`. */
codegen(
configure?: Configure<PlaywrightCodegenSettings>,
): Promise<CommandOutput> {
return runSettings(new PlaywrightCodegenSettings(), configure);
},
};

Xet Storage Details

Size:
7.81 kB
·
Xet hash:
2f6f7652659c96d8e395b87d89c03655bf1ba5b8f9349da6172e1068dda4c5c7

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