Skip to content

Commit c4dee4c

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 e2fce6c commit c4dee4c

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ describe("McpHub", () => {
21122112
}
21132113
})
21142114

2115-
// Mock the config file read BEFORE creating McpHub
2115+
// Mock the config file read
21162116
vi.mocked(fs.readFile).mockResolvedValue(
21172117
JSON.stringify({
21182118
mcpServers: {
@@ -2124,9 +2124,11 @@ describe("McpHub", () => {
21242124
}),
21252125
)
21262126

2127-
// Create a new McpHub instance and wait for initialization
2127+
// Create a new McpHub instance
21282128
const mcpHub = new McpHub(mockProvider as ClineProvider)
2129-
await mcpHub.waitUntilReady()
2129+
2130+
// Wait for initialization
2131+
await new Promise((resolve) => setTimeout(resolve, 100))
21302132

21312133
// Verify StdioClientTransport was called with wrapped command
21322134
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2174,7 +2176,7 @@ describe("McpHub", () => {
21742176
}
21752177
})
21762178

2177-
// Mock the config file read BEFORE creating McpHub
2179+
// Mock the config file read
21782180
vi.mocked(fs.readFile).mockResolvedValue(
21792181
JSON.stringify({
21802182
mcpServers: {
@@ -2186,9 +2188,11 @@ describe("McpHub", () => {
21862188
}),
21872189
)
21882190

2189-
// Create a new McpHub instance and wait for initialization
2191+
// Create a new McpHub instance
21902192
const mcpHub = new McpHub(mockProvider as ClineProvider)
2191-
await mcpHub.waitUntilReady()
2193+
2194+
// Wait for initialization
2195+
await new Promise((resolve) => setTimeout(resolve, 100))
21922196

21932197
// Verify StdioClientTransport was called without wrapping
21942198
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2236,7 +2240,7 @@ describe("McpHub", () => {
22362240
}
22372241
})
22382242

2239-
// Mock the config file read BEFORE creating McpHub
2243+
// Mock the config file read with cmd.exe already as command
22402244
vi.mocked(fs.readFile).mockResolvedValue(
22412245
JSON.stringify({
22422246
mcpServers: {
@@ -2248,9 +2252,11 @@ describe("McpHub", () => {
22482252
}),
22492253
)
22502254

2251-
// Create a new McpHub instance and wait for initialization
2255+
// Create a new McpHub instance
22522256
const mcpHub = new McpHub(mockProvider as ClineProvider)
2253-
await mcpHub.waitUntilReady()
2257+
2258+
// Wait for initialization
2259+
await new Promise((resolve) => setTimeout(resolve, 100))
22542260

22552261
// Verify StdioClientTransport was called without double-wrapping
22562262
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2305,7 +2311,7 @@ describe("McpHub", () => {
23052311
}
23062312
})
23072313

2308-
// Mock the config file read BEFORE creating McpHub - simulating fnm/nvm-windows scenario
2314+
// Mock the config file read - simulating fnm/nvm-windows scenario
23092315
vi.mocked(fs.readFile).mockResolvedValue(
23102316
JSON.stringify({
23112317
mcpServers: {
@@ -2323,9 +2329,11 @@ describe("McpHub", () => {
23232329
}),
23242330
)
23252331

2326-
// Create a new McpHub instance and wait for initialization
2332+
// Create a new McpHub instance
23272333
const mcpHub = new McpHub(mockProvider as ClineProvider)
2328-
await mcpHub.waitUntilReady()
2334+
2335+
// Wait for initialization
2336+
await new Promise((resolve) => setTimeout(resolve, 100))
23292337

23302338
// Verify that the command was wrapped with cmd.exe
23312339
expect(StdioClientTransport).toHaveBeenCalledWith(
@@ -2378,7 +2386,7 @@ describe("McpHub", () => {
23782386
}
23792387
})
23802388

2381-
// Mock the config file read BEFORE creating McpHub
2389+
// Mock the config file read with CMD (uppercase) as command
23822390
vi.mocked(fs.readFile).mockResolvedValue(
23832391
JSON.stringify({
23842392
mcpServers: {
@@ -2390,9 +2398,11 @@ describe("McpHub", () => {
23902398
}),
23912399
)
23922400

2393-
// Create a new McpHub instance and wait for initialization
2401+
// Create a new McpHub instance
23942402
const mcpHub = new McpHub(mockProvider as ClineProvider)
2395-
await mcpHub.waitUntilReady()
2403+
2404+
// Wait for initialization
2405+
await new Promise((resolve) => setTimeout(resolve, 100))
23962406

23972407
// Verify StdioClientTransport was called without double-wrapping
23982408
expect(StdioClientTransport).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)