From dd1b8119302bb2d721ba236ee75821dec64be7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Wed, 15 Jul 2026 11:33:37 +0100 Subject: [PATCH] fix(web): silence Vitest network noise (#1700) --- apps/web/vitest.config.ts | 11 +++++++++++ apps/web/vitest.setup.ts | 8 ++++++++ .../0037-resource-aware-frontend-unit-tests.md | 3 +++ 3 files changed, 22 insertions(+) diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.ts index 7e8aa23c32..5f60d7caaa 100644 --- a/apps/web/vitest.config.ts +++ b/apps/web/vitest.config.ts @@ -2,6 +2,8 @@ import { defineConfig, mergeConfig } from "vitest/config"; import viteConfig from "./vite.config"; +if (process.env.DEBUG === "1") process.env.DEBUG = ""; + const configuredMaxWorkers = process.env.VITEST_MAX_WORKERS?.trim(); const maxWorkers = resolveMaxWorkers(configuredMaxWorkers, Boolean(process.env.CI)); @@ -10,6 +12,15 @@ export default mergeConfig( defineConfig({ test: { environment: "happy-dom", + environmentOptions: { + happyDOM: { + settings: { + navigation: { + disableChildFrameNavigation: true, + }, + }, + }, + }, setupFiles: ["./vitest.setup.ts"], exclude: ["e2e/**", "node_modules/**"], pool: "threads", diff --git a/apps/web/vitest.setup.ts b/apps/web/vitest.setup.ts index 8b059008b1..1c565007bb 100644 --- a/apps/web/vitest.setup.ts +++ b/apps/web/vitest.setup.ts @@ -1,3 +1,5 @@ +import type { Window as HappyDOMWindow } from "happy-dom"; + function createLocalStorageMock(): Storage { const store = new Map(); @@ -31,6 +33,12 @@ Object.defineProperty(globalThis, "localStorage", { }); if (typeof window !== "undefined") { + const happyDOMWindow = window as unknown as HappyDOMWindow; + happyDOMWindow.happyDOM.settings.fetch.interceptor = { + beforeAsyncRequest: ({ window: requestWindow }) => + Promise.resolve(new requestWindow.Response(null, { status: 404 })), + }; + Object.defineProperty(window, "localStorage", { configurable: true, value: localStorageMock, diff --git a/docs/decisions/0037-resource-aware-frontend-unit-tests.md b/docs/decisions/0037-resource-aware-frontend-unit-tests.md index 7e2827356f..bd3c24c749 100644 --- a/docs/decisions/0037-resource-aware-frontend-unit-tests.md +++ b/docs/decisions/0037-resource-aware-frontend-unit-tests.md @@ -14,6 +14,8 @@ On a 10-logical-CPU development host, the old uncapped suite averaged roughly 7. Kandev keeps Vitest as the frontend unit-test runner and aligns it with the application's current Vite major. Local full-suite runs use the worker-thread pool and at most 20 percent of the host's available parallelism. `pool: "threads"` is explicit because Vitest 2+ defaults to `forks`; the suite's isolation trial showed that `forks` produces widespread cross-file mock and DOM failures. CI remains uncapped so dedicated runners can use their assigned capacity, and `VITEST_MAX_WORKERS` can override either default for an explicit execution environment when it is a positive integer or percentage. Vitest is upgraded to 4.1.10; Vite remains locked at 8.0.16 rather than the incompatible 8.1.4. +The unit-test environment disables Happy DOM child-frame navigation and intercepts otherwise unmocked Happy DOM requests with a deterministic non-success response, so component tests cannot make real requests to its default `http://localhost:3000` origin. Tests that explicitly replace `fetch` retain full control over their response. The Vitest config also clears the generic inherited `DEBUG=1` value, which otherwise enables Tailwind's per-file transform diagnostics; namespaced debug values remain available for intentional tooling diagnostics. + Targeted test-file runs remain the normal development loop. Full frontend suites run during final verification and CI rather than after every edit. Per-file isolation remains enabled because the suite relies on module and DOM isolation. ## Consequences @@ -22,6 +24,7 @@ Targeted test-file runs remain the normal development loop. Full frontend suites - A local full-suite run takes longer when it is the only workload, but overlapping runs cause less contention and thermal pressure. - CI performance is unchanged unless the CI environment explicitly sets `VITEST_MAX_WORKERS`. - Vitest and Vite share a supported dependency generation instead of loading a second legacy Vite toolchain. +- Unit tests do not load iframe content, access the network through Happy DOM, or inherit the host's generic debug verbosity. - The percentage limit scales across developer machines without hard-coding a workstation-specific core count. - Vite updates require a production-build smoke check until the Rolldown re-export regression is fixed upstream.