Skip to content

Commit 8be3bde

Browse files
committed
fix(client): fix ping tests timing out due to MockTransport not responding
The MockTransport class in ping.test.ts was not responding to initialize and ping requests, causing client.connect() to hang indefinitely and tests to timeout. Updated MockTransport.send() to properly respond to these requests with appropriate JSON-RPC responses.
1 parent ad869cf commit 8be3bde

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

packages/client/test/client/ping.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { JSONRPCMessage } from '@modelcontextprotocol/core';
33
import { Client } from '../../src/client/client.js';
44
import type { Transport, TransportSendOptions } from '@modelcontextprotocol/core';
55

6-
// Mock Transport class
6+
// Mock Transport class that responds to initialize requests
77
class MockTransport implements Transport {
88
onclose?: () => void;
99
onerror?: (error: Error) => void;
@@ -14,7 +14,33 @@ class MockTransport implements Transport {
1414
async close(): Promise<void> {
1515
this.onclose?.();
1616
}
17-
async send(_message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void> {}
17+
async send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void> {
18+
// Respond to initialize requests so connect() doesn't hang
19+
if ('method' in message && message.method === 'initialize' && 'id' in message) {
20+
// Use queueMicrotask to simulate async response
21+
queueMicrotask(() => {
22+
this.onmessage?.({
23+
jsonrpc: '2.0',
24+
id: message.id as string | number,
25+
result: {
26+
protocolVersion: '2024-11-05',
27+
capabilities: {},
28+
serverInfo: { name: 'test-server', version: '1.0.0' }
29+
}
30+
});
31+
});
32+
}
33+
// Respond to ping requests
34+
if ('method' in message && message.method === 'ping' && 'id' in message) {
35+
queueMicrotask(() => {
36+
this.onmessage?.({
37+
jsonrpc: '2.0',
38+
id: message.id as string | number,
39+
result: {}
40+
});
41+
});
42+
}
43+
}
1844
}
1945

2046
// Helper interface to access private members for testing

0 commit comments

Comments
 (0)