1
1
import { Scope } from '@sentry/node' ;
2
- // eslint-disable-next-line import/no-unresolved
3
2
import type { ServerLoad } from '@sveltejs/kit' ;
3
+ import { error } from '@sveltejs/kit' ;
4
+ import { vi } from 'vitest' ;
4
5
5
6
import { wrapLoadWithSentry } from '../../src/server/load' ;
6
7
7
- const mockCaptureException = jest . fn ( ) ;
8
+ const mockCaptureException = vi . fn ( ) ;
8
9
let mockScope = new Scope ( ) ;
9
10
10
- jest . mock ( '@sentry/node' , ( ) => {
11
- const original = jest . requireActual ( '@sentry/node' ) ;
11
+ vi . mock ( '@sentry/node' , async ( ) => {
12
+ const original = ( await vi . importActual ( '@sentry/node' ) ) as any ;
12
13
return {
13
14
...original ,
14
15
captureException : ( err : unknown , cb : ( arg0 : unknown ) => unknown ) => {
@@ -19,10 +20,10 @@ jest.mock('@sentry/node', () => {
19
20
} ;
20
21
} ) ;
21
22
22
- const mockAddExceptionMechanism = jest . fn ( ) ;
23
+ const mockAddExceptionMechanism = vi . fn ( ) ;
23
24
24
- jest . mock ( '@sentry/utils' , ( ) => {
25
- const original = jest . requireActual ( '@sentry/utils' ) ;
25
+ vi . mock ( '@sentry/utils' , async ( ) => {
26
+ const original = ( await vi . importActual ( '@sentry/utils' ) ) as any ;
26
27
return {
27
28
...original ,
28
29
addExceptionMechanism : ( ...args : unknown [ ] ) => mockAddExceptionMechanism ( ...args ) ,
@@ -33,12 +34,6 @@ function getById(_id?: string) {
33
34
throw new Error ( 'error' ) ;
34
35
}
35
36
36
- async function erroringLoad ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
37
- return {
38
- post : getById ( params . id ) ,
39
- } ;
40
- }
41
-
42
37
describe ( 'wrapLoadWithSentry' , ( ) => {
43
38
beforeEach ( ( ) => {
44
39
mockCaptureException . mockClear ( ) ;
@@ -47,20 +42,59 @@ describe('wrapLoadWithSentry', () => {
47
42
} ) ;
48
43
49
44
it ( 'calls captureException' , async ( ) => {
50
- const wrappedLoad = wrapLoadWithSentry ( erroringLoad ) ;
45
+ async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
46
+ return {
47
+ post : getById ( params . id ) ,
48
+ } ;
49
+ }
50
+
51
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
51
52
const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
52
53
await expect ( res ) . rejects . toThrow ( ) ;
53
54
54
55
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
55
56
} ) ;
56
57
58
+ describe ( 'with error() helper' , ( ) => {
59
+ it . each ( [
60
+ // [statusCode, timesCalled]
61
+ [ 400 , 0 ] ,
62
+ [ 401 , 0 ] ,
63
+ [ 403 , 0 ] ,
64
+ [ 404 , 0 ] ,
65
+ [ 409 , 0 ] ,
66
+ [ 429 , 0 ] ,
67
+ [ 499 , 0 ] ,
68
+ [ 500 , 1 ] ,
69
+ [ 501 , 1 ] ,
70
+ [ 503 , 1 ] ,
71
+ [ 504 , 1 ] ,
72
+ ] ) ( 'error with status code %s calls captureException %s times' , async ( code , times ) => {
73
+ async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
74
+ throw error ( code , params . id ) ;
75
+ }
76
+
77
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
78
+ const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
79
+ await expect ( res ) . rejects . toThrow ( ) ;
80
+
81
+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( times ) ;
82
+ } ) ;
83
+ } ) ;
84
+
57
85
it ( 'adds an exception mechanism' , async ( ) => {
58
- const addEventProcessorSpy = jest . spyOn ( mockScope , 'addEventProcessor' ) . mockImplementationOnce ( callback => {
86
+ const addEventProcessorSpy = vi . spyOn ( mockScope , 'addEventProcessor' ) . mockImplementationOnce ( callback => {
59
87
void callback ( { } , { event_id : 'fake-event-id' } ) ;
60
88
return mockScope ;
61
89
} ) ;
62
90
63
- const wrappedLoad = wrapLoadWithSentry ( erroringLoad ) ;
91
+ async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
92
+ return {
93
+ post : getById ( params . id ) ,
94
+ } ;
95
+ }
96
+
97
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
64
98
const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
65
99
await expect ( res ) . rejects . toThrow ( ) ;
66
100
0 commit comments