@@ -247,7 +247,7 @@ describe('tracingHandler', () => {
247
247
expect ( transaction ) . toEqual ( expect . objectContaining ( { name : `GET ${ urlString } ` , op : 'http.server' } ) ) ;
248
248
} ) ;
249
249
250
- it ( 'pulls status code from the response' , async ( ) => {
250
+ it ( 'pulls status code from the response' , done => {
251
251
const transaction = new Transaction ( { name : 'mockTransaction' } ) ;
252
252
jest . spyOn ( sentryCore , 'startTransaction' ) . mockReturnValue ( transaction as TransactionType ) ;
253
253
const finishTransaction = jest . spyOn ( transaction , 'finish' ) ;
@@ -256,9 +256,12 @@ describe('tracingHandler', () => {
256
256
res . statusCode = 200 ;
257
257
res . emit ( 'finish' ) ;
258
258
259
- expect ( finishTransaction ) . toHaveBeenCalled ( ) ;
260
- expect ( transaction . status ) . toBe ( SpanStatus . Ok ) ;
261
- expect ( transaction . tags ) . toEqual ( expect . objectContaining ( { 'http.status_code' : '200' } ) ) ;
259
+ setImmediate ( ( ) => {
260
+ expect ( finishTransaction ) . toHaveBeenCalled ( ) ;
261
+ expect ( transaction . status ) . toBe ( SpanStatus . Ok ) ;
262
+ expect ( transaction . tags ) . toEqual ( expect . objectContaining ( { 'http.status_code' : '200' } ) ) ;
263
+ done ( ) ;
264
+ } ) ;
262
265
} ) ;
263
266
264
267
it ( 'strips query string from request path' , ( ) => {
@@ -291,14 +294,42 @@ describe('tracingHandler', () => {
291
294
expect ( transaction ?. name ) . toBe ( `GET ${ urlString } ` ) ;
292
295
} ) ;
293
296
294
- it ( 'closes the transaction when request processing is done' , ( ) => {
297
+ it ( 'closes the transaction when request processing is done' , done => {
298
+ const transaction = new Transaction ( { name : 'mockTransaction' } ) ;
299
+ jest . spyOn ( sentryCore , 'startTransaction' ) . mockReturnValue ( transaction as TransactionType ) ;
300
+ const finishTransaction = jest . spyOn ( transaction , 'finish' ) ;
301
+
302
+ sentryTracingMiddleware ( req , res , next ) ;
303
+ res . emit ( 'finish' ) ;
304
+
305
+ setImmediate ( ( ) => {
306
+ expect ( finishTransaction ) . toHaveBeenCalled ( ) ;
307
+ done ( ) ;
308
+ } ) ;
309
+ } ) ;
310
+
311
+ it ( 'lets all spans being finished before calling `finish` itself, despite being registered to `res.finish` event first' , done => {
295
312
const transaction = new Transaction ( { name : 'mockTransaction' } ) ;
313
+ transaction . initSpanRecorder ( ) ;
314
+ const span = transaction . startChild ( {
315
+ description : 'reallyCoolHandler' ,
316
+ op : 'middleware' ,
317
+ } ) ;
296
318
jest . spyOn ( sentryCore , 'startTransaction' ) . mockReturnValue ( transaction as TransactionType ) ;
319
+ const finishSpan = jest . spyOn ( span , 'finish' ) ;
297
320
const finishTransaction = jest . spyOn ( transaction , 'finish' ) ;
298
321
299
322
sentryTracingMiddleware ( req , res , next ) ;
323
+ res . once ( 'finish' , ( ) => {
324
+ span . finish ( ) ;
325
+ } ) ;
300
326
res . emit ( 'finish' ) ;
301
327
302
- expect ( finishTransaction ) . toHaveBeenCalled ( ) ;
328
+ setImmediate ( ( ) => {
329
+ expect ( finishSpan ) . toHaveBeenCalled ( ) ;
330
+ expect ( finishTransaction ) . toHaveBeenCalled ( ) ;
331
+ expect ( span . endTimestamp ) . toBeLessThan ( transaction . endTimestamp ! ) ;
332
+ done ( ) ;
333
+ } ) ;
303
334
} ) ;
304
335
} ) ; // end describe('tracingHandler')
0 commit comments