Skip to content

Commit 9cca201

Browse files
committed
fix: modify test code about stdio.test
Signed-off-by: sunrabbit123 <[email protected]>
1 parent cd359ed commit 9cca201

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

src/client/cross-spawn.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StdioClientTransport } from "./stdio.js";
1+
import { getDefaultEnvironment, StdioClientTransport } from "./stdio.js";
22
import spawn from "cross-spawn";
33
import { JSONRPCMessage } from "../types.js";
44
import { ChildProcess } from "node:child_process";
@@ -72,7 +72,10 @@ describe("StdioClientTransport using cross-spawn", () => {
7272
"test-command",
7373
[],
7474
expect.objectContaining({
75-
env: customEnv
75+
env: {
76+
...customEnv,
77+
...getDefaultEnvironment()
78+
}
7679
})
7780
);
7881
});

src/client/stdio.test.ts

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { JSONRPCMessage } from "../types.js";
2-
import { StdioClientTransport, StdioServerParameters } from "./stdio.js";
2+
import { StdioClientTransport, StdioServerParameters, DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment } from "./stdio.js";
33

44
const serverParameters: StdioServerParameters = {
55
command: "/usr/bin/tee",
66
};
77

8+
9+
let spawnEnv: Record<string, string> | undefined;
10+
11+
jest.mock('cross-spawn', () => {
12+
const originalSpawn = jest.requireActual('cross-spawn');
13+
return jest.fn((command, args, options) => {
14+
spawnEnv = options.env;
15+
return originalSpawn(command, args, options);
16+
});
17+
});
18+
819
test("should start then close cleanly", async () => {
920
const client = new StdioClientTransport(serverParameters);
1021
client.onerror = (error) => {
@@ -60,32 +71,51 @@ test("should read messages", async () => {
6071
await client.close();
6172
});
6273

63-
test("should work with actual node mcp server", async () => {
64-
const client = new StdioClientTransport({
65-
command: "npx",
66-
args: ["-y", "@wrtnlabs/calculator-mcp"],
67-
});
68-
69-
await client.start();
70-
await client.close();
71-
});
74+
test("should properly set default environment variables in spawned process", async () => {
75+
const client = new StdioClientTransport(serverParameters);
7276

73-
test("should work with actual node mcp server and empty env", async () => {
74-
const client = new StdioClientTransport({
75-
command: "npx",
76-
args: ["-y", "@wrtnlabs/calculator-mcp"],
77-
env: {},
78-
});
7977
await client.start();
8078
await client.close();
79+
80+
// Get the default environment variables
81+
const defaultEnv = getDefaultEnvironment();
82+
83+
// Verify that all default environment variables are present
84+
for (const key of DEFAULT_INHERITED_ENV_VARS) {
85+
if (process.env[key] && !process.env[key].startsWith("()")) {
86+
expect(spawnEnv).toHaveProperty(key);
87+
expect(spawnEnv![key]).toBe(process.env[key]);
88+
expect(spawnEnv![key]).toBe(defaultEnv[key]);
89+
}
90+
}
8191
});
8292

83-
test("should work with actual node mcp server and custom env", async () => {
93+
test("should override default environment variables with custom ones", async () => {
94+
const customEnv = {
95+
HOME: "/custom/home",
96+
PATH: "/custom/path",
97+
USER: "custom_user"
98+
};
99+
84100
const client = new StdioClientTransport({
85-
command: "npx",
86-
args: ["-y", "@wrtnlabs/calculator-mcp"],
87-
env: {TEST_VAR: "test-value"},
101+
...serverParameters,
102+
env: customEnv
88103
});
104+
89105
await client.start();
90106
await client.close();
107+
108+
// Verify that custom environment variables override default ones
109+
for (const [key, value] of Object.entries(customEnv)) {
110+
expect(spawnEnv).toHaveProperty(key);
111+
expect(spawnEnv![key]).toBe(value);
112+
}
113+
114+
// Verify that other default environment variables are still present
115+
for (const key of DEFAULT_INHERITED_ENV_VARS) {
116+
if (!(key in customEnv) && process.env[key] && !process.env[key].startsWith("()")) {
117+
expect(spawnEnv).toHaveProperty(key);
118+
expect(spawnEnv![key]).toBe(process.env[key]);
119+
}
120+
}
91121
});

0 commit comments

Comments
 (0)