-
Notifications
You must be signed in to change notification settings - Fork 13
/
web-test-runner.config.ts
226 lines (213 loc) · 6.95 KB
/
web-test-runner.config.ts
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
import { platform } from 'node:os';
import { parseArgs } from 'node:util';
import { litSsrPlugin } from '@lit-labs/testing/web-test-runner-ssr-plugin.js';
import {
defaultReporter,
type TestRunnerConfig,
type TestRunnerCoreConfig,
type TestRunnerGroupConfig,
} from '@web/test-runner';
import { a11ySnapshotPlugin } from '@web/test-runner-commands/plugins';
import {
type PlaywrightLauncherArgs,
playwrightLauncher,
type PlaywrightLauncher,
} from '@web/test-runner-playwright';
import { puppeteerLauncher } from '@web/test-runner-puppeteer';
import { visualRegressionPlugin } from '@web/test-runner-visual-regression/plugin';
import { initCompiler } from 'sass';
import {
configureRemotePlaywrightBrowser,
minimalReporter,
patchedSummaryReporter,
containerPlaywrightBrowserPlugin,
visualRegressionConfig,
vitePlugin,
preloadIcons,
preloadFonts,
} from './tools/web-test-runner/index.js';
const { values: cliArgs } = parseArgs({
strict: false,
options: {
file: { type: 'string' },
ci: { type: 'boolean', default: !!process.env.CI },
debug: { type: 'boolean' },
'all-browsers': { type: 'boolean', short: 'a' },
firefox: { type: 'boolean' },
webkit: { type: 'boolean' },
parallel: { type: 'boolean' },
'update-visual-baseline': { type: 'boolean' },
group: { type: 'string' },
ssr: { type: 'boolean' },
container: { type: 'boolean' },
local: { type: 'boolean' },
},
});
const concurrency = cliArgs.parallel ? {} : { concurrency: 1 };
const launchOptions: PlaywrightLauncherArgs = {
launchOptions: {
ignoreDefaultArgs: ['--hide-scrollbars'],
// Enables focusing links with tab on Firefox, probably only relevant on macOS.
firefoxUserPrefs: { 'accessibility.tabfocus': 7 },
},
};
const stylesCompiler = initCompiler();
const renderStyles = (): string =>
stylesCompiler.compile('./src/elements/core/styles/standard-theme.scss', {
loadPaths: ['.', './node_modules/'],
}).css;
const browsers =
cliArgs.ci || cliArgs['all-browsers']
? // Parallelism has problems, we need force concurrency to 1
(['chromium', 'firefox', 'webkit'] as const).map((product) =>
playwrightLauncher({
product,
createPage: ({ context }) =>
context.newPage().then((page) => {
page.on('console', (message) => {
if (message.type() === 'error' && !message.location().url.includes('dummy.png')) {
console.error(`CONSOLE: ${product} ${page.url()}`);
console.error(message.location());
console.error(message.text());
}
});
page.on('pageerror', (err) => {
console.error(`PAGEERROR: ${product} ${page.url()}`);
console.error(err);
});
return page;
}),
...concurrency,
...launchOptions,
}),
)
: cliArgs.firefox
? [playwrightLauncher({ product: 'firefox', ...launchOptions })]
: cliArgs.webkit
? [playwrightLauncher({ product: 'webkit', ...launchOptions })]
: cliArgs.debug
? [
puppeteerLauncher({
launchOptions: { headless: false, devtools: true },
...concurrency,
}),
]
: [playwrightLauncher({ product: 'chromium', ...launchOptions })];
const preloadedIcons = await preloadIcons();
const preloadedFonts = await preloadFonts();
const testRunnerHtml = (
testFramework: string,
_config: TestRunnerCoreConfig,
group?: TestRunnerGroupConfig,
): string => `
<!DOCTYPE html>
<html lang='en'>
<head>${
// Although we provide the fonts as base64, we preload the original
// files which prevents a bug in Safari rendering special characters.
['Roman', 'Bold', 'Light']
.map(
(type) => `
<link
rel="preload"
href="https://cdn.app.sbb.ch/fonts/v1_6_subset/SBBWeb-${type}.woff2"
as="font"
type="font/woff2"
crossorigin="anonymous"
/>`,
)
.join('')
}
<link rel="modulepreload" href="/src/elements/core/testing/test-setup.ts" />
<style type="text/css">${renderStyles()}</style>
<style type="text/css">
${preloadedFonts
.map(
(f) => `
@font-face {
font-family: SBB;
src: ${f.font};
font-display: fallback;
font-weight: ${f.weight};
}`,
)
.join('')}
</style>
<script type="module">
globalThis.testEnv = '${cliArgs.debug ? 'debug' : ''}';
globalThis.testGroup = '${cliArgs.ssr ? 'ssr' : (group?.name ?? 'default')}';
globalThis.testRunScript = '${testFramework}';
</script>
</head>
<body class="sbb-disable-animation">${preloadedIcons
.map(
(i) => `
<template id="icon:${i.namespace}:${i.icon}">${i.svg}</template>`,
)
.join('')}
<script type="module" src="/src/elements/core/testing/test-setup.ts"></script>
</body>
</html>
`;
// A list of log messages, that should not be printed to the test console.
const suppressedLogs = [
'Lit is in dev mode. Not recommended for production! See https://lit.dev/msg/dev-mode for more information.',
'[vite] connecting...',
'[vite] connected.',
];
const testFile = typeof cliArgs.file === 'string' && cliArgs.file ? cliArgs.file : undefined;
const groups: TestRunnerGroupConfig[] = [
{ name: 'ssr', files: testFile ?? 'src/**/*.ssr.spec.ts', testRunnerHtml },
];
// The visual regression test group is only added when explicitly set, as the tests are very expensive.
if (cliArgs.group === 'visual-regression') {
groups.push({
name: 'visual-regression',
files: testFile ?? 'src/**/*.visual.spec.ts',
testRunnerHtml,
});
if (!cliArgs.local && platform() !== 'linux') {
console.log(
`Running visual regression tests in a non-linux environment. Switching to container usage. Use --local to opt-out.`,
);
cliArgs.container = true;
}
}
if (cliArgs.container) {
browsers
.filter((b): b is PlaywrightLauncher => b.type === 'playwright')
.forEach((browser) => configureRemotePlaywrightBrowser(browser));
}
export default {
files: testFile ?? ['src/**/*.spec.ts', '!**/*.{visual,ssr}.spec.ts'],
groups,
nodeResolve: true,
reporters:
cliArgs.debug || !cliArgs.ci
? [defaultReporter(), patchedSummaryReporter()]
: [minimalReporter()],
browsers: browsers,
concurrentBrowsers: 3,
plugins: [
a11ySnapshotPlugin(),
litSsrPlugin(),
vitePlugin(),
visualRegressionPlugin({
...visualRegressionConfig,
update: !!cliArgs['update-visual-baseline'],
}),
...(cliArgs.container ? [containerPlaywrightBrowserPlugin()] : []),
],
testFramework: {
config: {
timeout: '10000',
slow: '1000',
failZero: true,
},
},
coverageConfig: {
exclude: ['**/node_modules/**/*', '**/assets/*.svg', '**/assets/*.png', '**/*.scss'],
},
filterBrowserLogs: (log) => !suppressedLogs.includes(log.args[0]),
testRunnerHtml,
} satisfies TestRunnerConfig;