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,22 +46,54 @@ 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 ) ;
@@ -60,14 +105,14 @@ describe('wrapLoadWithSentry', () => {
60
105
return mockScope ;
61
106
} ) ;
62
107
63
- async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
108
+ async function load ( { params } : Parameters < Load > [ 0 ] ) : Promise < ReturnType < Load > > {
64
109
return {
65
110
post : getById ( params . id ) ,
66
111
} ;
67
112
}
68
113
69
114
const wrappedLoad = wrapLoadWithSentry ( load ) ;
70
- const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
115
+ const res = wrappedLoad ( MOCK_LOAD_ARGS ) ;
71
116
await expect ( res ) . rejects . toThrow ( ) ;
72
117
73
118
expect ( addEventProcessorSpy ) . toBeCalledTimes ( 1 ) ;
0 commit comments