-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaywright.config.ts
More file actions
118 lines (114 loc) · 3.61 KB
/
Copy pathplaywright.config.ts
File metadata and controls
118 lines (114 loc) · 3.61 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { execFileSync } from "node:child_process";
import { defineConfig, devices } from "@playwright/test";
const externalBaseUrl = process.env.BASE_URL;
const externalTestMode = process.env.EXTERNAL_TEST_MODE ?? "production";
const localPort = process.env.PLAYWRIGHT_PORT ?? "3000";
if (!/^[1-9][0-9]{0,4}$/.test(localPort) || Number(localPort) > 65_535) {
throw new Error("PLAYWRIGHT_PORT must be an integer from 1 through 65535.");
}
const localBaseUrl = `http://127.0.0.1:${localPort}`;
const externalModes = new Set(["preview", "production"]);
if (externalBaseUrl && !externalModes.has(externalTestMode)) {
throw new Error("EXTERNAL_TEST_MODE must be preview or production.");
}
const expectedCommit = externalBaseUrl
? (externalTestMode === "preview"
? process.env.EXPECTED_PREVIEW_COMMIT
: process.env.EXPECTED_COMMIT
)
?.trim()
.toLowerCase()
: undefined;
if (externalBaseUrl && !/^[0-9a-f]{40,64}$/.test(expectedCommit ?? "")) {
throw new Error(
externalTestMode === "preview"
? "EXPECTED_PREVIEW_COMMIT must be a full hexadecimal commit ID."
: "EXPECTED_COMMIT must be a full hexadecimal commit ID.",
);
}
const sourceCommit = execFileSync("git", ["rev-parse", "HEAD"], {
encoding: "utf8",
})
.trim()
.toLowerCase();
if (!/^[0-9a-f]{40,64}$/.test(sourceCommit)) {
throw new Error("Playwright source commit must be a full commit ID.");
}
if (externalBaseUrl && sourceCommit !== expectedCommit) {
throw new Error(
"Playwright checked-out source commit does not match the expected deployment commit.",
);
}
const testMode = externalBaseUrl ? externalTestMode : "local";
function parseOrigin(value: string, variableName: string) {
try {
return new URL(value).origin;
} catch {
throw new Error(`${variableName} must be a valid absolute URL.`);
}
}
const targetOrigin = parseOrigin(externalBaseUrl ?? localBaseUrl, "BASE_URL");
const testOrigin = process.env.CANONICAL_ORIGIN
? parseOrigin(process.env.CANONICAL_ORIGIN, "CANONICAL_ORIGIN")
: targetOrigin;
export default defineConfig({
testDir: "./tests/e2e",
fullyParallel: true,
forbidOnly: Boolean(process.env.CI),
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 2 : undefined,
metadata: {
schemaVersion: 1,
mode: testMode,
origin: testOrigin,
targetOrigin,
expectedCommit: expectedCommit ?? null,
sourceCommit,
},
reporter: [
["list"],
["html", { outputFolder: "playwright-report", open: "never" }],
],
outputDir: "test-results",
timeout: 30_000,
expect: { timeout: 8_000 },
use: {
baseURL: externalBaseUrl ?? localBaseUrl,
actionTimeout: 10_000,
navigationTimeout: 20_000,
trace: "retain-on-failure",
screenshot: "only-on-failure",
video: "retain-on-failure",
permissions: ["clipboard-read", "clipboard-write"],
},
webServer: externalBaseUrl
? undefined
: {
command: `npm run serve -- --port ${localPort}`,
url: localBaseUrl,
reuseExistingServer: !process.env.CI,
timeout: 120_000,
},
projects: externalBaseUrl
? [
{
name: externalTestMode,
testMatch: new RegExp(`${externalTestMode}\\.spec\\.ts$`),
use: { ...devices["Desktop Chrome"] },
},
]
: [
{
name: "chromium",
testIgnore: /(?:preview|production)\.spec\.ts/,
grepInvert: /@mobile/,
use: { ...devices["Desktop Chrome"] },
},
{
name: "mobile-chromium",
testIgnore: /(?:preview|production)\.spec\.ts/,
grep: /@mobile/,
use: { ...devices["Pixel 7"] },
},
],
});