fix: properly mock window.location in JSDOM environment
Use JSDOM's ability to delete non-configurable properties to create a proper window.location mock. This approach works reliably across different CI environments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
7c0f6548d8
commit
1c8cec6ed9
1 changed files with 22 additions and 29 deletions
|
|
@ -84,38 +84,31 @@ Object.defineProperty(window, "sessionStorage", {
|
||||||
value: sessionStorageMock,
|
value: sessionStorageMock,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mock window.location in a way that works in all environments
|
// In JSDOM, we can delete window.location even though it's non-configurable
|
||||||
const mockLocation: Partial<Location> = {
|
// This is a quirk of JSDOM that we can use to our advantage
|
||||||
|
delete (window as { location?: Location }).location;
|
||||||
|
|
||||||
|
// Create a mock location object that tracks href changes
|
||||||
|
interface MockLocation extends Partial<Location> {
|
||||||
|
_href: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mockLocation: MockLocation = {
|
||||||
origin: "http://localhost:3000",
|
origin: "http://localhost:3000",
|
||||||
|
_href: "",
|
||||||
|
get href() {
|
||||||
|
return this._href;
|
||||||
|
},
|
||||||
|
set href(value: string) {
|
||||||
|
this._href = value;
|
||||||
|
},
|
||||||
|
assign: jest.fn(),
|
||||||
|
reload: jest.fn(),
|
||||||
|
replace: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Use global to access window in a type-safe way
|
// Assign the mock to window.location
|
||||||
const globalWindow = global as unknown as Window & typeof globalThis;
|
(window as { location: Location }).location = mockLocation as Location;
|
||||||
|
|
||||||
// Try to delete first, then redefine
|
|
||||||
try {
|
|
||||||
delete (globalWindow as { location?: Location }).location;
|
|
||||||
Object.defineProperty(globalWindow, "location", {
|
|
||||||
value: mockLocation,
|
|
||||||
writable: true,
|
|
||||||
configurable: true,
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// If that fails, try to just override the origin property
|
|
||||||
try {
|
|
||||||
Object.defineProperty(globalWindow.location, "origin", {
|
|
||||||
value: "http://localhost:3000",
|
|
||||||
writable: true,
|
|
||||||
configurable: true,
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// As a last resort, mock what we need for the tests
|
|
||||||
jest.spyOn(window, "location", "get").mockReturnValue({
|
|
||||||
...window.location,
|
|
||||||
origin: "http://localhost:3000",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("AuthDebugger", () => {
|
describe("AuthDebugger", () => {
|
||||||
const defaultAuthState = EMPTY_DEBUGGER_STATE;
|
const defaultAuthState = EMPTY_DEBUGGER_STATE;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue