-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathwdio.shared.conf.ts
More file actions
220 lines (201 loc) · 7 KB
/
Copy pathwdio.shared.conf.ts
File metadata and controls
220 lines (201 loc) · 7 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import * as path from 'path';
import * as fs from 'fs';
import { createServer } from 'net';
import type { Options, Capabilities } from '@wdio/types';
import { SevereServiceError } from 'webdriverio';
import { projectRoot } from './tests/e2eTestUtils';
import { AUX_PORT, BASE_URL, TEST_PORT } from './tests/e2ePorts';
import { TEST_DATASETS } from './tests/datasets';
import { DATASET_CACHE, downloadDatasets } from './tests/downloadDatasets';
// Fixed capture viewport (Playwright's default).
export const CONTENT_VIEWPORT = { width: 1280, height: 720 } as const;
// Pin the content viewport so capture geometry is stable across Chrome versions
// and OSes (independent of the OS window). Enables one shared baseline.
export const applyTestViewport = (browser: any) =>
browser.setViewport({ ...CONTENT_VIEWPORT, devicePixelRatio: 1 });
// How long data may take to load and render. Nothing here crosses the network.
export const DOWNLOAD_TIMEOUT = Number(process.env.DOWNLOAD_TIMEOUT ?? 30000);
const IS_CI = !!(process.env.CI || process.env.GITHUB_ACTIONS);
const ROOT = projectRoot();
const TMP = '.tmp/';
// Everything a run generates or downloads through the browser, including the
// fixture links it serves. Also the browser downloads directory. Keyed by port
// so a checkout, or an overridden port, gets scratch space of its own.
export const TEMP_DIR = path.resolve(ROOT, TMP, 'runs', String(TEST_PORT));
const FIXTURES_DIR = 'tests/fixtures/';
export const FIXTURES = path.resolve(ROOT, FIXTURES_DIR);
// The static server and the browser's download directory both point into
// TEMP_DIR, and Windows refuses to unlink a file while a handle is open, so give
// their teardown a moment to catch up.
const removeDir = (dir: string) =>
fs.rmSync(dir, {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 200,
});
/**
* Exposes a cached fixture under this run's directory, so specs can reach it at
* `/tmp/<name>` without every run holding its own copy.
*/
function linkCachedDataset(name: string) {
const runPath = path.join(TEMP_DIR, name);
if (fs.existsSync(runPath)) return runPath;
fs.mkdirSync(TEMP_DIR, { recursive: true });
try {
fs.linkSync(path.join(DATASET_CACHE, name), runPath);
} catch {
// A hard link needs one filesystem; a copy always works.
fs.copyFileSync(path.join(DATASET_CACHE, name), runPath);
}
return runPath;
}
const inUse = (port: number) =>
new Promise<boolean>((resolve) => {
const server = createServer()
.once('error', (err: NodeJS.ErrnoException) =>
resolve(err.code === 'EADDRINUSE')
)
.once('listening', () => server.close(() => resolve(false)));
server.listen(port);
});
const NAMED_PORTS = [
['VOLVIEW_E2E_PORT', TEST_PORT],
['VOLVIEW_E2E_AUX_PORT', AUX_PORT],
] as const;
/**
* These ports are stable, not reserved, so a suite already running out of this
* checkout, a checkout that hashed the same way, or an unrelated process can be
* holding one.
*/
async function assertPortsAvailable() {
const checks = await Promise.all(
NAMED_PORTS.map(async ([name, port]) =>
(await inUse(port)) ? `${port} (${name})` : null
)
);
const taken = checks.filter(Boolean);
if (taken.length) {
// Anything less severe is logged and the run carries on without a server.
throw new SevereServiceError(
`E2E ports already in use: ${taken.join(', ')}. Set the named variables to free ports to run anyway.`
);
}
}
export const config: Options.Testrunner = {
baseUrl: BASE_URL,
// ====================
// Runner Configuration
// ====================
runner: 'local',
//
// ==================
// Specify Test Files
// ==================
specs: ['./tests/specs/**/*.e2e.ts'],
exclude: [],
//
// ============
// Capabilities
// ============
// WebGL rendering is resource-intensive enough that six local Chrome
// instances can starve one another and exceed view-rendering timeouts.
maxInstances: IS_CI ? 1 : 3,
//
// ===================
// Test Configurations
// ===================
logLevel: 'warn',
bail: 0,
waitforTimeout: 10000,
connectionRetryTimeout: 90000,
connectionRetryCount: 3,
services: [
[
'static-server',
{
folders: [
{
mount: '/',
path: './dist',
},
{ mount: '/tmp', path: TEMP_DIR },
],
port: TEST_PORT,
},
],
[
'visual',
{
baselineFolder: path.resolve(ROOT, 'tests/baseline/'),
// Pinned geometry, so no {platformName}/{width}x{height}; one shared baseline.
formatImageName: '{tag}-{browserName}-{dpr}',
screenshotPath: TEMP_DIR,
// A missing baseline is written and passes, which suits a local run
// adding a screenshot. On CI it would turn a renamed tag into a test
// that compares nothing.
autoSaveBaseline: !IS_CI,
},
],
'cleanuptotal',
],
framework: 'mocha',
reporters: ['spec', 'html-nice'],
mochaOpts: {
ui: 'bdd',
timeout: 90_000,
require: ['./tests/rootHooks.ts'],
},
//
// Hooks
//
async onPrepare() {
// Bail before the wipe below, which would otherwise take out the scratch
// directory of whichever suite is already holding the port.
await assertPortsAvailable();
// Start empty, so whatever is in here afterwards came from this run.
removeDir(TEMP_DIR);
fs.mkdirSync(TEMP_DIR, { recursive: true });
// Severe, so a failed download aborts the run and is never a spec failure.
await downloadDatasets().catch((err: Error) => {
throw new SevereServiceError(err.message);
});
TEST_DATASETS.forEach(({ name }) => linkCachedDataset(name));
},
async onComplete(exitCode, completedConfig) {
// A failed run keeps its directory: the screenshots and downloads in it are
// what there is to look at. Watch mode reports 0 whatever the tests did,
// since quitting it is not a failure. CI keeps its directory either way,
// because a passing run is exactly where a new baseline image gets picked
// up from the uploaded artifact.
if (exitCode === 0 && !completedConfig.watch && !IS_CI) {
removeDir(TEMP_DIR);
}
},
async before(
_capabilities:
| Capabilities.RequestedStandaloneCapabilities
| Capabilities.RequestedMultiremoteCapabilities,
_specs: string[],
browser: any
) {
await applyTestViewport(browser);
},
async afterCommand(commandName: string) {
// After navigation, inject console interceptor to stringify errors
if (commandName === 'navigateTo' || commandName === 'url') {
await browser.execute(() => {
if (!(console.error as any).__patched) {
const originalError = console.error;
console.error = (...args: any[]) => {
const stringArgs = args.map((arg) =>
arg instanceof Error ? `${arg.name}: ${arg.message}` : arg
);
originalError.apply(console, stringArgs);
};
(console.error as any).__patched = true;
}
});
}
},
};