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:
parent
12385a3ce6
commit
7c0f6548d8
1 changed files with 19 additions and 9 deletions
|
|
@ -85,25 +85,35 @@ Object.defineProperty(window, "sessionStorage", {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mock window.location in a way that works in all environments
|
// Mock window.location in a way that works in all environments
|
||||||
const mockLocation = {
|
const mockLocation: Partial<Location> = {
|
||||||
origin: "http://localhost:3000",
|
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 to delete first, then redefine
|
||||||
try {
|
try {
|
||||||
delete (window as { location?: Location }).location;
|
delete (globalWindow as { location?: Location }).location;
|
||||||
window.location = mockLocation;
|
Object.defineProperty(globalWindow, "location", {
|
||||||
|
value: mockLocation,
|
||||||
|
writable: true,
|
||||||
|
configurable: true,
|
||||||
|
});
|
||||||
} catch {
|
} catch {
|
||||||
// If that fails, try Object.defineProperty with configurable
|
// If that fails, try to just override the origin property
|
||||||
try {
|
try {
|
||||||
Object.defineProperty(window, "location", {
|
Object.defineProperty(globalWindow.location, "origin", {
|
||||||
value: mockLocation,
|
value: "http://localhost:3000",
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
});
|
});
|
||||||
} catch {
|
} catch {
|
||||||
// As a last resort, just assign to window.location
|
// As a last resort, mock what we need for the tests
|
||||||
(window as { location: Location }).location = mockLocation;
|
jest.spyOn(window, "location", "get").mockReturnValue({
|
||||||
|
...window.location,
|
||||||
|
origin: "http://localhost:3000",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue