|
| 1 | +import { Scope } from '@sentry/node'; |
| 2 | +import type { ServerLoad } from '@sveltejs/kit'; |
| 3 | +import { error } from '@sveltejs/kit'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | + |
| 6 | +import { wrapLoadWithSentry } from '../../src/server/load'; |
| 7 | + |
| 8 | +const mockCaptureException = vi.fn(); |
| 9 | +let mockScope = new Scope(); |
| 10 | + |
| 11 | +vi.mock('@sentry/node', async () => { |
| 12 | + const original = (await vi.importActual('@sentry/node')) as any; |
| 13 | + return { |
| 14 | + ...original, |
| 15 | + captureException: (err: unknown, cb: (arg0: unknown) => unknown) => { |
| 16 | + cb(mockScope); |
| 17 | + mockCaptureException(err, cb); |
| 18 | + return original.captureException(err, cb); |
| 19 | + }, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +const mockAddExceptionMechanism = vi.fn(); |
| 24 | + |
| 25 | +vi.mock('@sentry/utils', async () => { |
| 26 | + const original = (await vi.importActual('@sentry/utils')) as any; |
| 27 | + return { |
| 28 | + ...original, |
| 29 | + addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args), |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +function getById(_id?: string) { |
| 34 | + throw new Error('error'); |
| 35 | +} |
| 36 | + |
| 37 | +describe('wrapLoadWithSentry', () => { |
| 38 | + beforeEach(() => { |
| 39 | + mockCaptureException.mockClear(); |
| 40 | + mockAddExceptionMechanism.mockClear(); |
| 41 | + mockScope = new Scope(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('calls captureException', async () => { |
| 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); |
| 52 | + const res = wrappedLoad({ params: { id: '1' } } as any); |
| 53 | + await expect(res).rejects.toThrow(); |
| 54 | + |
| 55 | + expect(mockCaptureException).toHaveBeenCalledTimes(1); |
| 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 | + |
| 85 | + it('adds an exception mechanism', async () => { |
| 86 | + const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => { |
| 87 | + void callback({}, { event_id: 'fake-event-id' }); |
| 88 | + return mockScope; |
| 89 | + }); |
| 90 | + |
| 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); |
| 98 | + const res = wrappedLoad({ params: { id: '1' } } as any); |
| 99 | + await expect(res).rejects.toThrow(); |
| 100 | + |
| 101 | + expect(addEventProcessorSpy).toBeCalledTimes(1); |
| 102 | + expect(mockAddExceptionMechanism).toBeCalledTimes(1); |
| 103 | + expect(mockAddExceptionMechanism).toBeCalledWith( |
| 104 | + {}, |
| 105 | + { handled: false, type: 'sveltekit', data: { function: 'load' } }, |
| 106 | + ); |
| 107 | + }); |
| 108 | +}); |
0 commit comments