fix(test): drain React scheduler macrotask before jsdom teardown

React 19 defers passive-effect flushes onto a setImmediate callback that
reads window.event. When one was still queued as vitest tore down the
jsdom environment, it fired after window was deleted and surfaced as an
unhandled 'window is not defined' error, failing the run with exit 1
despite all tests passing. Drain the macrotask queue in afterEach so any
pending callback runs while window still exists.
This commit is contained in:
MHSanaei 2026-06-01 09:03:47 +02:00
parent cfd3b34362
commit ed21cf836d
No known key found for this signature in database
GPG key ID: 7E4060F2FBE5AB7A
2 changed files with 14 additions and 2 deletions

View file

@ -292,7 +292,7 @@ export const InboundSchema = z.object({
listen: z.string(), listen: z.string(),
nodeId: z.number().int().nullable().optional(), nodeId: z.number().int().nullable().optional(),
port: z.number().int().min(1).max(65535), port: z.number().int().min(1).max(65535),
protocol: z.enum(['vmess', 'vless', 'trojan', 'shadowsocks', 'wireguard', 'hysteria', 'http', 'mixed', 'tunnel']), protocol: z.enum(['vmess', 'vless', 'trojan', 'shadowsocks', 'wireguard', 'hysteria', 'http', 'mixed', 'tunnel', 'tun']),
remark: z.string(), remark: z.string(),
settings: z.unknown(), settings: z.unknown(),
sniffing: z.unknown(), sniffing: z.unknown(),

View file

@ -58,7 +58,19 @@ if (!i18next.isInitialized) {
}); });
} }
afterEach(() => { afterEach(async () => {
cleanup(); cleanup();
document.body.innerHTML = ''; document.body.innerHTML = '';
/*
* React 19 defers passive-effect flushes onto a macrotask (setImmediate),
* whose callback reads `window.event`. If one is still queued when vitest
* tears down the jsdom environment, it fires after `window` is gone and
* throws "window is not defined". Drain a few macrotask ticks here so any
* pending callback runs while `window` still exists. Several ticks are used
* because a microtask resolving mid-drain (rc-trigger/AntD) can queue a new
* one behind the first.
*/
for (let i = 0; i < 3; i += 1) {
await new Promise((resolve) => setImmediate(resolve));
}
}); });