-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb-test-runner.config.js
219 lines (196 loc) · 7.51 KB
/
web-test-runner.config.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
/* eslint-disable import/no-extraneous-dependencies */
import { visualRegressionPlugin } from '@web/test-runner-visual-regression/plugin';
import { playwrightLauncher } from '@web/test-runner-playwright';
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
const fuzzy = ['win32', 'darwin'].includes(process.platform); // allow for 1% difference on non-linux OSs
const local = !process.env.CI;
console.assert(local, 'Running in CI!');
console.assert(!fuzzy, 'Running on OS with 1% test pixel diff threshold!');
const thresholdPercentage = fuzzy && local ? 1 : 0;
const filteredLogs = [
'Running in dev mode',
'Lit is in dev mode',
'mwc-list-item scheduled an update',
];
const browsers = [
playwrightLauncher({ product: 'chromium' }),
playwrightLauncher({ product: 'firefox' }),
];
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'),
getImageDiff: (options) => {
const result = defaultGetImageDiff(options);
if (result.diffPercentage < thresholdPercentage)
result.diffPercentage = 0;
return result;
}
}),
],
groups: [
{
name: 'visual',
files: 'dist/**/*.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/icon?family=Material+Symbols+Outlined">
</head>
<body>
<script src="/node_modules/@webcomponents/scoped-custom-element-registry/scoped-custom-element-registry.min.js"></script>
<style class="deanimator">
*, *::before, *::after {
-moz-transition: none !important;
transition: none !important;
-moz-animation: none !important;
animation: none !important;
}
</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;
}
body {
background: white;
}
</style>
</body>
</html>`,
},
{
name: 'unit',
files: 'dist/**/*.spec.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/icon?family=Material+Symbols+Outlined">
</head>
<body>
<script src="/node_modules/@webcomponents/scoped-custom-element-registry/scoped-custom-element-registry.min.js"></script>
<style class="deanimator">
*, *::before, *::after {
-moz-transition: none !important;
transition: none !important;
-moz-animation: none !important;
animation: none !important;
}
</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;
}
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,
// See documentation for all available options
});