Skip to content

Commit ce1edd3

Browse files
Make testsuite compatible with node 22.x (#96)
* work around with global navigator in node 22 * fix lint
1 parent 335b44b commit ce1edd3

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
node-version: [18.x, 20.x]
16+
node-version: [18.x, 20.x, 22.x]
1717

1818
steps:
1919
- uses: actions/checkout@v3

test/requestTracing.test.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,23 @@ describe("request tracing", function () {
166166

167167
afterEach(() => {
168168
// Restore the original values after each test
169-
(global as any).navigator = originalNavigator;
169+
// global.navigator was added in node 21, https://nodejs.org/api/globals.html#navigator_1
170+
// global.navigator only has a getter, so we have to use Object.defineProperty to modify it
171+
Object.defineProperty(global, "navigator", {
172+
value: originalNavigator,
173+
configurable: true
174+
});
170175
(global as any).WorkerNavigator = originalWorkerNavigator;
171176
(global as any).WorkerGlobalScope = originalWorkerGlobalScope;
172177
(global as any).importScripts = originalImportScripts;
173178
});
174179

175180
it("should identify WebWorker environment", async () => {
176181
(global as any).WorkerNavigator = function WorkerNavigator() { };
177-
(global as any).navigator = new (global as any).WorkerNavigator();
182+
Object.defineProperty(global, "navigator", {
183+
value: new (global as any).WorkerNavigator(),
184+
configurable: true
185+
});
178186
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
179187
(global as any).importScripts = function importScripts() { };
180188

@@ -188,7 +196,10 @@ describe("request tracing", function () {
188196
});
189197

190198
it("is not WebWorker when WorkerNavigator is undefined", async () => {
191-
(global as any).navigator = { userAgent: "node.js" } as any; // Mock navigator
199+
Object.defineProperty(global, "navigator", {
200+
value: { userAgent: "node.js" } as any, // Mock navigator
201+
configurable: true
202+
});
192203
(global as any).WorkerNavigator = undefined;
193204
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
194205
(global as any).importScripts = function importScripts() { };
@@ -203,7 +214,10 @@ describe("request tracing", function () {
203214
});
204215

205216
it("is not WebWorker when navigator is not an instance of WorkerNavigator", async () => {
206-
(global as any).navigator = { userAgent: "node.js" } as any; // Mock navigator but not an instance of WorkerNavigator
217+
Object.defineProperty(global, "navigator", {
218+
value: { userAgent: "node.js" } as any, // Mock navigator but not an instance of WorkerNavigator
219+
configurable: true
220+
});
207221
(global as any).WorkerNavigator = function WorkerNavigator() { };
208222
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
209223
(global as any).importScripts = function importScripts() { };
@@ -219,7 +233,10 @@ describe("request tracing", function () {
219233

220234
it("is not WebWorker when WorkerGlobalScope is undefined", async () => {
221235
(global as any).WorkerNavigator = function WorkerNavigator() { };
222-
(global as any).navigator = new (global as any).WorkerNavigator();
236+
Object.defineProperty(global, "navigator", {
237+
value: new (global as any).WorkerNavigator(),
238+
configurable: true
239+
});
223240
(global as any).WorkerGlobalScope = undefined;
224241
(global as any).importScripts = function importScripts() { };
225242

@@ -234,7 +251,10 @@ describe("request tracing", function () {
234251

235252
it("is not WebWorker when importScripts is undefined", async () => {
236253
(global as any).WorkerNavigator = function WorkerNavigator() { };
237-
(global as any).navigator = new (global as any).WorkerNavigator();
254+
Object.defineProperty(global, "navigator", {
255+
value: new (global as any).WorkerNavigator(),
256+
configurable: true
257+
});
238258
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
239259
(global as any).importScripts = undefined;
240260

@@ -345,4 +365,4 @@ describe("request tracing", function () {
345365
expect(correlationContext.includes("Host=Web")).eq(false);
346366
});
347367
});
348-
});
368+
});

0 commit comments

Comments
 (0)