import { readFileSync } from "node:fs"; import { afterEach, describe, expect, it } from "vitest"; import { configRuns, describeResultFolders, describeServerPorts, itExitsWhenItsPortIsTaken, itKeepsTheViteCacheInTheRun, hold, releaseRuns, webServer, webServers, } from "../helpers/playwright-run-isolation.ts"; /** * 1105/12-04. Every Playwright config wrote its results, report and JUnit * file, and the e2e and visual builds wrote `dist`, at fixed paths. A second * run in one checkout deleted the first one's results (Playwright empties * `outputDir` at start) and emptied the `dist` its preview was serving. Under * TEST_RUN_ID each run gets its own paths; without it the paths stay where * FE-GATE-008 and the CI upload read them. * * The servers listened on fixed ports (e2e 4173, visual and capabilities both * 4174, storybook 6006, dev 5173), so the second run failed with "is already * used", and the dev config reused whatever answered on 5173. Each now picks * a free port, which the runner's environment hands to the workers. * * Each load evaluates the config again with the environment of one process. * * 1106/10-01. The capabilities config is checked in * tests/unit/playwright-capabilities-run-isolation.test.ts: it goes with the * browser file/storage runtime, and test:browser-file-storage-removal deletes * the config and that file by name. This file has the four configs every * project has. */ const runs = configRuns({ e2e: () => import("../../playwright.config.ts"), visual: () => import("../../playwright.visual.config.ts"), storybook: () => import("../../playwright.storybook.config.ts"), dev: () => import("../../playwright.dev.config.ts"), }); afterEach(releaseRuns); describeResultFolders(runs, [ ["e2e", "e2e"], ["visual", "visual"], ["storybook", "storybook"], ]); describe("the build a run serves", () => { it.each(["e2e", "visual"] as const)( "is the checkout's dist without a run ID, and the run's own with one (%s)", async (name) => { const checkout = webServer(await runs.load(name)); expect(checkout.env?.["RUN_OUTPUT_ROOT"]).toBe("."); expect(checkout.command).toMatch(/ --outDir dist(?: |$)/u); const run = webServer(await runs.load(name, { TEST_RUN_ID: "a" })); // The build writes under RUN_OUTPUT_ROOT and the preview serves it. expect(run.env?.["RUN_OUTPUT_ROOT"]).toBe(".tmp/runs/a"); expect(run.command).toMatch(/^corepack pnpm build && corepack pnpm preview /u); expect(run.command).toMatch(/ --outDir \.tmp\/runs\/a\/dist(?: |$)/u); }, ); itKeepsTheViteCacheInTheRun(runs, ["dev"]); it("builds the workshop into the run's folder the way build:storybook does", async () => { const checkout = webServer(await runs.load("storybook")).command; expect(checkout).toMatch( /^corepack pnpm build:storybook && node scripts\/serve-static\.ts artifacts\/storybook\/static /u, ); const run = webServer(await runs.load("storybook", { TEST_RUN_ID: "a" })).command; const script = ( JSON.parse(readFileSync("package.json", "utf8")) as { scripts: Record } ).scripts["build:storybook"]!; const sameStepsOtherFolder = script .replace("&& storybook build", "&& corepack pnpm exec storybook build") .replace("-o artifacts/storybook/static", "-o .tmp/runs/a/artifacts/storybook/static"); expect(sameStepsOtherFolder).not.toBe(script); expect(run.startsWith(`${sameStepsOtherFolder} && `)).toBe(true); expect(run).toMatch(/ node scripts\/serve-static\.ts \.tmp\/runs\/a\/artifacts\/storybook\/static /u); }); }); describeServerPorts(runs, ["e2e", "visual", "storybook", "dev"]); // The gallery goldens open workshop stories: the visual config starts the // static workshop next to its preview, on a port of its own. describe("the visual config's workshop server", () => { const workshop = async (environment: Readonly> = {}) => { const runner = await runs.loadProcess("visual", environment); const servers = webServers(runner.config); expect(servers).toHaveLength(2); return { runner, server: servers[1]! }; }; it("is a free port of its own the runner hands to its workers", async () => { const { runner, server } = await workshop(); const port = Number(runner.after["TEST_VISUAL_WORKSHOP_PORT"]); expect(port).toBeGreaterThan(0); expect(port).not.toBe(Number(runner.after["TEST_VISUAL_PORT"])); expect(server.url).toBe(`http://127.0.0.1:${port}`); expect(server.command).toMatch(new RegExp(` ${port}$`, "u")); expect(server.reuseExistingServer).toBe(false); }); it("moves for a second run while the first run's server holds its port", async () => { const first = (await workshop()).server.url; await hold(Number(new URL(String(first)).port)); expect((await workshop()).server.url).not.toBe(first); }); it("serves the build the workshop config serves, in the run's folder", async () => { const pinned = { TEST_VISUAL_WORKSHOP_PORT: "40124", TEST_STORYBOOK_PORT: "40124" }; for (const environment of [pinned, { ...pinned, TEST_RUN_ID: "a" }]) { const { server } = await workshop(environment); expect(server.command).toBe(webServer(await runs.load("storybook", environment)).command); } }); }); describe("vite servers", () => { itExitsWhenItsPortIsTaken(runs, ["e2e", "visual", "dev"]); });