|
| 1 | +import assert from "node:assert"; |
| 2 | +import { type SpawnOptions, spawn } from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { stripVTControlCharacters, styleText } from "node:util"; |
| 6 | +import test from "@playwright/test"; |
| 7 | +import { x } from "tinyexec"; |
| 8 | + |
| 9 | +function runCli(options: { command: string; label?: string } & SpawnOptions) { |
| 10 | + const [name, ...args] = options.command.split(" "); |
| 11 | + const child = x(name!, args, { nodeOptions: options }).process!; |
| 12 | + const label = `[${options.label ?? "cli"}]`; |
| 13 | + child.stdout!.on("data", (data) => { |
| 14 | + if (process.env.TEST_DEBUG) { |
| 15 | + console.log(styleText("cyan", label), data.toString()); |
| 16 | + } |
| 17 | + }); |
| 18 | + child.stderr!.on("data", (data) => { |
| 19 | + console.log(styleText("magenta", label), data.toString()); |
| 20 | + }); |
| 21 | + const done = new Promise<void>((resolve) => { |
| 22 | + child.on("exit", (code) => { |
| 23 | + if (code !== 0 && code !== 143 && process.platform !== "win32") { |
| 24 | + console.log(styleText("magenta", `${label}`), `exit code ${code}`); |
| 25 | + } |
| 26 | + resolve(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + async function findPort(): Promise<number> { |
| 31 | + let stdout = ""; |
| 32 | + return new Promise((resolve) => { |
| 33 | + child.stdout!.on("data", (data) => { |
| 34 | + stdout += stripVTControlCharacters(String(data)); |
| 35 | + const match = stdout.match( |
| 36 | + /http:\/\/(?:localhost|\[[::\d]+\]|[\d.]+):(\d+)/, |
| 37 | + ); |
| 38 | + if (match) { |
| 39 | + resolve(Number(match[1])); |
| 40 | + } |
| 41 | + }); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + function kill() { |
| 46 | + if (process.platform === "win32") { |
| 47 | + spawn("taskkill", ["/pid", String(child.pid), "/t", "/f"]); |
| 48 | + } else { |
| 49 | + child.kill(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return { proc: child, done, findPort, kill }; |
| 54 | +} |
| 55 | + |
| 56 | +export type Fixture = ReturnType<typeof useFixture>; |
| 57 | + |
| 58 | +export function useFixture(options: { |
| 59 | + root: string; |
| 60 | + mode?: "dev" | "build"; |
| 61 | + command?: string; |
| 62 | + buildCommand?: string; |
| 63 | + cliOptions?: SpawnOptions; |
| 64 | +}) { |
| 65 | + let cleanup: (() => Promise<void>) | undefined; |
| 66 | + let baseURL!: string; |
| 67 | + |
| 68 | + const cwd = path.resolve(options.root); |
| 69 | + |
| 70 | + // TODO: `beforeAll` is called again on any test failure. |
| 71 | + // https://playwright.dev/docs/test-retries |
| 72 | + test.beforeAll(async () => { |
| 73 | + if (options.mode === "dev") { |
| 74 | + const proc = runCli({ |
| 75 | + command: options.command ?? `pnpm dev`, |
| 76 | + label: `${options.root}:dev`, |
| 77 | + cwd, |
| 78 | + ...options.cliOptions, |
| 79 | + }); |
| 80 | + const port = await proc.findPort(); |
| 81 | + // TODO: use `test.extend` to set `baseURL`? |
| 82 | + baseURL = `http://localhost:${port}`; |
| 83 | + cleanup = async () => { |
| 84 | + proc.kill(); |
| 85 | + await proc.done; |
| 86 | + }; |
| 87 | + } |
| 88 | + if (options.mode === "build") { |
| 89 | + if (!process.env.TEST_SKIP_BUILD) { |
| 90 | + const proc = runCli({ |
| 91 | + command: options.buildCommand ?? `pnpm build`, |
| 92 | + label: `${options.root}:build`, |
| 93 | + cwd, |
| 94 | + ...options.cliOptions, |
| 95 | + }); |
| 96 | + await proc.done; |
| 97 | + } |
| 98 | + const proc = runCli({ |
| 99 | + command: options.command ?? `pnpm preview`, |
| 100 | + label: `${options.root}:preview`, |
| 101 | + cwd, |
| 102 | + ...options.cliOptions, |
| 103 | + }); |
| 104 | + const port = await proc.findPort(); |
| 105 | + baseURL = `http://localhost:${port}`; |
| 106 | + cleanup = async () => { |
| 107 | + proc.kill(); |
| 108 | + await proc.done; |
| 109 | + }; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + test.afterAll(async () => { |
| 114 | + await cleanup?.(); |
| 115 | + }); |
| 116 | + |
| 117 | + const originalFiles: Record<string, string> = {}; |
| 118 | + |
| 119 | + function createEditor(filepath: string) { |
| 120 | + filepath = path.resolve(cwd, filepath); |
| 121 | + const init = fs.readFileSync(filepath, "utf-8"); |
| 122 | + originalFiles[filepath] ??= init; |
| 123 | + let current = init; |
| 124 | + return { |
| 125 | + edit(editFn: (data: string) => string): void { |
| 126 | + const next = editFn(current); |
| 127 | + assert(next !== current, "Edit function did not change the content"); |
| 128 | + current = next; |
| 129 | + fs.writeFileSync(filepath, next); |
| 130 | + }, |
| 131 | + reset(): void { |
| 132 | + fs.writeFileSync(filepath, originalFiles[filepath]!); |
| 133 | + }, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + test.afterAll(async () => { |
| 138 | + for (const [filepath, content] of Object.entries(originalFiles)) { |
| 139 | + fs.writeFileSync(filepath, content); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + return { |
| 144 | + mode: options.mode, |
| 145 | + root: cwd, |
| 146 | + url: (url: string = "./") => new URL(url, baseURL).href, |
| 147 | + createEditor, |
| 148 | + }; |
| 149 | +} |
0 commit comments