Skip to content

Commit 15cfa35

Browse files
fix: Use type-safe window.location mocking in tests
Replace any type with proper TypeScript types to satisfy linter while maintaining test functionality
1 parent 9502ab3 commit 15cfa35

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ 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+
1116
// Helper to mock window.location
1217
function mockWindowLocation(href: string) {
13-
// Delete the property first if it exists and is configurable
14-
Reflect.deleteProperty(window, "location");
15-
16-
Object.defineProperty(window, "location", {
17-
value: { ...window.location, href },
18-
writable: true,
19-
configurable: true,
20-
});
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;
2125
}
2226

2327
describe("configUtils", () => {
@@ -99,7 +103,8 @@ describe("configUtils", () => {
99103
});
100104

101105
afterEach(() => {
102-
mockWindowLocation(originalLocation.href);
106+
const windowWithLocation = window as unknown as WindowWithMutableLocation;
107+
windowWithLocation.location = originalLocation;
103108
});
104109

105110
test("returns transport type from URL query parameter", () => {
@@ -143,7 +148,8 @@ describe("configUtils", () => {
143148
});
144149

145150
afterEach(() => {
146-
mockWindowLocation(originalLocation.href);
151+
const windowWithLocation = window as unknown as WindowWithMutableLocation;
152+
windowWithLocation.location = originalLocation;
147153
});
148154

149155
test("returns serverUrl from query parameter", () => {
@@ -186,7 +192,8 @@ describe("configUtils", () => {
186192
});
187193

188194
afterEach(() => {
189-
mockWindowLocation(originalLocation.href);
195+
const windowWithLocation = window as unknown as WindowWithMutableLocation;
196+
windowWithLocation.location = originalLocation;
190197
});
191198

192199
test("returns serverCommand from query parameter", () => {
@@ -219,7 +226,8 @@ describe("configUtils", () => {
219226
});
220227

221228
afterEach(() => {
222-
mockWindowLocation(originalLocation.href);
229+
const windowWithLocation = window as unknown as WindowWithMutableLocation;
230+
windowWithLocation.location = originalLocation;
223231
});
224232

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

0 commit comments

Comments
 (0)