11import { Scope } from '@sentry/node' ;
2- // eslint-disable-next-line import/no-unresolved
32import type { ServerLoad } from '@sveltejs/kit' ;
3+ import { error } from '@sveltejs/kit' ;
4+ import { vi } from 'vitest' ;
45
56import { wrapLoadWithSentry } from '../../src/server/load' ;
67
7- const mockCaptureException = jest . fn ( ) ;
8+ const mockCaptureException = vi . fn ( ) ;
89let mockScope = new Scope ( ) ;
910
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 ;
1213 return {
1314 ...original ,
1415 captureException : ( err : unknown , cb : ( arg0 : unknown ) => unknown ) => {
@@ -19,10 +20,10 @@ jest.mock('@sentry/node', () => {
1920 } ;
2021} ) ;
2122
22- const mockAddExceptionMechanism = jest . fn ( ) ;
23+ const mockAddExceptionMechanism = vi . fn ( ) ;
2324
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 ;
2627 return {
2728 ...original ,
2829 addExceptionMechanism : ( ...args : unknown [ ] ) => mockAddExceptionMechanism ( ...args ) ,
@@ -33,12 +34,6 @@ function getById(_id?: string) {
3334 throw new Error ( 'error' ) ;
3435}
3536
36- async function erroringLoad ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
37- return {
38- post : getById ( params . id ) ,
39- } ;
40- }
41-
4237describe ( 'wrapLoadWithSentry' , ( ) => {
4338 beforeEach ( ( ) => {
4439 mockCaptureException . mockClear ( ) ;
@@ -47,20 +42,59 @@ describe('wrapLoadWithSentry', () => {
4742 } ) ;
4843
4944 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 ) ;
5152 const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
5253 await expect ( res ) . rejects . toThrow ( ) ;
5354
5455 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
5556 } ) ;
5657
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+
5785 it ( 'adds an exception mechanism' , async ( ) => {
58- const addEventProcessorSpy = jest . spyOn ( mockScope , 'addEventProcessor' ) . mockImplementationOnce ( callback => {
86+ const addEventProcessorSpy = vi . spyOn ( mockScope , 'addEventProcessor' ) . mockImplementationOnce ( callback => {
5987 void callback ( { } , { event_id : 'fake-event-id' } ) ;
6088 return mockScope ;
6189 } ) ;
6290
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 ) ;
6498 const res = wrappedLoad ( { params : { id : '1' } } as any ) ;
6599 await expect ( res ) . rejects . toThrow ( ) ;
66100
0 commit comments