Skip to content

Commit 7c0f654

Browse files
fix: resolve TypeScript build error for window.location mock
Use Partial<Location> 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 <[email protected]>
1 parent 12385a3 commit 7c0f654

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

client/src/components/__tests__/AuthDebugger.test.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,35 @@ Object.defineProperty(window, "sessionStorage", {
8585
});
8686

8787
// Mock window.location in a way that works in all environments
88-
const mockLocation = {
88+
const mockLocation: Partial<Location> = {
8989
origin: "http://localhost:3000",
90-
} as Location;
90+
};
91+
92+
// Use global to access window in a type-safe way
93+
const globalWindow = global as unknown as Window & typeof globalThis;
9194

9295
// Try to delete first, then redefine
9396
try {
94-
delete (window as { location?: Location }).location;
95-
window.location = mockLocation;
97+
delete (globalWindow as { location?: Location }).location;
98+
Object.defineProperty(globalWindow, "location", {
99+
value: mockLocation,
100+
writable: true,
101+
configurable: true,
102+
});
96103
} catch {
97-
// If that fails, try Object.defineProperty with configurable
104+
// If that fails, try to just override the origin property
98105
try {
99-
Object.defineProperty(window, "location", {
100-
value: mockLocation,
106+
Object.defineProperty(globalWindow.location, "origin", {
107+
value: "http://localhost:3000",
101108
writable: true,
102109
configurable: true,
103110
});
104111
} catch {
105-
// As a last resort, just assign to window.location
106-
(window as { location: Location }).location = mockLocation;
112+
// As a last resort, mock what we need for the tests
113+
jest.spyOn(window, "location", "get").mockReturnValue({
114+
...window.location,
115+
origin: "http://localhost:3000",
116+
});
107117
}
108118
}
109119

0 commit comments

Comments
 (0)