From f7d0242abf89da9864d469bfb32a3acf86c8a755 Mon Sep 17 00:00:00 2001 From: Pixnop <77785313+Pixnop@users.noreply.github.com> Date: Fri, 28 Aug 2026 14:18:35 +0200 Subject: [PATCH] test(renderer): keep the TaskProvider guard throw inside the component The guard test rendered a component that throws and let the throw escape the render. React's dev-only replay rethrows an escaped render error through a synthetic DOM event, jsdom turns that into an uncancelled window "error" event, and Vitest's jsdom environment re-emits such an event as an uncaught exception whenever no other error listener happens to be registered at that moment. That fails the whole run and blames whichever file was running at the time. Catch the throw inside the component instead, so React never sees it. A console.error spy pins the behaviour: React logs for every render throw it has to handle itself, so letting the throw escape again fails this test directly rather than surfacing somewhere else in the run. --- tests/renderer-dom/taskManagerFlows.test.tsx | 27 ++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/renderer-dom/taskManagerFlows.test.tsx b/tests/renderer-dom/taskManagerFlows.test.tsx index 026fd508..8571441a 100644 --- a/tests/renderer-dom/taskManagerFlows.test.tsx +++ b/tests/renderer-dom/taskManagerFlows.test.tsx @@ -1,5 +1,5 @@ import type { ReactElement, ReactNode } from "react" -import { describe, expect, it, vi } from "vitest" +import { describe, expect, it, onTestFinished, vi } from "vitest" import { act, renderHook, waitFor } from "@testing-library/react" import type { RenderHookResult } from "@testing-library/react" @@ -79,7 +79,30 @@ describe("taskReducer", () => { describe("useTaskContext", () => { it("throws when used outside a TaskProvider", () => { - expect(() => renderHook(() => useTaskContext())).toThrow(/must be used within an TaskProvider/) + // The throw is caught inside the component on purpose. A render throw + // that escapes reaches React's dev-only replay, which rethrows it + // through a synthetic DOM event so devtools can see it; jsdom turns that + // into an uncancelled window "error" event, and Vitest's jsdom + // environment re-emits such an event as an uncaught exception that fails + // the whole run and gets pinned on whichever file happened to be running. + const consoleError = vi.spyOn(console, "error") + onTestFinished(() => consoleError.mockRestore()) + + let thrown: unknown + renderHook(() => { + try { + useTaskContext() + } catch (error) { + thrown = error + } + }) + + expect(thrown).toBeInstanceOf(Error) + expect((thrown as Error).message).toMatch(/must be used within an TaskProvider/) + // React logs "The above error occurred in ..." for every render throw it + // has to handle itself, so this fails right here if the throw is ever let + // out of the component again. + expect(consoleError).not.toHaveBeenCalled() }) })