-
Notifications
You must be signed in to change notification settings - Fork 206
/
web-test-runner.utils.js
289 lines (275 loc) · 9.28 KB
/
web-test-runner.utils.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { playwrightLauncher } from '@web/test-runner-playwright';
import { visualRegressionPlugin } from '@web/test-runner-visual-regression/plugin';
import fs from 'fs';
import path from 'path';
import fg from 'fast-glob';
export const chromium = playwrightLauncher({
product: 'chromium',
createBrowserContext: ({ browser }) =>
browser.newContext({
ignoreHTTPSErrors: true,
permissions: ['clipboard-read', 'clipboard-write'],
}),
});
export const chromiumWithMemoryTooling = playwrightLauncher({
product: 'chromium',
createBrowserContext: ({ browser }) =>
browser.newContext({
ignoreHTTPSErrors: true,
permissions: ['clipboard-read', 'clipboard-write'],
}),
launchOptions: {
headless: false,
args: [
'--js-flags=--expose-gc',
'--headless=new',
/**
* Cause `measureUserAgentSpecificMemory()` to GC immediately,
* instead of up to 20s later:
* https://web.dev/articles/monitor-total-page-memory-usage#local_testing
**/
'--enable-blink-features=ForceEagerMeasureMemory',
],
},
});
export const chromiumWithMemoryToolingCI = playwrightLauncher({
product: 'chromium',
concurrency: 2,
createBrowserContext: ({ browser }) =>
browser.newContext({
ignoreHTTPSErrors: true,
permissions: ['clipboard-read', 'clipboard-write'],
}),
launchOptions: {
headless: false,
args: [
'--js-flags=--expose-gc',
'--headless=new',
/**
* Cause `measureUserAgentSpecificMemory()` to GC immediately,
* instead of up to 20s later:
* https://web.dev/articles/monitor-total-page-memory-usage#local_testing
**/
'--enable-blink-features=ForceEagerMeasureMemory',
],
},
});
export const chromiumWithFlags = playwrightLauncher({
product: 'chromium',
launchOptions: {
args: ['--enable-experimental-web-platform-features'],
},
createBrowserContext: ({ browser }) =>
browser.newContext({
ignoreHTTPSErrors: true,
permissions: ['clipboard-read', 'clipboard-write'],
}),
});
export const firefox = playwrightLauncher({
product: 'firefox',
concurrency: 1,
createBrowserContext: ({ browser }) =>
browser.newContext({
ignoreHTTPSErrors: true,
}),
launchOptions: {
firefoxUserPrefs: {
'toolkit.telemetry.reportingpolicy.firstRun': false,
'browser.shell.checkDefaultBrowser': false,
'browser.bookmarks.restore_default_bookmarks': false,
'dom.disable_open_during_load': false,
'dom.max_script_run_time': 0,
'dom.min_background_timeout_value': 10,
'extensions.autoDisableScopes': 0,
'extensions.enabledScopes': 15,
'dom.events.asyncClipboard.readText': true,
'dom.events.testing.asyncClipboard': true,
},
},
});
export const webkit = playwrightLauncher({
product: 'webkit',
concurrency: 4,
createBrowserContext: ({ browser }) =>
browser.newContext({
ignoreHTTPSErrors: true,
}),
});
const tools = fs
.readdirSync('tools')
.filter((dir) => fs.statSync(`tools/${dir}`).isDirectory());
export const packages = fs
.readdirSync('packages')
.filter((dir) => fs.statSync(`packages/${dir}`).isDirectory())
.concat(tools);
const vrtHTML =
({ systemVariant, color, scale, dir, reduceMotion, hcm }) =>
(testFramework) =>
`<!doctype html>
<html dir=${dir}>
<head>
<link rel="preconnect" href="https://use.typekit.net" />
<link rel="dns-prefetch" href="https://use.typekit.net" />
<!-- For Adobe Clean font support -->
<link rel="stylesheet" href="https://use.typekit.net/evk7lzt.css" />
<style>
body {
margin: 0;
}
sp-story-decorator {
display: block;
}
</style>
</head>
<body>
<script>
window.__swc_hack_knobs__ = {
defaultSystemVariant: "${systemVariant || ''}",
defaultColor: "${color || ''}",
defaultScale: "${scale || ''}",
defaultDirection: "${dir || ''}",
defaultReduceMotion: ${reduceMotion},
hcm: ${!!hcm},
};
</script>
<script type="module" src="${testFramework}"></script>
</body>
</html>`;
export let vrtGroups = [];
const systemVariants = ['spectrum', 'express', 'spectrum-two'];
const colors = ['light', 'dark'];
const scales = ['medium', 'large'];
const directions = ['ltr', 'rtl'];
systemVariants.forEach((systemVariant) => {
colors.forEach((color) => {
scales.forEach((scale) => {
directions.forEach((dir) => {
const reduceMotion = true;
const testHTML = vrtHTML({
systemVariant,
color,
scale,
dir,
reduceMotion,
});
vrtGroups.push({
name: `vrt-${systemVariant}-${color}-${scale}-${dir}`,
files: '(packages|tools)/*/test/*.test-vrt.js',
testRunnerHtml: testHTML,
browsers: [chromium],
});
});
});
});
});
vrtGroups = [
...vrtGroups,
...packages.reduce((acc, pkg) => {
const skipPkgs = ['bundle', 'modal'];
if (!skipPkgs.includes(pkg)) {
acc.push({
name: `vrt-${pkg}`,
files: `(packages|tools)/${pkg}/test/*.test-vrt.js`,
testRunnerHtml: vrtHTML({
reduceMotion: true,
}),
browsers: [chromium],
});
acc.push({
name: `vrt-${pkg}-single`,
files: `(packages|tools)/${pkg}/test/*.test-vrt.js`,
testRunnerHtml: vrtHTML({
systemVariant: 'spectrum',
color: 'light',
scale: 'medium',
dir: 'ltr',
reduceMotion: true,
}),
browsers: [chromium],
});
}
return acc;
}, []),
{
name: `vrt-hcm`,
files: '(packages|tools)/*/test/*.test-vrt.js',
testRunnerHtml: vrtHTML({
systemVariant: 'spectrum',
color: 'dark',
scale: 'medium',
dir: 'ltr',
hcm: true,
reduceMotion: true,
}),
browsers: [chromium],
},
];
export const configuredVisualRegressionPlugin = () =>
visualRegressionPlugin({
update: process.argv.includes('--update-visual-baseline'),
diffOptions: {
threshold: 0,
},
baseDir: 'test/visual',
buildCache: true,
getBaselineName: ({ browser, name }) => {
const nameParts = name.split(' - ');
return path.join('screenshots-baseline', browser, ...nameParts);
},
getDiffName: ({ browser, name }) => {
const nameParts = name.split(' - ');
return path.join(
'screenshots-actual',
'diff',
browser,
...nameParts
);
},
getFailedName: ({ browser, name }) => {
const nameParts = name.split(' - ');
return path.join(
'screenshots-actual',
'updates',
browser,
...nameParts
);
},
});
export function watchSWC() {
return {
name: 'watch-swc-plugin',
async serverStart({ fileWatcher }) {
// register SWC output files to be watched
const files = await fg(
[
'{packages,projects,tools}/**/*.js',
'{packages,projects,tools}/**/spectrum-*.css',
],
{
ignore: [
'**/*.map',
'**/*.vrt.js',
'**/spectrum-config.js',
],
}
);
for (const file of files) {
fileWatcher.add(process.cwd() + file);
}
// Use the following for reviewing the file changes that are reacted to here...
// fileWatcher.on('change', (path) => {
// console.log(`Process change in: ${path}`);
// });
},
};
}