Skip to content

Commit 6e987c3

Browse files
committed
🐛 test(mcp): fix McpHub Windows command wrapping test ordering
Fix mock initialization race: fs.readFile mock must be set before new McpHub() triggers async initializeGlobalMcpServers(). Also fix duplicate vi.mock('fs/promises') without factory causing auto-mock on line 96. Closes #349
1 parent e0dd61a commit 6e987c3

1 file changed

Lines changed: 26 additions & 27 deletions

File tree

src/services/mcp/__tests__/McpHub.spec.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from "fs/promises"
1+
import * as fs from "fs/promises"
22

33
import type { Mock } from "vitest"
44
import type { ExtensionContext, Uri } from "vscode"
@@ -93,7 +93,6 @@ vi.mock("vscode", () => ({
9393
from: vi.fn(),
9494
},
9595
}))
96-
vi.mock("fs/promises")
9796
vi.mock("../../../core/webview/ClineProvider")
9897

9998
// Mock the MCP SDK modules
@@ -2125,9 +2124,6 @@ describe("McpHub", () => {
21252124
}
21262125
})
21272126

2128-
// Create a new McpHub instance
2129-
const mcpHub = new McpHub(mockProvider as ClineProvider)
2130-
21312127
// Mock the config file read
21322128
vi.mocked(fs.readFile).mockResolvedValue(
21332129
JSON.stringify({
@@ -2140,8 +2136,11 @@ describe("McpHub", () => {
21402136
}),
21412137
)
21422138

2143-
// Initialize servers (this will trigger connectToServer)
2144-
await mcpHub["initializeGlobalMcpServers"]()
2139+
// Create a new McpHub instance
2140+
const mcpHub = new McpHub(mockProvider as ClineProvider)
2141+
2142+
// Wait for initialization
2143+
await new Promise((resolve) => setTimeout(resolve, 100))
21452144

21462145
// Verify StdioClientTransport was called with wrapped command
21472146
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2189,9 +2188,6 @@ describe("McpHub", () => {
21892188
}
21902189
})
21912190

2192-
// Create a new McpHub instance
2193-
const mcpHub = new McpHub(mockProvider as ClineProvider)
2194-
21952191
// Mock the config file read
21962192
vi.mocked(fs.readFile).mockResolvedValue(
21972193
JSON.stringify({
@@ -2204,8 +2200,11 @@ describe("McpHub", () => {
22042200
}),
22052201
)
22062202

2207-
// Initialize servers (this will trigger connectToServer)
2208-
await mcpHub["initializeGlobalMcpServers"]()
2203+
// Create a new McpHub instance
2204+
const mcpHub = new McpHub(mockProvider as ClineProvider)
2205+
2206+
// Wait for initialization
2207+
await new Promise((resolve) => setTimeout(resolve, 100))
22092208

22102209
// Verify StdioClientTransport was called without wrapping
22112210
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2253,9 +2252,6 @@ describe("McpHub", () => {
22532252
}
22542253
})
22552254

2256-
// Create a new McpHub instance
2257-
const mcpHub = new McpHub(mockProvider as ClineProvider)
2258-
22592255
// Mock the config file read with cmd.exe already as command
22602256
vi.mocked(fs.readFile).mockResolvedValue(
22612257
JSON.stringify({
@@ -2268,8 +2264,11 @@ describe("McpHub", () => {
22682264
}),
22692265
)
22702266

2271-
// Initialize servers (this will trigger connectToServer)
2272-
await mcpHub["initializeGlobalMcpServers"]()
2267+
// Create a new McpHub instance
2268+
const mcpHub = new McpHub(mockProvider as ClineProvider)
2269+
2270+
// Wait for initialization
2271+
await new Promise((resolve) => setTimeout(resolve, 100))
22732272

22742273
// Verify StdioClientTransport was called without double-wrapping
22752274
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2324,9 +2323,6 @@ describe("McpHub", () => {
23242323
}
23252324
})
23262325

2327-
// Create a new McpHub instance
2328-
const mcpHub = new McpHub(mockProvider as ClineProvider)
2329-
23302326
// Mock the config file read - simulating fnm/nvm-windows scenario
23312327
vi.mocked(fs.readFile).mockResolvedValue(
23322328
JSON.stringify({
@@ -2345,8 +2341,11 @@ describe("McpHub", () => {
23452341
}),
23462342
)
23472343

2348-
// Initialize servers (this will trigger connectToServer)
2349-
await mcpHub["initializeGlobalMcpServers"]()
2344+
// Create a new McpHub instance
2345+
const mcpHub = new McpHub(mockProvider as ClineProvider)
2346+
2347+
// Wait for initialization
2348+
await new Promise((resolve) => setTimeout(resolve, 100))
23502349

23512350
// Verify that the command was wrapped with cmd.exe
23522351
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2399,9 +2398,6 @@ describe("McpHub", () => {
23992398
}
24002399
})
24012400

2402-
// Create a new McpHub instance
2403-
const mcpHub = new McpHub(mockProvider as ClineProvider)
2404-
24052401
// Mock the config file read with CMD (uppercase) as command
24062402
vi.mocked(fs.readFile).mockResolvedValue(
24072403
JSON.stringify({
@@ -2414,8 +2410,11 @@ describe("McpHub", () => {
24142410
}),
24152411
)
24162412

2417-
// Initialize servers (this will trigger connectToServer)
2418-
await mcpHub["initializeGlobalMcpServers"]()
2413+
// Create a new McpHub instance
2414+
const mcpHub = new McpHub(mockProvider as ClineProvider)
2415+
2416+
// Wait for initialization
2417+
await new Promise((resolve) => setTimeout(resolve, 100))
24192418

24202419
// Verify StdioClientTransport was called without double-wrapping
24212420
expect(StdioClientTransport).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)