Skip to content

Commit d0985b1

Browse files
fix: Implement proper window.location mocking for Jest with TypeScript
Uses Partial<Window> type assertion to allow delete operation while maintaining type safety. This avoids both TypeScript's 'delete must be optional' error and JSDOM's 'Cannot redefine property' error.
1 parent fe31ecb commit d0985b1

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

client/src/utils/__tests__/configUtils.test.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@ import { InspectorConfig } from "../../lib/configurationTypes";
1010

1111
// Helper to mock window.location
1212
function mockWindowLocation(href: string) {
13-
// Use Object.defineProperty with a getter to avoid type issues
14-
Object.defineProperty(window, "location", {
15-
value: new URL(href),
16-
writable: true,
17-
configurable: true,
18-
});
13+
// Delete using Partial<Window> to satisfy TypeScript
14+
delete (window as Partial<Window>).location;
15+
16+
// Create a complete location mock
17+
const url = new URL(href);
18+
window.location = {
19+
href,
20+
hostname: url.hostname,
21+
pathname: url.pathname,
22+
protocol: url.protocol,
23+
search: url.search,
24+
hash: url.hash,
25+
host: url.host,
26+
origin: url.origin,
27+
port: url.port,
28+
reload: jest.fn(),
29+
replace: jest.fn(),
30+
assign: jest.fn(),
31+
toString: jest.fn(() => href),
32+
ancestorOrigins: {} as DOMStringList,
33+
} as Location;
1934
}
2035

2136
describe("configUtils", () => {
@@ -97,11 +112,8 @@ describe("configUtils", () => {
97112
});
98113

99114
afterEach(() => {
100-
Object.defineProperty(window, "location", {
101-
value: originalLocation,
102-
writable: true,
103-
configurable: true,
104-
});
115+
delete (window as Partial<Window>).location;
116+
window.location = originalLocation;
105117
});
106118

107119
test("returns transport type from URL query parameter", () => {
@@ -145,11 +157,8 @@ describe("configUtils", () => {
145157
});
146158

147159
afterEach(() => {
148-
Object.defineProperty(window, "location", {
149-
value: originalLocation,
150-
writable: true,
151-
configurable: true,
152-
});
160+
delete (window as Partial<Window>).location;
161+
window.location = originalLocation;
153162
});
154163

155164
test("returns serverUrl from query parameter", () => {
@@ -192,11 +201,8 @@ describe("configUtils", () => {
192201
});
193202

194203
afterEach(() => {
195-
Object.defineProperty(window, "location", {
196-
value: originalLocation,
197-
writable: true,
198-
configurable: true,
199-
});
204+
delete (window as Partial<Window>).location;
205+
window.location = originalLocation;
200206
});
201207

202208
test("returns serverCommand from query parameter", () => {
@@ -229,11 +235,8 @@ describe("configUtils", () => {
229235
});
230236

231237
afterEach(() => {
232-
Object.defineProperty(window, "location", {
233-
value: originalLocation,
234-
writable: true,
235-
configurable: true,
236-
});
238+
delete (window as Partial<Window>).location;
239+
window.location = originalLocation;
237240
});
238241

239242
test("returns serverArgs from query parameter", () => {

0 commit comments

Comments
 (0)