From 7c0f6548d8cb9740304d037b0c38ec08c4eb085b Mon Sep 17 00:00:00 2001 From: Jerome Date: Fri, 13 Jun 2025 17:17:46 +0100 Subject: [PATCH] fix: resolve TypeScript build error for window.location mock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use Partial type and improve fallback strategies for mocking window.location to work across different test environments while satisfying TypeScript's type checker. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../__tests__/AuthDebugger.test.tsx | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/client/src/components/__tests__/AuthDebugger.test.tsx b/client/src/components/__tests__/AuthDebugger.test.tsx index b9744356..cf85fbe7 100644 --- a/client/src/components/__tests__/AuthDebugger.test.tsx +++ b/client/src/components/__tests__/AuthDebugger.test.tsx @@ -85,25 +85,35 @@ Object.defineProperty(window, "sessionStorage", { }); // Mock window.location in a way that works in all environments -const mockLocation = { +const mockLocation: Partial = { origin: "http://localhost:3000", -} as Location; +}; + +// Use global to access window in a type-safe way +const globalWindow = global as unknown as Window & typeof globalThis; // Try to delete first, then redefine try { - delete (window as { location?: Location }).location; - window.location = mockLocation; + delete (globalWindow as { location?: Location }).location; + Object.defineProperty(globalWindow, "location", { + value: mockLocation, + writable: true, + configurable: true, + }); } catch { - // If that fails, try Object.defineProperty with configurable + // If that fails, try to just override the origin property try { - Object.defineProperty(window, "location", { - value: mockLocation, + Object.defineProperty(globalWindow.location, "origin", { + value: "http://localhost:3000", writable: true, configurable: true, }); } catch { - // As a last resort, just assign to window.location - (window as { location: Location }).location = mockLocation; + // As a last resort, mock what we need for the tests + jest.spyOn(window, "location", "get").mockReturnValue({ + ...window.location, + origin: "http://localhost:3000", + }); } }