forked from OpenEnergyTools/scl-communication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-test-runner.config.js
More file actions
191 lines (170 loc) · 6.25 KB
/
web-test-runner.config.js
File metadata and controls
191 lines (170 loc) · 6.25 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
/* eslint-disable no-undef */
import { visualRegressionPlugin } from '@web/test-runner-visual-regression/plugin';
import { playwrightLauncher } from '@web/test-runner-playwright';
import { polyfill } from '@web/dev-server-polyfill';
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'];
const browsers = [
playwrightLauncher({ product: 'chromium' }),
playwrightLauncher({ product: 'firefox' }),
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'),
getImageDiff: options => {
const result = defaultGetImageDiff(options);
if (result.diffPercentage < thresholdPercentage) {
result.diffPercentage = 0;
}
return result;
},
}),
polyfill({
scopedCustomElementRegistry: true,
}),
],
testsFinishTimeout: 15000,
groups: [
{
name: 'unit',
files: 'dist/**/*.spec.js',
browsers: [playwrightLauncher({ product: 'chromium' })],
},
{
name: 'visual',
files: 'dist/**/*.test.js',
/** Browsers to run tests on */
browsers,
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/css?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>
* {
--oscd-primary: var(#2aa198);
--oscd-secondary: var(--oscd-theme-secondary, #6c71c4);
--oscd-error: var(--oscd-theme-error, #dc322f);
--oscd-base03: var(--oscd-theme-base03, #002b36);
--oscd-base02: var(--oscd-theme-base02, #073642);
--oscd-base01: var(--oscd-theme-base01, #586e75);
--oscd-base00: var(--oscd-theme-base00, #657b83);
--oscd-base0: var(--oscd-theme-base0, #839496);
--oscd-base1: var(--oscd-theme-base1, #93a1a1);
--oscd-base2: var(--oscd-theme-base2, #eee8d5);
--oscd-base3: var(--oscd-theme-base3, #fdf6e3);
}
</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: 1,
// See documentation for all available options
});