Repository navigation
Expand file tree
/
Copy pathvitest.browser.shared.ts
More file actions
179 lines (165 loc) · 6.06 KB
/
Copy pathvitest.browser.shared.ts
File metadata and controls
179 lines (165 loc) · 6.06 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
import type { BrowserCommandContext, BrowserConfigOptions } from 'vitest/node';
import { playwright } from '@vitest/browser-playwright';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = fileURLToPath(new URL('.', import.meta.url));
/**
* Deterministic glyphs for visual baselines: the fontconfig selects the
* Liberation 2.1.5 files bundled in `packages/csc-ui/src/test/fonts` ahead of
* any system font and aliases every generic family onto them. Without it
* `sans-serif` is WenQuanYi Zen Hei in the devcontainer and DejaVu Sans on
* ubuntu-latest, and the distros' own Liberation copies differ in version
* (1.07.4 vs 2.1.5) (ADR-0049).
*/
export const FONTS_CONF = path.join(
root,
'packages/csc-ui/src/test/fonts.conf',
);
const FONTS_DIR = path.join(root, 'packages/csc-ui/src/test/fonts');
/**
* Chromium reports a ResizeObserver callback that changed layout in the same
* frame through the window `error` event. It is delivered as an unhandled
* error but is not a component fault.
*/
export const RESIZE_OBSERVER_NOTICE =
/ResizeObserver loop completed with undelivered notifications/;
export const ignoreBenignBrowserNotices = (error: {
message: string;
}): boolean => !RESIZE_OBSERVER_NOTICE.test(error.message);
export type Engine = 'chromium' | 'firefox' | 'webkit';
/**
* The engines `TEST_BROWSERS` lists (comma-separated), Chromium alone when it
* is unset. CI runs the behaviour specs on all three (ADR-0077); the
* devcontainer has only Chromium, since Playwright's Firefox and WebKit need
* system libraries its arm64 image lacks.
*/
export const testEngines = (): Engine[] => {
const engines = (process.env.TEST_BROWSERS ?? 'chromium')
.split(',')
.map((engine) => engine.trim())
.filter(Boolean);
for (const engine of engines) {
if (!['chromium', 'firefox', 'webkit'].includes(engine)) {
throw new Error(`TEST_BROWSERS: unknown engine "${engine}"`);
}
}
return engines as Engine[];
};
const browserContext = {
colorScheme: 'light',
deviceScaleFactor: 1,
// Components honour prefers-reduced-motion (c-modal, c-tabs): no
// open/close keyframes to wait for.
reducedMotion: 'reduce',
} as const;
const providers = {
chromium: () =>
playwright({
contextOptions: browserContext,
launchOptions: {
args: ['--font-render-hinting=none', '--force-device-scale-factor=1'],
env: { ...process.env, FONTCONFIG_FILE: FONTS_CONF },
// Unset → Playwright resolves PLAYWRIGHT_BROWSERS_PATH (/ms-playwright
// in the devcontainer, ~/.cache/ms-playwright in CI).
executablePath: process.env.PLAYWRIGHT_CHROMIUM_PATH || undefined,
},
}),
// Firefox and WebKit run behaviour specs only, never visual baselines, but
// read the same pinned fonts: specs that measure text (c-breadcrumb's fold,
// c-tab-buttons' overflow) then see the same glyph widths.
firefox: () =>
playwright({
contextOptions: browserContext,
launchOptions: {
env: { ...process.env, FONTCONFIG_FILE: FONTS_CONF },
// The content-process sandbox reads only the system font directories;
// without this the bundled Liberation files are invisible and every
// family falls back to DejaVu, about 15% wider.
firefoxUserPrefs: {
'security.sandbox.content.read_path_whitelist': `${FONTS_DIR}/`,
},
},
}),
webkit: () =>
playwright({
contextOptions: browserContext,
launchOptions: { env: { ...process.env, FONTCONFIG_FILE: FONTS_CONF } },
}),
} satisfies Record<Engine, () => BrowserConfigOptions['provider']>;
/**
* The browser block every Vitest browser project in the monorepo shares:
* headless Playwright, a desktop viewport (c-dropdown goes mobile at ≤760px),
* reduced motion, pinned fonts, and the visual-baseline comparator. `name` is
* the Chromium instance's name; another engine's instance is
* `<name> (<engine>)`.
*/
export const browserProject = (
name: string,
engines: readonly Engine[] = ['chromium'],
): BrowserConfigOptions => ({
commands: {
/**
* Switch the page's `forced-colors` for a spec that asserts how a
* component draws in a forced-colors mode (Windows High Contrast). Restore
* `none` after.
*/
emulateForcedColors: async (
ctx: BrowserCommandContext,
value: 'active' | 'none',
) => {
await ctx.page.emulateMedia({ forcedColors: value });
},
/**
* Switch the page's `prefers-reduced-motion` for a spec that asserts the
* motion itself. The context default (`browserContext`) is `reduce`;
* restore it after.
*/
emulateReducedMotion: async (
ctx: BrowserCommandContext,
value: 'no-preference' | 'reduce',
) => {
await ctx.page.emulateMedia({ reducedMotion: value });
},
},
enabled: true,
expect: {
toMatchScreenshot: {
comparatorName: 'pixelmatch',
// A 1% budget of differing pixels plus a per-pixel YIQ tolerance for
// arm64 vs x64 anti-aliasing. Tune after the first CI run; never raise
// past the point where a token flip would pass.
comparatorOptions: {
allowedMismatchedPixelRatio: 0.01,
threshold: 0.2,
},
// The default appends `-${platform}`; every run is linux, so drop it.
// If arm64/x64 ever diverge past tolerance, append `-${process.arch}`
// here and author one set per arch.
resolveScreenshotPath: ({
arg,
browserName,
ext,
root: projectRoot,
screenshotDirectory,
testFileDirectory,
testFileName,
}) =>
path.join(
projectRoot,
testFileDirectory,
screenshotDirectory,
testFileName,
`${arg}-${browserName}${ext}`,
),
},
},
headless: true,
instances: engines.map((engine) => ({
browser: engine,
name: engine === 'chromium' ? name : `${name} (${engine})`,
provider: providers[engine](),
})),
// Failure screenshots would land in __screenshots__ next to baselines.
screenshotFailures: false,
viewport: { height: 800, width: 1280 },
});