Skip to content

Commit fe31ecb

Browse files
fix: Use URL constructor for window.location mocking
Simplifies the mock implementation and avoids TypeScript type conflicts
1 parent 15cfa35 commit fe31ecb

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@ import {
88
import { DEFAULT_INSPECTOR_CONFIG } from "../../lib/constants";
99
import { InspectorConfig } from "../../lib/configurationTypes";
1010

11-
// Type for window with mutable location
12-
type WindowWithMutableLocation = Window & {
13-
location: Location;
14-
};
15-
1611
// Helper to mock window.location
1712
function mockWindowLocation(href: string) {
18-
const windowWithLocation = window as unknown as WindowWithMutableLocation;
19-
delete (windowWithLocation as WindowWithMutableLocation & { location?: Location }).location;
20-
21-
windowWithLocation.location = {
22-
...window.location,
23-
href,
24-
} as Location;
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+
});
2519
}
2620

2721
describe("configUtils", () => {
@@ -103,8 +97,11 @@ describe("configUtils", () => {
10397
});
10498

10599
afterEach(() => {
106-
const windowWithLocation = window as unknown as WindowWithMutableLocation;
107-
windowWithLocation.location = originalLocation;
100+
Object.defineProperty(window, "location", {
101+
value: originalLocation,
102+
writable: true,
103+
configurable: true,
104+
});
108105
});
109106

110107
test("returns transport type from URL query parameter", () => {
@@ -148,8 +145,11 @@ describe("configUtils", () => {
148145
});
149146

150147
afterEach(() => {
151-
const windowWithLocation = window as unknown as WindowWithMutableLocation;
152-
windowWithLocation.location = originalLocation;
148+
Object.defineProperty(window, "location", {
149+
value: originalLocation,
150+
writable: true,
151+
configurable: true,
152+
});
153153
});
154154

155155
test("returns serverUrl from query parameter", () => {
@@ -192,8 +192,11 @@ describe("configUtils", () => {
192192
});
193193

194194
afterEach(() => {
195-
const windowWithLocation = window as unknown as WindowWithMutableLocation;
196-
windowWithLocation.location = originalLocation;
195+
Object.defineProperty(window, "location", {
196+
value: originalLocation,
197+
writable: true,
198+
configurable: true,
199+
});
197200
});
198201

199202
test("returns serverCommand from query parameter", () => {
@@ -226,8 +229,11 @@ describe("configUtils", () => {
226229
});
227230

228231
afterEach(() => {
229-
const windowWithLocation = window as unknown as WindowWithMutableLocation;
230-
windowWithLocation.location = originalLocation;
232+
Object.defineProperty(window, "location", {
233+
value: originalLocation,
234+
writable: true,
235+
configurable: true,
236+
});
231237
});
232238

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

0 commit comments

Comments
 (0)