1+ import { HTTP_ROUTE } from '@sentry/conventions/attributes' ;
12import type { ErrorContext } from 'elysia' ;
23import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
34
@@ -32,6 +33,12 @@ const mockGetIsolationScope = vi.fn(() => ({
3233const mockGetClient = vi . fn ( ( ) => ( {
3334 on : vi . fn ( ) ,
3435} ) ) ;
36+ const mockRootSpan = {
37+ setAttributes : vi . fn ( ) ,
38+ updateName : vi . fn ( ) ,
39+ } ;
40+ const mockGetActiveSpan = vi . fn ( ) ;
41+ const mockGetRootSpan = vi . fn ( ( ) => mockRootSpan ) ;
3542const mockGetTraceData = vi . fn ( ( ) => ( {
3643 'sentry-trace' : 'abc123-def456-1' ,
3744 baggage : 'sentry-environment=test,sentry-trace_id=abc123' ,
@@ -43,8 +50,10 @@ vi.mock('@sentry/core', async importActual => {
4350 return {
4451 ...actual ,
4552 captureException : ( ...args : unknown [ ] ) => mockCaptureException ( ...args ) ,
53+ getActiveSpan : ( ) => mockGetActiveSpan ( ) ,
4654 getIsolationScope : ( ) => mockGetIsolationScope ( ) ,
4755 getClient : ( ) => mockGetClient ( ) ,
56+ getRootSpan : ( ) => mockGetRootSpan ( ) ,
4857 getTraceData : ( ) => mockGetTraceData ( ) ,
4958 } ;
5059} ) ;
@@ -88,6 +97,23 @@ describe('withElysia', () => {
8897 expect ( headers [ 'baggage' ] ) . toBe ( 'sentry-environment=test,sentry-trace_id=abc123' ) ;
8998 } ) ;
9099
100+ it ( 'sets the matched route on the root span' , ( ) => {
101+ mockGetActiveSpan . mockReturnValueOnce ( mockRootSpan ) ;
102+ // @ts -expect-error - mock app
103+ withElysia ( mockApp ) ;
104+
105+ onAfterHandleHandler ( {
106+ route : '/users/:id' ,
107+ request : new Request ( 'https://example.com/users/42' , { method : 'GET' } ) ,
108+ set : { headers : { } } ,
109+ } ) ;
110+
111+ expect ( mockRootSpan . setAttributes ) . toHaveBeenCalledWith ( {
112+ 'sentry.source' : 'route' ,
113+ [ HTTP_ROUTE ] : '/users/:id' ,
114+ } ) ;
115+ } ) ;
116+
91117 it ( 'does not set headers when trace data is empty' , ( ) => {
92118 mockGetTraceData . mockReturnValueOnce ( { } ) ;
93119 // @ts -expect-error - mock app
0 commit comments