1
+ import { addTracingExtensions } from '@sentry/core' ;
1
2
import { Scope } from '@sentry/node' ;
2
3
import type { ServerLoad } from '@sveltejs/kit' ;
3
4
import { error } from '@sveltejs/kit' ;
@@ -20,6 +21,19 @@ vi.mock('@sentry/node', async () => {
20
21
} ;
21
22
} ) ;
22
23
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
+
23
37
const mockAddExceptionMechanism = vi . fn ( ) ;
24
38
25
39
vi . mock ( '@sentry/utils' , async ( ) => {
@@ -34,10 +48,42 @@ function getById(_id?: string) {
34
48
throw new Error ( 'error' ) ;
35
49
}
36
50
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
+
37
82
describe ( 'wrapLoadWithSentry' , ( ) => {
38
83
beforeEach ( ( ) => {
39
84
mockCaptureException . mockClear ( ) ;
40
85
mockAddExceptionMechanism . mockClear ( ) ;
86
+ mockTrace . mockClear ( ) ;
41
87
mockScope = new Scope ( ) ;
42
88
} ) ;
43
89
@@ -49,12 +95,49 @@ describe('wrapLoadWithSentry', () => {
49
95
}
50
96
51
97
const wrappedLoad = wrapLoadWithSentry ( load ) ;
52
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
98
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
53
99
await expect ( res ) . rejects . toThrow ( ) ;
54
100
55
101
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
56
102
} ) ;
57
103
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
+
58
141
describe ( 'with error() helper' , ( ) => {
59
142
it . each ( [
60
143
// [statusCode, timesCalled]
@@ -75,7 +158,7 @@ describe('wrapLoadWithSentry', () => {
75
158
}
76
159
77
160
const wrappedLoad = wrapLoadWithSentry ( load ) ;
78
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
161
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
79
162
await expect ( res ) . rejects . toThrow ( ) ;
80
163
81
164
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( times ) ;
@@ -95,7 +178,7 @@ describe('wrapLoadWithSentry', () => {
95
178
}
96
179
97
180
const wrappedLoad = wrapLoadWithSentry ( load ) ;
98
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
181
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
99
182
await expect ( res ) . rejects . toThrow ( ) ;
100
183
101
184
expect ( addEventProcessorSpy ) . toBeCalledTimes ( 1 ) ;
0 commit comments