Skip to content

Commit 1480b78

Browse files
committed
refactor(mcp-server.test): Simplify test setup by using beforeEach for mock instances
1 parent cb28ccc commit 1480b78

File tree

1 file changed

+30
-74
lines changed

1 file changed

+30
-74
lines changed

packages/core/test/lib/mcp-server.test.ts

Lines changed: 30 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -46,73 +46,53 @@ describe('wrapMcpServerWithSentry', () => {
4646
});
4747

4848
describe('Transport-level instrumentation', () => {
49+
let mockMcpServer: ReturnType<typeof createMockMcpServer>;
50+
let wrappedMcpServer: ReturnType<typeof createMockMcpServer>;
51+
let mockTransport: ReturnType<typeof createMockTransport>;
52+
53+
beforeEach(async () => {
54+
mockMcpServer = createMockMcpServer();
55+
wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
56+
mockTransport = createMockTransport();
57+
58+
// Connect the server to transport - this is common to most tests
59+
await wrappedMcpServer.connect(mockTransport);
60+
});
61+
4962
it('should proxy the connect method', () => {
50-
const mockMcpServer = createMockMcpServer();
51-
const originalConnect = mockMcpServer.connect;
63+
// We need to test this before connection, so create fresh instances
64+
const freshMockMcpServer = createMockMcpServer();
65+
const originalConnect = freshMockMcpServer.connect;
5266

53-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
67+
const freshWrappedMcpServer = wrapMcpServerWithSentry(freshMockMcpServer);
5468

55-
expect(wrappedMcpServer.connect).not.toBe(originalConnect);
69+
expect(freshWrappedMcpServer.connect).not.toBe(originalConnect);
5670
});
5771

58-
it('should intercept transport onmessage handler', async () => {
59-
const mockMcpServer = createMockMcpServer();
60-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
61-
62-
const mockTransport = createMockTransport();
72+
it('should intercept transport onmessage handler', () => {
6373
const originalOnMessage = mockTransport.onmessage;
64-
65-
await wrappedMcpServer.connect(mockTransport);
66-
67-
// onmessage should be wrapped
74+
// onmessage should be wrapped after connection
6875
expect(mockTransport.onmessage).not.toBe(originalOnMessage);
6976
});
7077

71-
it('should intercept transport send handler', async () => {
72-
const mockMcpServer = createMockMcpServer();
73-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
74-
75-
const mockTransport = createMockTransport();
78+
it('should intercept transport send handler', () => {
7679
const originalSend = mockTransport.send;
77-
78-
await wrappedMcpServer.connect(mockTransport);
79-
80-
// send should be wrapped
80+
// send should be wrapped after connection
8181
expect(mockTransport.send).not.toBe(originalSend);
8282
});
8383

84-
it('should intercept transport onclose handler', async () => {
85-
const mockMcpServer = createMockMcpServer();
86-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
87-
88-
const mockTransport = createMockTransport();
84+
it('should intercept transport onclose handler', () => {
8985
const originalOnClose = mockTransport.onclose;
90-
91-
await wrappedMcpServer.connect(mockTransport);
92-
93-
// onclose should be wrapped
86+
// onclose should be wrapped after connection
9487
expect(mockTransport.onclose).not.toBe(originalOnClose);
9588
});
9689

97-
it('should call original connect and preserve functionality', async () => {
98-
const mockMcpServer = createMockMcpServer();
99-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
100-
101-
const mockTransport = createMockTransport();
102-
103-
await wrappedMcpServer.connect(mockTransport);
104-
105-
// Original connect should have been called
90+
it('should call original connect and preserve functionality', () => {
91+
// Original connect should have been called during beforeEach
10692
expect(mockMcpServer.connect).toHaveBeenCalledWith(mockTransport);
10793
});
10894

109-
it('should create spans for incoming JSON-RPC requests', async () => {
110-
const mockMcpServer = createMockMcpServer();
111-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
112-
113-
const mockTransport = createMockTransport();
114-
await wrappedMcpServer.connect(mockTransport);
115-
95+
it('should create spans for incoming JSON-RPC requests', () => {
11696
const jsonRpcRequest = {
11797
jsonrpc: '2.0',
11898
method: 'tools/call',
@@ -133,13 +113,7 @@ describe('wrapMcpServerWithSentry', () => {
133113
);
134114
});
135115

136-
it('should create spans for incoming JSON-RPC notifications', async () => {
137-
const mockMcpServer = createMockMcpServer();
138-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
139-
140-
const mockTransport = createMockTransport();
141-
await wrappedMcpServer.connect(mockTransport);
142-
116+
it('should create spans for incoming JSON-RPC notifications', () => {
143117
const jsonRpcNotification = {
144118
jsonrpc: '2.0',
145119
method: 'notifications/initialized',
@@ -160,12 +134,6 @@ describe('wrapMcpServerWithSentry', () => {
160134
});
161135

162136
it('should create spans for outgoing notifications', async () => {
163-
const mockMcpServer = createMockMcpServer();
164-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
165-
166-
const mockTransport = createMockTransport();
167-
await wrappedMcpServer.connect(mockTransport);
168-
169137
const outgoingNotification = {
170138
jsonrpc: '2.0',
171139
method: 'notifications/tools/list_changed',
@@ -185,29 +153,17 @@ describe('wrapMcpServerWithSentry', () => {
185153
);
186154
});
187155

188-
it('should not create spans for non-JSON-RPC messages', async () => {
189-
const mockMcpServer = createMockMcpServer();
190-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
191-
192-
const mockTransport = createMockTransport();
193-
await wrappedMcpServer.connect(mockTransport);
194-
156+
it('should not create spans for non-JSON-RPC messages', () => {
195157
// Simulate non-JSON-RPC message
196158
expect(mockTransport.onmessage).toBeDefined();
197159
mockTransport.onmessage?.({ some: 'data' }, {});
198160

199161
expect(tracingModule.startSpan).not.toHaveBeenCalled();
200162
});
201163

202-
it('should handle transport onclose events', async () => {
203-
const mockMcpServer = createMockMcpServer();
204-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
205-
206-
const mockTransport = createMockTransport();
164+
it('should handle transport onclose events', () => {
207165
mockTransport.sessionId = 'test-session-123';
208166

209-
await wrappedMcpServer.connect(mockTransport);
210-
211167
// Trigger onclose - should not throw
212168
expect(mockTransport.onclose).toBeDefined();
213169
expect(() => mockTransport.onclose?.()).not.toThrow();

0 commit comments

Comments
 (0)