Skip to content

Commit

Permalink
Make testsuite compatible with node 22.x (#96)
Browse files Browse the repository at this point in the history
* work around with global navigator in node 22

* fix lint
  • Loading branch information
zhiyuanliang-ms authored Sep 14, 2024
1 parent 335b44b commit ce1edd3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v3
Expand Down
34 changes: 27 additions & 7 deletions test/requestTracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,23 @@ describe("request tracing", function () {

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

it("should identify WebWorker environment", async () => {
(global as any).WorkerNavigator = function WorkerNavigator() { };
(global as any).navigator = new (global as any).WorkerNavigator();
Object.defineProperty(global, "navigator", {
value: new (global as any).WorkerNavigator(),
configurable: true
});
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
(global as any).importScripts = function importScripts() { };

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

it("is not WebWorker when WorkerNavigator is undefined", async () => {
(global as any).navigator = { userAgent: "node.js" } as any; // Mock navigator
Object.defineProperty(global, "navigator", {
value: { userAgent: "node.js" } as any, // Mock navigator
configurable: true
});
(global as any).WorkerNavigator = undefined;
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
(global as any).importScripts = function importScripts() { };
Expand All @@ -203,7 +214,10 @@ describe("request tracing", function () {
});

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

it("is not WebWorker when WorkerGlobalScope is undefined", async () => {
(global as any).WorkerNavigator = function WorkerNavigator() { };
(global as any).navigator = new (global as any).WorkerNavigator();
Object.defineProperty(global, "navigator", {
value: new (global as any).WorkerNavigator(),
configurable: true
});
(global as any).WorkerGlobalScope = undefined;
(global as any).importScripts = function importScripts() { };

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

it("is not WebWorker when importScripts is undefined", async () => {
(global as any).WorkerNavigator = function WorkerNavigator() { };
(global as any).navigator = new (global as any).WorkerNavigator();
Object.defineProperty(global, "navigator", {
value: new (global as any).WorkerNavigator(),
configurable: true
});
(global as any).WorkerGlobalScope = function WorkerGlobalScope() { };
(global as any).importScripts = undefined;

Expand Down Expand Up @@ -345,4 +365,4 @@ describe("request tracing", function () {
expect(correlationContext.includes("Host=Web")).eq(false);
});
});
});
});

0 comments on commit ce1edd3

Please sign in to comment.