perf(web): optimize vitest performance and add testing guide
This commit is contained in:
1 parent
e910e75849
commit
5139adf915
5 files changed
+171
-29
No files matched your search
@@ -0,0 +1,25 @@
|
||||
# pi2-web
|
||||
|
||||
React port of the pi2 web UI prototype, built into `web/dist` and served statically by `pi2 daemon`.
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
# Start dev server with proxy to daemon (:7676)
|
||||
pnpm --dir web dev
|
||||
|
||||
# Typecheck
|
||||
pnpm --dir web typecheck
|
||||
|
||||
# Run test suite
|
||||
pnpm --dir web exec vitest run
|
||||
|
||||
# Run only changed tests or specific file
|
||||
pnpm --dir web exec vitest run --changed
|
||||
pnpm --dir web exec vitest run tests/chat.composer.test.tsx
|
||||
|
||||
# Watch mode
|
||||
pnpm --dir web test
|
||||
```
|
||||
|
||||
For performance tuning, concurrency settings, and DOM environment details, see [docs/testing.md](../docs/testing.md).
|
||||
+31
-18
@@ -4,25 +4,38 @@ import { afterEach, vi } from "vitest";
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
localStorage.clear();
|
||||
document.documentElement.removeAttribute("data-theme");
|
||||
if (typeof localStorage !== "undefined") {
|
||||
localStorage.clear();
|
||||
}
|
||||
if (typeof document !== "undefined") {
|
||||
document.documentElement.removeAttribute("data-theme");
|
||||
}
|
||||
});
|
||||
|
||||
// next-themes / responsive helpers expect these in jsdom.
|
||||
if (!window.matchMedia) {
|
||||
Object.defineProperty(window, "matchMedia", {
|
||||
writable: true,
|
||||
value: vi.fn().mockImplementation((query: string) => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addListener: vi.fn(),
|
||||
removeListener: vi.fn(),
|
||||
addEventListener: vi.fn(),
|
||||
removeEventListener: vi.fn(),
|
||||
dispatchEvent: vi.fn(),
|
||||
})),
|
||||
});
|
||||
if (typeof window !== "undefined") {
|
||||
// next-themes / responsive helpers expect these in jsdom.
|
||||
if (!window.matchMedia) {
|
||||
Object.defineProperty(window, "matchMedia", {
|
||||
writable: true,
|
||||
value: vi.fn().mockImplementation((query: string) => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addListener: vi.fn(),
|
||||
removeListener: vi.fn(),
|
||||
addEventListener: vi.fn(),
|
||||
dispatchEvent: vi.fn(),
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// jsdom ships stubs that print "Not implemented: ..." errors to stderr.
|
||||
// Replace them with silent vi.fn() mocks.
|
||||
window.scrollTo = vi.fn();
|
||||
}
|
||||
|
||||
if (typeof HTMLCanvasElement !== "undefined") {
|
||||
HTMLCanvasElement.prototype.getContext = vi.fn();
|
||||
}
|
||||
|
||||
if (!globalThis.ResizeObserver) {
|
||||
@@ -34,6 +47,6 @@ if (!globalThis.ResizeObserver) {
|
||||
globalThis.ResizeObserver = ResizeObserverStub;
|
||||
}
|
||||
|
||||
if (!Element.prototype.scrollIntoView) {
|
||||
if (typeof Element !== "undefined") {
|
||||
Element.prototype.scrollIntoView = vi.fn();
|
||||
}
|
||||
+27
-11
@@ -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,
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user