1
- import { Scope } from '@sentry/svelte' ;
2
- import type { ServerLoad } from '@sveltejs/kit' ;
1
+ import { addTracingExtensions , Scope } from '@sentry/svelte' ;
2
+ import type { Load } from '@sveltejs/kit' ;
3
3
import { vi } from 'vitest' ;
4
4
5
5
import { wrapLoadWithSentry } from '../../src/client/load' ;
@@ -19,6 +19,19 @@ vi.mock('@sentry/svelte', async () => {
19
19
} ;
20
20
} ) ;
21
21
22
+ const mockTrace = vi . fn ( ) ;
23
+
24
+ vi . mock ( '@sentry/core' , async ( ) => {
25
+ const original = ( await vi . importActual ( '@sentry/core' ) ) as any ;
26
+ return {
27
+ ...original ,
28
+ trace : ( ...args : unknown [ ] ) => {
29
+ mockTrace ( ...args ) ;
30
+ return original . trace ( ...args ) ;
31
+ } ,
32
+ } ;
33
+ } ) ;
34
+
22
35
const mockAddExceptionMechanism = vi . fn ( ) ;
23
36
24
37
vi . mock ( '@sentry/utils' , async ( ) => {
@@ -33,41 +46,98 @@ function getById(_id?: string) {
33
46
throw new Error ( 'error' ) ;
34
47
}
35
48
49
+ const MOCK_LOAD_ARGS : any = {
50
+ params : { id : '123' } ,
51
+ route : {
52
+ id : '/users/[id]' ,
53
+ } ,
54
+ url : new URL ( 'http://localhost:3000/users/123' ) ,
55
+ request : {
56
+ headers : {
57
+ get : ( key : string ) => {
58
+ if ( key === 'sentry-trace' ) {
59
+ return '1234567890abcdef1234567890abcdef-1234567890abcdef-1' ;
60
+ }
61
+
62
+ if ( key === 'baggage' ) {
63
+ return (
64
+ 'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
65
+ 'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
66
+ 'sentry-trace_id=1234567890abcdef1234567890abcdef,sentry-sample_rate=1'
67
+ ) ;
68
+ }
69
+
70
+ return null ;
71
+ } ,
72
+ } ,
73
+ } ,
74
+ } ;
75
+
76
+ beforeAll ( ( ) => {
77
+ addTracingExtensions ( ) ;
78
+ } ) ;
79
+
36
80
describe ( 'wrapLoadWithSentry' , ( ) => {
37
81
beforeEach ( ( ) => {
38
82
mockCaptureException . mockClear ( ) ;
39
83
mockAddExceptionMechanism . mockClear ( ) ;
84
+ mockTrace . mockClear ( ) ;
40
85
mockScope = new Scope ( ) ;
41
86
} ) ;
42
87
43
88
it ( 'calls captureException' , async ( ) => {
44
- async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
89
+ async function load ( { params } : Parameters < Load > [ 0 ] ) : Promise < ReturnType < Load > > {
45
90
return {
46
91
post : getById ( params . id ) ,
47
92
} ;
48
93
}
49
94
50
95
const wrappedLoad = wrapLoadWithSentry ( load ) ;
51
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
96
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
52
97
await expect ( res ) . rejects . toThrow ( ) ;
53
98
54
99
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
55
100
} ) ;
56
101
102
+ it ( 'calls trace function' , async ( ) => {
103
+ async function load ( { params } : Parameters < Load > [ 0 ] ) : Promise < ReturnType < Load > > {
104
+ return {
105
+ post : params . id ,
106
+ } ;
107
+ }
108
+
109
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
110
+ await wrappedLoad ( MOCK_LOAD_ARGS ) ;
111
+
112
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
113
+ expect ( mockTrace ) . toHaveBeenCalledWith (
114
+ {
115
+ op : 'function.sveltekit.load' ,
116
+ name : '/users/[id]' ,
117
+ status : 'ok' ,
118
+ metadata : {
119
+ source : 'route' ,
120
+ } ,
121
+ } ,
122
+ expect . any ( Function ) ,
123
+ expect . any ( Function ) ,
124
+ ) ;
125
+ } ) ;
126
+
57
127
it ( 'adds an exception mechanism' , async ( ) => {
58
128
const addEventProcessorSpy = vi . spyOn ( mockScope , 'addEventProcessor' ) . mockImplementationOnce ( callback => {
59
129
void callback ( { } , { event_id : 'fake-event-id' } ) ;
60
130
return mockScope ;
61
131
} ) ;
62
132
63
- async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
133
+ async function load ( { params } : Parameters < Load > [ 0 ] ) : Promise < ReturnType < Load > > {
64
134
return {
65
135
post : getById ( params . id ) ,
66
136
} ;
67
137
}
68
138
69
139
const wrappedLoad = wrapLoadWithSentry ( load ) ;
70
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
140
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
71
141
await expect ( res ) . rejects . toThrow ( ) ;
72
142
73
143
expect ( addEventProcessorSpy ) . toBeCalledTimes ( 1 ) ;
0 commit comments