diff --git a/packages/node/test/handlers.test.ts b/packages/node/test/handlers.test.ts index 7e995d7e8d6b..d189f7531ba0 100644 --- a/packages/node/test/handlers.test.ts +++ b/packages/node/test/handlers.test.ts @@ -308,8 +308,8 @@ describe('tracingHandler', () => { }); }); - it('lets all spans being finished before calling `finish` itself, despite being registered to `res.finish` event first', done => { - const transaction = new Transaction({ name: 'mockTransaction' }); + it('waits to finish transaction until all spans are finished, even though `transaction.finish()` is registered on `res.finish` event first', done => { + const transaction = new Transaction({ name: 'mockTransaction', sampled: true }); transaction.initSpanRecorder(); const span = transaction.startChild({ description: 'reallyCoolHandler', @@ -319,6 +319,11 @@ describe('tracingHandler', () => { const finishSpan = jest.spyOn(span, 'finish'); const finishTransaction = jest.spyOn(transaction, 'finish'); + let sentEvent: Event; + jest.spyOn((transaction as any)._hub, 'captureEvent').mockImplementation(event => { + sentEvent = event as Event; + }); + sentryTracingMiddleware(req, res, next); res.once('finish', () => { span.finish(); @@ -328,7 +333,9 @@ describe('tracingHandler', () => { setImmediate(() => { expect(finishSpan).toHaveBeenCalled(); expect(finishTransaction).toHaveBeenCalled(); - expect(span.endTimestamp).toBeLessThan(transaction.endTimestamp!); + expect(span.endTimestamp).toBeLessThanOrEqual(transaction.endTimestamp!); + expect(sentEvent.spans?.length).toEqual(1); + expect(sentEvent.spans?.[0].spanId).toEqual(span.spanId); done(); }); });