@@ -20,6 +20,19 @@ vi.mock('@sentry/node', async () => {
20
20
} ;
21
21
} ) ;
22
22
23
+ const mockTrace = vi . fn ( ) ;
24
+
25
+ vi . mock ( '@sentry/core' , async ( ) => {
26
+ const original = ( await vi . importActual ( '@sentry/core' ) ) as any ;
27
+ return {
28
+ ...original ,
29
+ trace : ( ...args ) => {
30
+ mockTrace ( ...args ) ;
31
+ return original . trace ( ...args ) ;
32
+ } ,
33
+ } ;
34
+ } ) ;
35
+
23
36
const mockAddExceptionMechanism = vi . fn ( ) ;
24
37
25
38
vi . mock ( '@sentry/utils' , async ( ) => {
@@ -34,27 +47,79 @@ function getById(_id?: string) {
34
47
throw new Error ( 'error' ) ;
35
48
}
36
49
50
+ const MOCK_LOAD_ARGS : any = {
51
+ params : { id : '1' } ,
52
+ route : {
53
+ id : '/users/[id]' ,
54
+ } ,
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
+
37
76
describe ( 'wrapLoadWithSentry' , ( ) => {
38
77
beforeEach ( ( ) => {
39
78
mockCaptureException . mockClear ( ) ;
40
79
mockAddExceptionMechanism . mockClear ( ) ;
80
+ mockTrace . mockClear ( ) ;
41
81
mockScope = new Scope ( ) ;
42
82
} ) ;
43
83
44
- it ( 'calls captureException' , async ( ) => {
84
+ it . only ( 'calls captureException' , async ( ) => {
45
85
async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
46
86
return {
47
87
post : getById ( params . id ) ,
48
88
} ;
49
89
}
50
90
51
91
const wrappedLoad = wrapLoadWithSentry ( load ) ;
52
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
92
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
53
93
await expect ( res ) . rejects . toThrow ( ) ;
54
94
95
+ // create promise that waits for timeout
96
+ await new Promise ( resolve => setTimeout ( resolve , 1000 , 'timeout' ) ) ;
97
+
55
98
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
56
99
} ) ;
57
100
101
+ it ( 'calls trace function' , async ( ) => {
102
+ async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
103
+ return {
104
+ post : params . id ,
105
+ } ;
106
+ }
107
+
108
+ 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 ) ;
116
+
117
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
118
+ expect ( mockTrace ) . toHaveBeenCalledWith ( {
119
+ op : 'function.sveltekit.load' ,
120
+ } ) ;
121
+ } ) ;
122
+
58
123
describe ( 'with error() helper' , ( ) => {
59
124
it . each ( [
60
125
// [statusCode, timesCalled]
0 commit comments