Skip to content

Commit f43bfcc

Browse files
committed
test: fix test code for save test case principal
Signed-off-by: sunrabbit123 <[email protected]>
1 parent bd61d21 commit f43bfcc

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/client/stdio.test.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { JSONRPCMessage } from "../types.js";
22
import { StdioClientTransport, StdioServerParameters, DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment } from "./stdio.js";
3+
import { AsyncLocalStorage } from "node:async_hooks";
34

45
const serverParameters: StdioServerParameters = {
56
command: "/usr/bin/tee",
67
};
78

8-
9-
let spawnEnv: Record<string, string> | undefined;
9+
const envAsyncLocalStorage = new AsyncLocalStorage<{ env: Record<string, string> }>();
1010

1111
jest.mock('cross-spawn', () => {
1212
const originalSpawn = jest.requireActual('cross-spawn');
1313
return jest.fn((command, args, options) => {
14-
spawnEnv = options.env;
14+
const env = envAsyncLocalStorage.getStore();
15+
if (env) {
16+
env.env = options.env;
17+
}
1518
return originalSpawn(command, args, options);
1619
});
1720
});
@@ -72,25 +75,29 @@ test("should read messages", async () => {
7275
});
7376

7477
test("should properly set default environment variables in spawned process", async () => {
78+
await envAsyncLocalStorage.run({ env: {} }, async () => {
7579
const client = new StdioClientTransport(serverParameters);
7680

7781
await client.start();
7882
await client.close();
7983

8084
// Get the default environment variables
8185
const defaultEnv = getDefaultEnvironment();
82-
86+
const spawnEnv = envAsyncLocalStorage.getStore()?.env;
87+
expect(spawnEnv).toBeDefined();
8388
// Verify that all default environment variables are present
8489
for (const key of DEFAULT_INHERITED_ENV_VARS) {
8590
if (process.env[key] && !process.env[key].startsWith("()")) {
8691
expect(spawnEnv).toHaveProperty(key);
8792
expect(spawnEnv![key]).toBe(process.env[key]);
88-
expect(spawnEnv![key]).toBe(defaultEnv[key]);
93+
expect(spawnEnv![key]).toBe(defaultEnv[key]);
94+
}
8995
}
90-
}
96+
});
9197
});
9298

9399
test("should override default environment variables with custom ones", async () => {
100+
await envAsyncLocalStorage.run({ env: {} }, async () => {
94101
const customEnv = {
95102
HOME: "/custom/home",
96103
PATH: "/custom/path",
@@ -104,7 +111,9 @@ test("should override default environment variables with custom ones", async ()
104111

105112
await client.start();
106113
await client.close();
107-
114+
115+
const spawnEnv = envAsyncLocalStorage.getStore()?.env;
116+
expect(spawnEnv).toBeDefined();
108117
// Verify that custom environment variables override default ones
109118
for (const [key, value] of Object.entries(customEnv)) {
110119
expect(spawnEnv).toHaveProperty(key);
@@ -117,5 +126,6 @@ test("should override default environment variables with custom ones", async ()
117126
expect(spawnEnv).toHaveProperty(key);
118127
expect(spawnEnv![key]).toBe(process.env[key]);
119128
}
120-
}
121-
});
129+
}
130+
});
131+
});

0 commit comments

Comments
 (0)