Skip to content

Commit 0ff7ae0

Browse files
committed
simplify tests
1 parent d4bfe55 commit 0ff7ae0

File tree

2 files changed

+25
-78
lines changed

2 files changed

+25
-78
lines changed

src/client/cross-spawn.test.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDefaultEnvironment, StdioClientTransport } from "./stdio.js";
1+
import { StdioClientTransport, getDefaultEnvironment } from "./stdio.js";
22
import spawn from "cross-spawn";
33
import { JSONRPCMessage } from "../types.js";
44
import { ChildProcess } from "node:child_process";
@@ -67,19 +67,37 @@ describe("StdioClientTransport using cross-spawn", () => {
6767

6868
await transport.start();
6969

70-
// verify environment variables are passed correctly
70+
// verify environment variables are merged correctly
7171
expect(mockSpawn).toHaveBeenCalledWith(
7272
"test-command",
7373
[],
7474
expect.objectContaining({
7575
env: {
7676
...getDefaultEnvironment(),
77-
...customEnv,
77+
...customEnv
7878
}
7979
})
8080
);
8181
});
8282

83+
test("should use default environment when env is undefined", async () => {
84+
const transport = new StdioClientTransport({
85+
command: "test-command",
86+
env: undefined
87+
});
88+
89+
await transport.start();
90+
91+
// verify default environment is used
92+
expect(mockSpawn).toHaveBeenCalledWith(
93+
"test-command",
94+
[],
95+
expect.objectContaining({
96+
env: getDefaultEnvironment()
97+
})
98+
);
99+
});
100+
83101
test("should send messages correctly", async () => {
84102
const transport = new StdioClientTransport({
85103
command: "test-command"

src/client/stdio.test.ts

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
import { JSONRPCMessage } from "../types.js";
2-
import { StdioClientTransport, StdioServerParameters, DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment } from "./stdio.js";
3-
import { AsyncLocalStorage } from "node:async_hooks";
2+
import { StdioClientTransport, StdioServerParameters } from "./stdio.js";
43

54
const serverParameters: StdioServerParameters = {
65
command: "/usr/bin/tee",
76
};
87

9-
const envAsyncLocalStorage = new AsyncLocalStorage<{ env: Record<string, string> }>();
10-
11-
jest.mock('cross-spawn', () => {
12-
const originalSpawn = jest.requireActual('cross-spawn');
13-
return jest.fn((command, args, options) => {
14-
const env = envAsyncLocalStorage.getStore();
15-
if (env) {
16-
env.env = options.env;
17-
}
18-
return originalSpawn(command, args, options);
19-
});
20-
});
21-
228
test("should start then close cleanly", async () => {
239
const client = new StdioClientTransport(serverParameters);
2410
client.onerror = (error) => {
@@ -74,68 +60,11 @@ test("should read messages", async () => {
7460
await client.close();
7561
});
7662

77-
78-
test("should properly set default environment variables in spawned process", async () => {
79-
await envAsyncLocalStorage.run({ env: {} }, async () => {
63+
test("should return child process pid", async () => {
8064
const client = new StdioClientTransport(serverParameters);
8165

8266
await client.start();
67+
expect(client.pid).not.toBeNull();
8368
await client.close();
84-
85-
// Get the default environment variables
86-
const defaultEnv = getDefaultEnvironment();
87-
const spawnEnv = envAsyncLocalStorage.getStore()?.env;
88-
expect(spawnEnv).toBeDefined();
89-
// Verify that all default environment variables are present
90-
for (const key of DEFAULT_INHERITED_ENV_VARS) {
91-
if (process.env[key] && !process.env[key].startsWith("()")) {
92-
expect(spawnEnv).toHaveProperty(key);
93-
expect(spawnEnv![key]).toBe(process.env[key]);
94-
expect(spawnEnv![key]).toBe(defaultEnv[key]);
95-
}
96-
}
97-
});
98-
});
99-
100-
test("should override default environment variables with custom ones", async () => {
101-
await envAsyncLocalStorage.run({ env: {} }, async () => {
102-
const customEnv = {
103-
HOME: "/custom/home",
104-
PATH: "/custom/path",
105-
USER: "custom_user"
106-
};
107-
108-
const client = new StdioClientTransport({
109-
...serverParameters,
110-
env: customEnv
111-
});
112-
113-
await client.start();
114-
await client.close();
115-
116-
const spawnEnv = envAsyncLocalStorage.getStore()?.env;
117-
expect(spawnEnv).toBeDefined();
118-
// Verify that custom environment variables override default ones
119-
for (const [key, value] of Object.entries(customEnv)) {
120-
expect(spawnEnv).toHaveProperty(key);
121-
expect(spawnEnv![key]).toBe(value);
122-
}
123-
124-
// Verify that other default environment variables are still present
125-
for (const key of DEFAULT_INHERITED_ENV_VARS) {
126-
if (!(key in customEnv) && process.env[key] && !process.env[key].startsWith("()")) {
127-
expect(spawnEnv).toHaveProperty(key);
128-
expect(spawnEnv![key]).toBe(process.env[key]);
129-
}
130-
}
131-
});
132-
133-
test("should return child process pid", async () => {
134-
const client = new StdioClientTransport(serverParameters);
135-
136-
await client.start();
137-
expect(client.pid).not.toBeNull();
138-
await client.close();
139-
expect(client.pid).toBeNull();
140-
});
69+
expect(client.pid).toBeNull();
14170
});

0 commit comments

Comments
 (0)