浏览代码

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.
MHSanaei 10 小时之前
父节点
当前提交
ed21cf836d
共有 2 个文件被更改,包括 14 次插入2 次删除
  1. 1 1
      frontend/src/generated/zod.ts
  2. 13 1
      frontend/src/test/setup.components.ts

+ 1 - 1
frontend/src/generated/zod.ts

@@ -292,7 +292,7 @@ export const InboundSchema = z.object({
   listen: z.string(),
   nodeId: z.number().int().nullable().optional(),
   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(),
   settings: z.unknown(),
   sniffing: z.unknown(),

+ 13 - 1
frontend/src/test/setup.components.ts

@@ -58,7 +58,19 @@ if (!i18next.isInitialized) {
   });
 }
 
-afterEach(() => {
+afterEach(async () => {
   cleanup();
   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));
+  }
 });