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 <noreply@anthropic.com>
This commit is contained in:
Jerome 2025-06-13 17:17:46 +01:00
commit 7c0f6548d8
No known key found for this signature in database

View file

@ -85,25 +85,35 @@ Object.defineProperty(window, "sessionStorage", {
});
// Mock window.location in a way that works in all environments
const mockLocation = {
const mockLocation: Partial<Location> = {
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",
});
}
}