|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createCatcher, createTransport, getLastPayload, wait } from './catcher.helpers'; |
| 3 | +import { MessageProcessor, ProcessingPayload } from '@hawk.so/core'; |
| 4 | + |
| 5 | +const mockParse = vi.hoisted(() => vi.fn().mockResolvedValue([])); |
| 6 | +vi.mock('../src/modules/stackParser', () => ({ |
| 7 | + default: class { |
| 8 | + parse = mockParse; |
| 9 | + } |
| 10 | +})); |
| 11 | + |
| 12 | +describe('Catcher', () => { |
| 13 | + describe('message processor', () => { |
| 14 | + it('should send original message when processor does not modify it', async () => { |
| 15 | + const { sendSpy, transport } = createTransport(); |
| 16 | + const applySpy = vi.fn((payload: ProcessingPayload<'errors/javascript'>) => payload); |
| 17 | + const processor: MessageProcessor<'errors/javascript'> = { apply: applySpy }; |
| 18 | + const hawk = createCatcher(transport, { messageProcessors: [processor] }); |
| 19 | + |
| 20 | + hawk.send('original message'); |
| 21 | + await wait(); |
| 22 | + |
| 23 | + expect(applySpy).toHaveBeenCalledTimes(1); |
| 24 | + expect(sendSpy).toHaveBeenCalledTimes(1); |
| 25 | + expect(getLastPayload(sendSpy).message).toBe('original message'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should send modified message when processor modify it', async () => { |
| 29 | + const { sendSpy, transport } = createTransport(); |
| 30 | + const applySpy = vi.fn((payload: ProcessingPayload<'errors/javascript'>) => { |
| 31 | + return { |
| 32 | + ...payload, |
| 33 | + message: 'modified message', |
| 34 | + }; |
| 35 | + }); |
| 36 | + const processor: MessageProcessor<'errors/javascript'> = { apply: applySpy, }; |
| 37 | + const hawk = createCatcher(transport, { messageProcessors: [processor], }); |
| 38 | + |
| 39 | + hawk.send('original message'); |
| 40 | + await wait(); |
| 41 | + |
| 42 | + expect(applySpy).toHaveBeenCalledTimes(1); |
| 43 | + expect(sendSpy).toHaveBeenCalledTimes(1); |
| 44 | + expect(getLastPayload(sendSpy).message).toBe('modified message'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should drop message when processor returns null', async () => { |
| 48 | + const { sendSpy, transport } = createTransport(); |
| 49 | + const applySpy = vi.fn(() => null); |
| 50 | + const processor: MessageProcessor<'errors/javascript'> = { apply: applySpy, }; |
| 51 | + const hawk = createCatcher(transport, { messageProcessors: [processor], }); |
| 52 | + |
| 53 | + hawk.send('test error'); |
| 54 | + await wait(); |
| 55 | + |
| 56 | + expect(applySpy).toHaveBeenCalledTimes(1); |
| 57 | + expect(sendSpy).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments