-
Notifications
You must be signed in to change notification settings - Fork 1
/
web-test-runner.config.mjs
210 lines (188 loc) · 6.06 KB
/
web-test-runner.config.mjs
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
import { visualRegressionPlugin } from '@web/test-runner-visual-regression/plugin';
import { playwrightLauncher } from '@web/test-runner-playwright';
import path from 'path';
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
const fuzzy = ['win32', 'darwin'].includes(process.platform); // allow for difference on non-linux OSs
const local = !process.env.CI;
const thresholdPercentage = fuzzy && !local ? 0.2 : 0;
// eslint-disable-next-line no-console
console.assert(local, 'Running in CI!');
// eslint-disable-next-line no-console
console.assert(
!fuzzy,
`Running on OS with ${thresholdPercentage}% test pixel diff threshold!`
);
const filteredLogs = [
'Running in dev mode',
'Lit is in dev mode',
'scheduled an update',
];
// Consistency in fonts is hard without containers, see:
// https://github.com/microsoft/playwright/issues/20097
// https://github.com/puppeteer/puppeteer/issues/661
const browsers = [
playwrightLauncher({
product: 'chromium',
launchOptions: {
args: [
'--font-render-hinting=none',
'--disable-skia-runtime-opts',
'--disable-font-subpixel-positioning',
'--disable-lcd-text',
'--disable-gpu',
],
},
}),
playwrightLauncher({ product: 'firefox' }),
// TODO: Webkit disabled as unable to get consistent screenshots between
// local and CI environment
// playwrightLauncher({ product: 'webkit' }),
];
function defaultGetImageDiff({ baselineImage, image, options }) {
let error = '';
let basePng = PNG.sync.read(baselineImage);
let png = PNG.sync.read(image);
let { width, height } = png;
if (basePng.width !== png.width || basePng.height !== png.height) {
error =
`Screenshot is not the same width and height as the baseline. ` +
`Baseline: { width: ${basePng.width}, height: ${basePng.height} }` +
`Screenshot: { width: ${png.width}, height: ${png.height} }`;
width = Math.max(basePng.width, png.width);
height = Math.max(basePng.height, png.height);
let oldPng = basePng;
basePng = new PNG({ width, height });
oldPng.data.copy(basePng.data, 0, 0, oldPng.data.length);
oldPng = png;
png = new PNG({ width, height });
oldPng.data.copy(png.data, 0, 0, oldPng.data.length);
}
const diff = new PNG({ width, height });
const numDiffPixels = pixelmatch(
basePng.data,
png.data,
diff.data,
width,
height,
options
);
const diffPercentage = (numDiffPixels / (width * height)) * 100;
return {
error,
diffImage: PNG.sync.write(diff),
diffPercentage,
};
}
export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
plugins: [
visualRegressionPlugin({
update: process.argv.includes('--update-visual-baseline'),
baseDir: 'test/screenshots',
diffOptions: {
includeAA: true,
},
getImageDiff: options => {
const result = defaultGetImageDiff(options);
if (result.diffPercentage < thresholdPercentage)
result.diffPercentage = 0;
return result;
},
getBaselineName: ({ browser, name }) =>
path.join('baseline', `${name}-${browser}`),
getDiffName: ({ browser, name }) =>
path.join('failed', `${name}-${browser}-diff`),
getFailedName: ({ browser, name }) =>
path.join('failed', `${name}-${browser}`),
}),
],
groups: [
{
name: 'visual',
files: 'dist/test/**/*.test.js',
testRunnerHtml: testFramework => `
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300&family=Roboto:wght@300;400;500&display=swap" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined&display=block" />
</head>
<body>
<style class="deanimator">
*, *::before, *::after {
-moz-transition: none !important;
transition: none !important;
-moz-animation: none !important;
animation: none !important;
}
</style>
<style>
* { -webkit-font-smoothing: none;
font-kerning: none;
text-rendering: geometricPrecision;
font-variant-ligatures: none;
letter-spacing: 0.01em;}
</style>
<script>window.process = { env: ${JSON.stringify(process.env)} }</script>
<script type="module" src="${testFramework}"></script>
<script>
function descendants(parent) {
return (Array.from(parent.childNodes)).concat(
...Array.from(parent.children).map(child => descendants(child))
);
}
const deanimator = document.querySelector('.deanimator');
function deanimate(element) {
if (!element.shadowRoot) return;
if (element.shadowRoot.querySelector('.deanimator')) return;
const style = deanimator.cloneNode(true);
element.shadowRoot.appendChild(style);
descendants(element.shadowRoot).forEach(deanimate);
}
const observer = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
descendants(document.body).forEach(deanimate);
}
}
});
observer.observe(document.body, {childList: true, subtree:true});
</script>
<style>
* {
margin: 0px;
padding: 0px;
--mdc-icon-font: 'Material Symbols Outlined';
}
body {
background: white;
}
</style>
</body>
</html>`,
},
],
/** Resolve bare module imports */
nodeResolve: {
exportConditions: ['browser', 'development'],
},
/** Filter out lit dev mode logs */
filterBrowserLogs(log) {
for (const arg of log.args) {
if (typeof arg === 'string' && filteredLogs.some(l => arg.includes(l))) {
return false;
}
}
return true;
},
/** Compile JS for older browsers. Requires @web/dev-server-esbuild plugin */
// esbuildTarget: 'auto',
/** Amount of browsers to run concurrently */
concurrentBrowsers: 3,
/** Amount of test files per browser to test concurrently */
concurrency: 2,
/** Browsers to run tests on */
browsers,
// 30 minutes
testsFinishTimeout: 30 * 60 * 1000,
// See documentation for all available options
});