@@ -50,13 +50,11 @@ describe('wrapMcpServerWithSentry', () => {
50
50
let wrappedMcpServer : ReturnType < typeof createMockMcpServer > ;
51
51
let mockTransport : ReturnType < typeof createMockTransport > ;
52
52
53
- beforeEach ( async ( ) => {
53
+ beforeEach ( ( ) => {
54
54
mockMcpServer = createMockMcpServer ( ) ;
55
55
wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
56
56
mockTransport = createMockTransport ( ) ;
57
-
58
- // Connect the server to transport - this is common to most tests
59
- await wrappedMcpServer . connect ( mockTransport ) ;
57
+ // Don't connect transport here. let individual tests control when connection happens
60
58
} ) ;
61
59
62
60
it ( 'should proxy the connect method' , ( ) => {
@@ -69,30 +67,43 @@ describe('wrapMcpServerWithSentry', () => {
69
67
expect ( freshWrappedMcpServer . connect ) . not . toBe ( originalConnect ) ;
70
68
} ) ;
71
69
72
- it ( 'should intercept transport onmessage handler' , ( ) => {
70
+ it ( 'should intercept transport onmessage handler' , async ( ) => {
73
71
const originalOnMessage = mockTransport . onmessage ;
72
+
73
+ await wrappedMcpServer . connect ( mockTransport ) ;
74
+
74
75
// onmessage should be wrapped after connection
75
76
expect ( mockTransport . onmessage ) . not . toBe ( originalOnMessage ) ;
76
77
} ) ;
77
78
78
- it ( 'should intercept transport send handler' , ( ) => {
79
+ it ( 'should intercept transport send handler' , async ( ) => {
79
80
const originalSend = mockTransport . send ;
81
+
82
+ await wrappedMcpServer . connect ( mockTransport ) ;
83
+
80
84
// send should be wrapped after connection
81
85
expect ( mockTransport . send ) . not . toBe ( originalSend ) ;
82
86
} ) ;
83
87
84
- it ( 'should intercept transport onclose handler' , ( ) => {
88
+ it ( 'should intercept transport onclose handler' , async ( ) => {
85
89
const originalOnClose = mockTransport . onclose ;
90
+
91
+ await wrappedMcpServer . connect ( mockTransport ) ;
92
+
86
93
// onclose should be wrapped after connection
87
94
expect ( mockTransport . onclose ) . not . toBe ( originalOnClose ) ;
88
95
} ) ;
89
96
90
- it ( 'should call original connect and preserve functionality' , ( ) => {
91
- // Original connect should have been called during beforeEach
97
+ it ( 'should call original connect and preserve functionality' , async ( ) => {
98
+ await wrappedMcpServer . connect ( mockTransport ) ;
99
+
100
+ // Original connect should have been called
92
101
expect ( mockMcpServer . connect ) . toHaveBeenCalledWith ( mockTransport ) ;
93
102
} ) ;
94
103
95
- it ( 'should create spans for incoming JSON-RPC requests' , ( ) => {
104
+ it ( 'should create spans for incoming JSON-RPC requests' , async ( ) => {
105
+ await wrappedMcpServer . connect ( mockTransport ) ;
106
+
96
107
const jsonRpcRequest = {
97
108
jsonrpc : '2.0' ,
98
109
method : 'tools/call' ,
@@ -101,7 +112,6 @@ describe('wrapMcpServerWithSentry', () => {
101
112
} ;
102
113
103
114
// Simulate incoming message
104
- expect ( mockTransport . onmessage ) . toBeDefined ( ) ;
105
115
mockTransport . onmessage ?.( jsonRpcRequest , { } ) ;
106
116
107
117
expect ( tracingModule . startSpan ) . toHaveBeenCalledWith (
@@ -113,15 +123,16 @@ describe('wrapMcpServerWithSentry', () => {
113
123
) ;
114
124
} ) ;
115
125
116
- it ( 'should create spans for incoming JSON-RPC notifications' , ( ) => {
126
+ it ( 'should create spans for incoming JSON-RPC notifications' , async ( ) => {
127
+ await wrappedMcpServer . connect ( mockTransport ) ;
128
+
117
129
const jsonRpcNotification = {
118
130
jsonrpc : '2.0' ,
119
131
method : 'notifications/initialized' ,
120
132
// No 'id' field - this makes it a notification
121
133
} ;
122
134
123
135
// Simulate incoming notification
124
- expect ( mockTransport . onmessage ) . toBeDefined ( ) ;
125
136
mockTransport . onmessage ?.( jsonRpcNotification , { } ) ;
126
137
127
138
expect ( tracingModule . startSpan ) . toHaveBeenCalledWith (
@@ -134,14 +145,15 @@ describe('wrapMcpServerWithSentry', () => {
134
145
} ) ;
135
146
136
147
it ( 'should create spans for outgoing notifications' , async ( ) => {
148
+ await wrappedMcpServer . connect ( mockTransport ) ;
149
+
137
150
const outgoingNotification = {
138
151
jsonrpc : '2.0' ,
139
152
method : 'notifications/tools/list_changed' ,
140
153
// No 'id' field
141
154
} ;
142
155
143
156
// Simulate outgoing notification
144
- expect ( mockTransport . send ) . toBeDefined ( ) ;
145
157
await mockTransport . send ?.( outgoingNotification ) ;
146
158
147
159
expect ( tracingModule . startSpan ) . toHaveBeenCalledWith (
@@ -153,19 +165,20 @@ describe('wrapMcpServerWithSentry', () => {
153
165
) ;
154
166
} ) ;
155
167
156
- it ( 'should not create spans for non-JSON-RPC messages' , ( ) => {
168
+ it ( 'should not create spans for non-JSON-RPC messages' , async ( ) => {
169
+ await wrappedMcpServer . connect ( mockTransport ) ;
170
+
157
171
// Simulate non-JSON-RPC message
158
- expect ( mockTransport . onmessage ) . toBeDefined ( ) ;
159
172
mockTransport . onmessage ?.( { some : 'data' } , { } ) ;
160
173
161
174
expect ( tracingModule . startSpan ) . not . toHaveBeenCalled ( ) ;
162
175
} ) ;
163
176
164
- it ( 'should handle transport onclose events' , ( ) => {
177
+ it ( 'should handle transport onclose events' , async ( ) => {
178
+ await wrappedMcpServer . connect ( mockTransport ) ;
165
179
mockTransport . sessionId = 'test-session-123' ;
166
180
167
181
// Trigger onclose - should not throw
168
- expect ( mockTransport . onclose ) . toBeDefined ( ) ;
169
182
expect ( ( ) => mockTransport . onclose ?.( ) ) . not . toThrow ( ) ;
170
183
} ) ;
171
184
} ) ;
@@ -175,16 +188,17 @@ describe('wrapMcpServerWithSentry', () => {
175
188
let wrappedMcpServer : ReturnType < typeof createMockMcpServer > ;
176
189
let mockTransport : ReturnType < typeof createMockTransport > ;
177
190
178
- beforeEach ( async ( ) => {
191
+ beforeEach ( ( ) => {
179
192
mockMcpServer = createMockMcpServer ( ) ;
180
193
wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
181
194
mockTransport = createMockTransport ( ) ;
182
195
mockTransport . sessionId = 'test-session-123' ;
183
-
184
- await wrappedMcpServer . connect ( mockTransport ) ;
196
+ // Don't connect here - let individual tests control when connection happens
185
197
} ) ;
186
198
187
- it ( 'should create spans with correct MCP server semantic attributes for tool operations' , ( ) => {
199
+ it ( 'should create spans with correct MCP server semantic attributes for tool operations' , async ( ) => {
200
+ await wrappedMcpServer . connect ( mockTransport ) ;
201
+
188
202
const jsonRpcRequest = {
189
203
jsonrpc : '2.0' ,
190
204
method : 'tools/call' ,
@@ -237,7 +251,9 @@ describe('wrapMcpServerWithSentry', () => {
237
251
) ;
238
252
} ) ;
239
253
240
- it ( 'should create spans with correct attributes for resource operations' , ( ) => {
254
+ it ( 'should create spans with correct attributes for resource operations' , async ( ) => {
255
+ await wrappedMcpServer . connect ( mockTransport ) ;
256
+
241
257
const jsonRpcRequest = {
242
258
jsonrpc : '2.0' ,
243
259
method : 'resources/read' ,
@@ -265,7 +281,9 @@ describe('wrapMcpServerWithSentry', () => {
265
281
) ;
266
282
} ) ;
267
283
268
- it ( 'should create spans with correct attributes for prompt operations' , ( ) => {
284
+ it ( 'should create spans with correct attributes for prompt operations' , async ( ) => {
285
+ await wrappedMcpServer . connect ( mockTransport ) ;
286
+
269
287
const jsonRpcRequest = {
270
288
jsonrpc : '2.0' ,
271
289
method : 'prompts/get' ,
@@ -293,7 +311,9 @@ describe('wrapMcpServerWithSentry', () => {
293
311
) ;
294
312
} ) ;
295
313
296
- it ( 'should create spans with correct attributes for notifications (no request id)' , ( ) => {
314
+ it ( 'should create spans with correct attributes for notifications (no request id)' , async ( ) => {
315
+ await wrappedMcpServer . connect ( mockTransport ) ;
316
+
297
317
const jsonRpcNotification = {
298
318
jsonrpc : '2.0' ,
299
319
method : 'notifications/tools/list_changed' ,
@@ -327,7 +347,9 @@ describe('wrapMcpServerWithSentry', () => {
327
347
expect ( attributes ) . not . toHaveProperty ( 'mcp.request.id' ) ;
328
348
} ) ;
329
349
330
- it ( 'should create spans for list operations without target in name' , ( ) => {
350
+ it ( 'should create spans for list operations without target in name' , async ( ) => {
351
+ await wrappedMcpServer . connect ( mockTransport ) ;
352
+
331
353
const jsonRpcRequest = {
332
354
jsonrpc : '2.0' ,
333
355
method : 'tools/list' ,
0 commit comments