From 1bd8f5119851712f1415160c4df64e91480264a8 Mon Sep 17 00:00:00 2001 From: gsxdsm Date: Mon, 25 May 2026 12:59:48 -0700 Subject: [PATCH] fix(plugins/reports): wire @testing-library/react cleanup so React teardown doesn't leak past test env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The api stub from 43be32bbd silenced the fetch-rejection path but the suite still failed on CI: the actual unhandled error is React's scheduler firing deferred work via setImmediate after jsdom is torn down — its internal render then dereferences `window` and throws ReferenceError. @testing-library only auto-registers cleanup() when vitest `globals: true` is set, and this package doesn't enable globals, so the React tree from each render() stays mounted across teardown. Register cleanup manually in test-setup.ts so every dashboard test unmounts its tree before the environment tears down. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../fusion-plugin-reports/src/dashboard/test-setup.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/fusion-plugin-reports/src/dashboard/test-setup.ts b/plugins/fusion-plugin-reports/src/dashboard/test-setup.ts index 9d53dd2c3..a90051000 100644 --- a/plugins/fusion-plugin-reports/src/dashboard/test-setup.ts +++ b/plugins/fusion-plugin-reports/src/dashboard/test-setup.ts @@ -1,5 +1,13 @@ import "@testing-library/jest-dom/vitest"; -import { vi } from "vitest"; +import { cleanup } from "@testing-library/react"; +import { afterEach, vi } from "vitest"; + +// @testing-library/react only auto-registers cleanup when vitest globals are +// enabled. We don't enable globals here, so we wire it manually — otherwise +// React leaves the test tree mounted, its scheduler fires a deferred update +// via setImmediate after the jsdom environment is torn down, and the suite +// fails with "ReferenceError: window is not defined". +afterEach(() => cleanup()); if (typeof window !== "undefined") { Object.defineProperty(window, "matchMedia", {