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' ;
@@ -26,7 +27,7 @@ vi.mock('@sentry/core', async () => {
26
27
const original = ( await vi . importActual ( '@sentry/core' ) ) as any ;
27
28
return {
28
29
...original ,
29
- trace : ( ...args ) => {
30
+ trace : ( ...args : unknown [ ] ) => {
30
31
mockTrace ( ...args ) ;
31
32
return original . trace ( ...args ) ;
32
33
} ,
@@ -48,10 +49,11 @@ function getById(_id?: string) {
48
49
}
49
50
50
51
const MOCK_LOAD_ARGS : any = {
51
- params : { id : '1 ' } ,
52
+ params : { id : '123 ' } ,
52
53
route : {
53
54
id : '/users/[id]' ,
54
55
} ,
56
+ url : new URL ( 'http://localhost:3000/users/123' ) ,
55
57
request : {
56
58
headers : {
57
59
get : ( key : string ) => {
@@ -73,6 +75,10 @@ const MOCK_LOAD_ARGS: any = {
73
75
} ,
74
76
} ;
75
77
78
+ beforeAll ( ( ) => {
79
+ addTracingExtensions ( ) ;
80
+ } ) ;
81
+
76
82
describe ( 'wrapLoadWithSentry' , ( ) => {
77
83
beforeEach ( ( ) => {
78
84
mockCaptureException . mockClear ( ) ;
@@ -81,7 +87,7 @@ describe('wrapLoadWithSentry', () => {
81
87
mockScope = new Scope ( ) ;
82
88
} ) ;
83
89
84
- it . only ( 'calls captureException' , async ( ) => {
90
+ it ( 'calls captureException' , async ( ) => {
85
91
async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
86
92
return {
87
93
post : getById ( params . id ) ,
@@ -106,18 +112,33 @@ describe('wrapLoadWithSentry', () => {
106
112
}
107
113
108
114
const wrappedLoad = wrapLoadWithSentry ( load ) ;
109
- await wrappedLoad ( {
110
- params : { id : '1' } ,
111
- route : {
112
- id : '' ,
113
- } ,
114
- headers : { 'sentry-trace' : '1234567890abcdef1234567890abcdef-1234567890abcdef-1' } ,
115
- } as any ) ;
115
+ await wrappedLoad ( MOCK_LOAD_ARGS ) ;
116
116
117
117
expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
118
- expect ( mockTrace ) . toHaveBeenCalledWith ( {
119
- op : 'function.sveltekit.load' ,
120
- } ) ;
118
+ expect ( mockTrace ) . toHaveBeenCalledWith (
119
+ {
120
+ op : 'function.sveltekit.load' ,
121
+ name : '/users/[id]' ,
122
+ parentSampled : true ,
123
+ parentSpanId : '1234567890abcdef' ,
124
+ status : 'ok' ,
125
+ traceId : '1234567890abcdef1234567890abcdef' ,
126
+ metadata : {
127
+ dynamicSamplingContext : {
128
+ environment : 'production' ,
129
+ public_key : 'dogsarebadatkeepingsecrets' ,
130
+ release : '1.0.0' ,
131
+ sample_rate : '1' ,
132
+ trace_id : '1234567890abcdef1234567890abcdef' ,
133
+ transaction : 'dogpark' ,
134
+ user_segment : 'segmentA' ,
135
+ } ,
136
+ source : 'route' ,
137
+ } ,
138
+ } ,
139
+ expect . any ( Function ) ,
140
+ expect . any ( Function ) ,
141
+ ) ;
121
142
} ) ;
122
143
123
144
describe ( 'with error() helper' , ( ) => {
@@ -140,7 +161,7 @@ describe('wrapLoadWithSentry', () => {
140
161
}
141
162
142
163
const wrappedLoad = wrapLoadWithSentry ( load ) ;
143
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
164
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
144
165
await expect ( res ) . rejects . toThrow ( ) ;
145
166
146
167
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( times ) ;
@@ -160,7 +181,7 @@ describe('wrapLoadWithSentry', () => {
160
181
}
161
182
162
183
const wrappedLoad = wrapLoadWithSentry ( load ) ;
163
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
184
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
164
185
await expect ( res ) . rejects . toThrow ( ) ;
165
186
166
187
expect ( addEventProcessorSpy ) . toBeCalledTimes ( 1 ) ;
0 commit comments