1+ import { addTracingExtensions } from '@sentry/core' ;
12import { Scope } from '@sentry/node' ;
23import type { ServerLoad } from '@sveltejs/kit' ;
34import { error } from '@sveltejs/kit' ;
@@ -20,6 +21,19 @@ vi.mock('@sentry/node', async () => {
2021 } ;
2122} ) ;
2223
24+ const mockTrace = vi . fn ( ) ;
25+
26+ vi . mock ( '@sentry/core' , async ( ) => {
27+ const original = ( await vi . importActual ( '@sentry/core' ) ) as any ;
28+ return {
29+ ...original ,
30+ trace : ( ...args : unknown [ ] ) => {
31+ mockTrace ( ...args ) ;
32+ return original . trace ( ...args ) ;
33+ } ,
34+ } ;
35+ } ) ;
36+
2337const mockAddExceptionMechanism = vi . fn ( ) ;
2438
2539vi . mock ( '@sentry/utils' , async ( ) => {
@@ -34,10 +48,42 @@ function getById(_id?: string) {
3448 throw new Error ( 'error' ) ;
3549}
3650
51+ const MOCK_LOAD_ARGS : any = {
52+ params : { id : '123' } ,
53+ route : {
54+ id : '/users/[id]' ,
55+ } ,
56+ url : new URL ( 'http://localhost:3000/users/123' ) ,
57+ request : {
58+ headers : {
59+ get : ( key : string ) => {
60+ if ( key === 'sentry-trace' ) {
61+ return '1234567890abcdef1234567890abcdef-1234567890abcdef-1' ;
62+ }
63+
64+ if ( key === 'baggage' ) {
65+ return (
66+ 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
67+ 'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
68+ 'sentry-trace_id=1234567890abcdef1234567890abcdef,sentry-sample_rate=1'
69+ ) ;
70+ }
71+
72+ return null ;
73+ } ,
74+ } ,
75+ } ,
76+ } ;
77+
78+ beforeAll ( ( ) => {
79+ addTracingExtensions ( ) ;
80+ } ) ;
81+
3782describe ( 'wrapLoadWithSentry' , ( ) => {
3883 beforeEach ( ( ) => {
3984 mockCaptureException . mockClear ( ) ;
4085 mockAddExceptionMechanism . mockClear ( ) ;
86+ mockTrace . mockClear ( ) ;
4187 mockScope = new Scope ( ) ;
4288 } ) ;
4389
@@ -49,12 +95,49 @@ describe('wrapLoadWithSentry', () => {
4995 }
5096
5197 const wrappedLoad = wrapLoadWithSentry ( load ) ;
52- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
98+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
5399 await expect ( res ) . rejects . toThrow ( ) ;
54100
55101 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
56102 } ) ;
57103
104+ it ( 'calls trace function' , async ( ) => {
105+ async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
106+ return {
107+ post : params . id ,
108+ } ;
109+ }
110+
111+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
112+ await wrappedLoad ( MOCK_LOAD_ARGS ) ;
113+
114+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
115+ expect ( mockTrace ) . toHaveBeenCalledWith (
116+ {
117+ op : 'function.sveltekit.load' ,
118+ name : '/users/[id]' ,
119+ parentSampled : true ,
120+ parentSpanId : '1234567890abcdef' ,
121+ status : 'ok' ,
122+ traceId : '1234567890abcdef1234567890abcdef' ,
123+ metadata : {
124+ dynamicSamplingContext : {
125+ environment : 'production' ,
126+ public_key : 'dogsarebadatkeepingsecrets' ,
127+ release : '1.0.0' ,
128+ sample_rate : '1' ,
129+ trace_id : '1234567890abcdef1234567890abcdef' ,
130+ transaction : 'dogpark' ,
131+ user_segment : 'segmentA' ,
132+ } ,
133+ source : 'route' ,
134+ } ,
135+ } ,
136+ expect . any ( Function ) ,
137+ expect . any ( Function ) ,
138+ ) ;
139+ } ) ;
140+
58141 describe ( 'with error() helper' , ( ) => {
59142 it . each ( [
60143 // [statusCode, timesCalled]
@@ -75,7 +158,7 @@ describe('wrapLoadWithSentry', () => {
75158 }
76159
77160 const wrappedLoad = wrapLoadWithSentry ( load ) ;
78- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
161+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
79162 await expect ( res ) . rejects . toThrow ( ) ;
80163
81164 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( times ) ;
@@ -95,7 +178,7 @@ describe('wrapLoadWithSentry', () => {
95178 }
96179
97180 const wrappedLoad = wrapLoadWithSentry ( load ) ;
98- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
181+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
99182 await expect ( res ) . rejects . toThrow ( ) ;
100183
101184 expect ( addEventProcessorSpy ) . toBeCalledTimes ( 1 ) ;
0 commit comments