|
| 1 | +import { Scope } from '@sentry/node'; |
| 2 | +// For now disable the import/no-unresolved rule, because we don't have a way to |
| 3 | +// tell eslint that we are only importing types from the @sveltejs/kit package without |
| 4 | +// adding a custom resolver, which will take too much time. |
| 5 | +// eslint-disable-next-line import/no-unresolved |
| 6 | +import type { HandleServerError, RequestEvent } from '@sveltejs/kit'; |
| 7 | + |
| 8 | +import { handleErrorWithSentry } from '../../src/server/handleError'; |
| 9 | + |
| 10 | +const mockCaptureException = jest.fn(); |
| 11 | +let mockScope = new Scope(); |
| 12 | + |
| 13 | +jest.mock('@sentry/node', () => { |
| 14 | + const original = jest.requireActual('@sentry/core'); |
| 15 | + return { |
| 16 | + ...original, |
| 17 | + captureException: (err: unknown, cb: (arg0: unknown) => unknown) => { |
| 18 | + cb(mockScope); |
| 19 | + mockCaptureException(err, cb); |
| 20 | + return original.captureException(err, cb); |
| 21 | + }, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +const mockAddExceptionMechanism = jest.fn(); |
| 26 | + |
| 27 | +jest.mock('@sentry/utils', () => { |
| 28 | + const original = jest.requireActual('@sentry/utils'); |
| 29 | + return { |
| 30 | + ...original, |
| 31 | + addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args), |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +function handleError(_input: { error: unknown; event: RequestEvent }): ReturnType<HandleServerError> { |
| 36 | + return { |
| 37 | + message: 'Whoops!', |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +const requestEvent = {} as RequestEvent; |
| 42 | + |
| 43 | +describe('handleError', () => { |
| 44 | + beforeEach(() => { |
| 45 | + mockCaptureException.mockClear(); |
| 46 | + mockAddExceptionMechanism.mockClear(); |
| 47 | + mockScope = new Scope(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('works when a handleError func is not provided', async () => { |
| 51 | + const wrappedHandleError = handleErrorWithSentry(); |
| 52 | + const mockError = new Error('test'); |
| 53 | + const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent }); |
| 54 | + |
| 55 | + expect(returnVal).not.toBeDefined(); |
| 56 | + expect(mockCaptureException).toHaveBeenCalledTimes(1); |
| 57 | + expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function)); |
| 58 | + }); |
| 59 | + |
| 60 | + it('calls captureException', async () => { |
| 61 | + const wrappedHandleError = handleErrorWithSentry(handleError); |
| 62 | + const mockError = new Error('test'); |
| 63 | + const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent }); |
| 64 | + |
| 65 | + expect(returnVal!.message).toEqual('Whoops!'); |
| 66 | + expect(mockCaptureException).toHaveBeenCalledTimes(1); |
| 67 | + expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function)); |
| 68 | + }); |
| 69 | + |
| 70 | + it('adds an exception mechanism', async () => { |
| 71 | + const addEventProcessorSpy = jest.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => { |
| 72 | + void callback({}, { event_id: 'fake-event-id' }); |
| 73 | + return mockScope; |
| 74 | + }); |
| 75 | + |
| 76 | + const wrappedHandleError = handleErrorWithSentry(handleError); |
| 77 | + const mockError = new Error('test'); |
| 78 | + await wrappedHandleError({ error: mockError, event: requestEvent }); |
| 79 | + |
| 80 | + expect(addEventProcessorSpy).toBeCalledTimes(1); |
| 81 | + expect(mockAddExceptionMechanism).toBeCalledTimes(1); |
| 82 | + expect(mockAddExceptionMechanism).toBeCalledWith({}, { handled: false, type: 'sveltekit' }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments