perf(web): optimize vitest performance and add testing guide

This commit is contained in:
yuez committed 2026-10-27 17:30:40 +08:00
1 parent e910e75849
commit 5139adf915
5 files changed
+171 -29

No files matched your search

+27 -11
View File
@@ -5,6 +5,25 @@ import { defineConfig } from "vitest/config";
const DAEMON = "http://127.0.0.1:7676";
const REPO_ROOT = path.resolve(import.meta.dirname, "..");
/**
* Dev/preview proxy for one daemon surface.
*
* `changeOrigin` rewrites `Host` only, so the browser's own `Origin`
* (`http://localhost:5173`) reaches the daemon — whose allowlist is derived from
* the daemon's own port — and every POST/WS upgrade would be rejected with
* `403 origin is not allowed`. Presenting the daemon's own origin (always in that
* derived allowlist) keeps `pnpm --dir web dev` working without `PI2_ORIGINS`,
* and keeps the daemon's Origin check on rather than bypassing it.
*/
function daemonProxy({ ws = false } = {}) {
return {
target: DAEMON,
changeOrigin: true,
headers: { origin: DAEMON },
...(ws ? { ws: true } : {}),
};
}
export default defineConfig({
plugins: [react()],
resolve: {
@@ -22,21 +41,14 @@ export default defineConfig({
port: 5173,
strictPort: false,
proxy: {
"/api": {
target: DAEMON,
changeOrigin: true,
},
"/ws": {
target: DAEMON,
changeOrigin: true,
ws: true,
},
"/api": daemonProxy(),
"/ws": daemonProxy({ ws: true }),
},
},
preview: {
proxy: {
"/api": { target: DAEMON, changeOrigin: true },
"/ws": { target: DAEMON, changeOrigin: true, ws: true },
"/api": daemonProxy(),
"/ws": daemonProxy({ ws: true }),
},
},
test: {
@@ -45,6 +57,10 @@ export default defineConfig({
setupFiles: ["./tests/setup.ts"],
include: ["tests/**/*.test.{ts,tsx}"],
css: true,
// Reuse environment instances across test files within workers to avoid
// recreating jsdom 35 times (which accounted for ~52% of CPU time).
isolate: false,
testTimeout: 15000,
restoreMocks: true,
},
});