|
| 1 | +import { EventProcessor, Integration } from '@sentry/types'; |
| 2 | + |
| 3 | +import { Debug } from '../src/debug'; |
| 4 | + |
| 5 | +const mockGetCurrentHub = (getIntegrationResult: Integration) => ({ |
| 6 | + getIntegration: jest.fn(() => getIntegrationResult), |
| 7 | +}); |
| 8 | + |
| 9 | +// Replace console log with a mock so we can check for invocations |
| 10 | +const mockConsoleLog = jest.fn(); |
| 11 | +const originalConsoleLog = global.console.log; |
| 12 | +global.console.log = mockConsoleLog; |
| 13 | + |
| 14 | +describe('Debug integration setup should register an event processor that', () => { |
| 15 | + afterAll(() => { |
| 16 | + // Reset mocked console log to original one |
| 17 | + global.console.log = originalConsoleLog; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('logs an event', () => { |
| 25 | + const debugIntegration = new Debug(); |
| 26 | + |
| 27 | + const captureEventProcessor = (eventProcessor: EventProcessor) => { |
| 28 | + const testEvent = { event_id: 'some event' }; |
| 29 | + void eventProcessor(testEvent); |
| 30 | + expect(mockConsoleLog).toHaveBeenCalledTimes(1); |
| 31 | + expect(mockConsoleLog).toBeCalledWith(testEvent); |
| 32 | + }; |
| 33 | + |
| 34 | + debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); |
| 35 | + }); |
| 36 | + |
| 37 | + it('logs an event hint if available', () => { |
| 38 | + const debugIntegration = new Debug(); |
| 39 | + |
| 40 | + const captureEventProcessor = (eventProcessor: EventProcessor) => { |
| 41 | + const testEvent = { event_id: 'some event' }; |
| 42 | + const testEventHint = { event_id: 'some event hint' }; |
| 43 | + void eventProcessor(testEvent, testEventHint); |
| 44 | + expect(mockConsoleLog).toHaveBeenCalledTimes(2); |
| 45 | + expect(mockConsoleLog).toBeCalledWith(testEvent); |
| 46 | + expect(mockConsoleLog).toBeCalledWith(testEventHint); |
| 47 | + }; |
| 48 | + |
| 49 | + debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); |
| 50 | + }); |
| 51 | + |
| 52 | + it('logs events in stringified format when `stringify` option was set', () => { |
| 53 | + const debugIntegration = new Debug({ stringify: true }); |
| 54 | + |
| 55 | + const captureEventProcessor = (eventProcessor: EventProcessor) => { |
| 56 | + const testEvent = { event_id: 'some event' }; |
| 57 | + void eventProcessor(testEvent); |
| 58 | + expect(mockConsoleLog).toHaveBeenCalledTimes(1); |
| 59 | + expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2)); |
| 60 | + }; |
| 61 | + |
| 62 | + debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); |
| 63 | + }); |
| 64 | + |
| 65 | + it('logs event hints in stringified format when `stringify` option was set', () => { |
| 66 | + const debugIntegration = new Debug({ stringify: true }); |
| 67 | + |
| 68 | + const captureEventProcessor = (eventProcessor: EventProcessor) => { |
| 69 | + const testEvent = { event_id: 'some event' }; |
| 70 | + const testEventHint = { event_id: 'some event hint' }; |
| 71 | + void eventProcessor(testEvent, testEventHint); |
| 72 | + expect(mockConsoleLog).toHaveBeenCalledTimes(2); |
| 73 | + expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2)); |
| 74 | + }; |
| 75 | + |
| 76 | + debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); |
| 77 | + }); |
| 78 | +}); |
0 commit comments