@danwo/playwright 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/spawn.ts ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Runs `playwright test` for one matrix trial: one worker, no retries, the trial's fault and
3
+ * outcome file in its environment. Playwright's own CLI is the one the project installed.
4
+ */
5
+ import { createRequire } from "node:module"
6
+ import { join } from "node:path"
7
+
8
+ import { spawnOwned } from "@danwo/runner/children"
9
+
10
+ import { OUTCOME_FILE, TRIAL_FAULT } from "./protocol.ts"
11
+
12
+ export interface PlaywrightRun {
13
+ readonly spec: string
14
+ readonly grep?: string | undefined
15
+ readonly config?: string | undefined
16
+ readonly cwd?: string | undefined
17
+ readonly outcomeFile: string
18
+ /** Playwright's output folder for this trial, so one trial's results outlive the next. */
19
+ readonly outputDir: string
20
+ /** The fault the trial arms, by label; none for the clean trial. */
21
+ readonly fault?: string | undefined
22
+ }
23
+
24
+ /** The project's Playwright Test CLI. */
25
+ function playwrightCli(cwd: string): string {
26
+ return createRequire(join(cwd, "package.json")).resolve("@playwright/test/cli")
27
+ }
28
+
29
+ function argsOf(run: PlaywrightRun): string[] {
30
+ return [
31
+ "test",
32
+ run.spec,
33
+ "--workers=1",
34
+ "--retries=0",
35
+ "--reporter=line",
36
+ `--output=${run.outputDir}`,
37
+ ...(run.grep === undefined ? [] : ["--grep", run.grep]),
38
+ ...(run.config === undefined ? [] : ["--config", run.config]),
39
+ ]
40
+ }
41
+
42
+ function environmentOf(run: PlaywrightRun): NodeJS.ProcessEnv {
43
+ const env: NodeJS.ProcessEnv = { ...process.env, [OUTCOME_FILE]: run.outcomeFile }
44
+
45
+ if (run.fault === undefined) delete env[TRIAL_FAULT]
46
+ else env[TRIAL_FAULT] = run.fault
47
+
48
+ return env
49
+ }
50
+
51
+ /** Runs the trial; resolves with Playwright's output once it exits, whatever its exit code. */
52
+ export function runPlaywright(run: PlaywrightRun): Promise<string> {
53
+ const cwd = run.cwd ?? process.cwd()
54
+
55
+ return new Promise((resolve, reject) => {
56
+ /* Owned: Playwright's workers and browsers go with it if this process ends first. */
57
+ const { child } = spawnOwned(process.execPath, [playwrightCli(cwd), ...argsOf(run)], {
58
+ cwd,
59
+ env: environmentOf(run),
60
+ stdio: ["ignore", "pipe", "pipe"],
61
+ })
62
+ let output = ""
63
+
64
+ const collect = (chunk: Buffer) => {
65
+ output += chunk.toString()
66
+ }
67
+
68
+ child.stdout?.on("data", collect)
69
+ child.stderr?.on("data", collect)
70
+ child.once("error", reject)
71
+ child.once("close", () => resolve(output))
72
+ })
73
+ }