All files / packages/configs/src/playwright index.ts

100% Statements 5/5
100% Branches 9/9
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                1x             6x   6x   6x                   18x                                  
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import type { PlaywrightTestConfig } from "@playwright/test";
import { SERVER_CONSOLE_ENV } from "./setup/server.ts";
 
interface PlaywrightOptions {
    port: number;
    /** Starts the built app; it reads `PORT` and gets `env` on top of the runner's own. */
    command: string;
    env?: Record<string, string>;
    widths?: number[];
    /** The first width a `@desktop` test runs at; a `@mobile` test runs only below it. */
    desktopFrom?: number;
}
 
/** Chromium at each width, with specs in `e2e/`, failing the run on anything the server logs. */
export const createConfig = ({
    port,
    command,
    env = {},
    widths = [390, 768, 1280],
    desktopFrom = 1024,
}: PlaywrightOptions): PlaywrightTestConfig => {
    const baseURL = `http://localhost:${port}`;
    // Outside `outputDir`, which Playwright empties after the server may already have written
    const serverConsole = join(process.cwd(), "node_modules/.cache/e2e/server-console.log");
 
    return {
        testDir: "e2e",
        outputDir: ".playwright",
        forbidOnly: !!process.env.CI,
        fullyParallel: true,
        // Each worker runs its own Chromium, and parallel agent sessions each start a run
        workers: process.env.CI ? undefined : 2,
        reporter: "list",
        globalTeardown: fileURLToPath(new URL("./setup/teardown.ts", import.meta.url)),
        use: { baseURL },
        projects: widths.map((width, index) => ({
            name: `${width}px`,
            use: { browserName: "chromium", viewport: { width, height: 900 } },
            grepInvert: new RegExp(
                [width >= desktopFrom ? "@mobile" : "@desktop", index > 0 && "@any-width"]
                    .filter(Boolean)
                    .join("|"),
            ),
        })),
        webServer: {
            command,
            url: baseURL,
            env: { PORT: String(port), [SERVER_CONSOLE_ENV]: serverConsole, ...env },
            reuseExistingServer: false,
        },
    };
};