From 1f4d4270024717cbfeb35aa719587383c16b492a Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 13 Sep 2023 11:21:44 +0200 Subject: [PATCH] ref: Do not use event processor for `Debug` integration Instead we can ensure this is called after all processing finished via a hook. --- packages/integrations/src/debug.ts | 22 +++---- packages/integrations/test/debug.test.ts | 83 ++++++++++++++---------- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/packages/integrations/src/debug.ts b/packages/integrations/src/debug.ts index 1deb1f8dec17..1c2459636510 100644 --- a/packages/integrations/src/debug.ts +++ b/packages/integrations/src/debug.ts @@ -38,32 +38,32 @@ export class Debug implements Integration { /** * @inheritDoc */ - public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { - addGlobalEventProcessor((event: Event, hint: EventHint) => { - const self = getCurrentHub().getIntegration(Debug); - if (self) { - if (self._options.debugger) { + public setupOnce(_addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void { + const client = getCurrentHub().getClient(); + + if (client && client.on) { + client.on('beforeSendEvent', (event: Event, hint?: EventHint) => { + if (this._options.debugger) { // eslint-disable-next-line no-debugger debugger; } /* eslint-disable no-console */ consoleSandbox(() => { - if (self._options.stringify) { + if (this._options.stringify) { console.log(JSON.stringify(event, null, 2)); - if (Object.keys(hint).length) { + if (hint && Object.keys(hint).length) { console.log(JSON.stringify(hint, null, 2)); } } else { console.log(event); - if (Object.keys(hint).length) { + if (hint && Object.keys(hint).length) { console.log(hint); } } }); /* eslint-enable no-console */ - } - return event; - }); + }); + } } } diff --git a/packages/integrations/test/debug.test.ts b/packages/integrations/test/debug.test.ts index 268c03dcc4d5..953fcdb0258e 100644 --- a/packages/integrations/test/debug.test.ts +++ b/packages/integrations/test/debug.test.ts @@ -1,10 +1,33 @@ -import type { EventProcessor, Integration } from '@sentry/types'; +import type { Client, Event, EventHint, Hub, Integration } from '@sentry/types'; import { Debug } from '../src/debug'; -const mockGetCurrentHub = (getIntegrationResult: Integration) => ({ - getIntegration: jest.fn(() => getIntegrationResult), -}); +function testEventLogged(integration: Integration, testEvent?: Event, testEventHint?: EventHint) { + const callbacks: ((event: Event, hint?: EventHint) => void)[] = []; + + const client: Client = { + on(hook: string, callback: (event: Event, hint?: EventHint) => void) { + expect(hook).toEqual('beforeSendEvent'); + callbacks.push(callback); + }, + } as Client; + + function getCurrentHub() { + return { + getClient: jest.fn(() => { + return client; + }), + } as unknown as Hub; + } + + integration.setupOnce(() => {}, getCurrentHub); + + expect(callbacks.length).toEqual(1); + + if (testEvent) { + callbacks[0](testEvent, testEventHint); + } +} // Replace console log with a mock so we can check for invocations const mockConsoleLog = jest.fn(); @@ -24,56 +47,46 @@ describe('Debug integration setup should register an event processor that', () = it('logs an event', () => { const debugIntegration = new Debug(); + const testEvent = { event_id: 'some event' }; - const captureEventProcessor = (eventProcessor: EventProcessor) => { - const testEvent = { event_id: 'some event' }; - void eventProcessor(testEvent, {}); - expect(mockConsoleLog).toHaveBeenCalledTimes(1); - expect(mockConsoleLog).toBeCalledWith(testEvent); - }; + testEventLogged(debugIntegration, testEvent); - debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); + expect(mockConsoleLog).toHaveBeenCalledTimes(1); + expect(mockConsoleLog).toBeCalledWith(testEvent); }); it('logs an event hint if available', () => { const debugIntegration = new Debug(); - const captureEventProcessor = (eventProcessor: EventProcessor) => { - const testEvent = { event_id: 'some event' }; - const testEventHint = { event_id: 'some event hint' }; - void eventProcessor(testEvent, testEventHint); - expect(mockConsoleLog).toHaveBeenCalledTimes(2); - expect(mockConsoleLog).toBeCalledWith(testEvent); - expect(mockConsoleLog).toBeCalledWith(testEventHint); - }; + const testEvent = { event_id: 'some event' }; + const testEventHint = { event_id: 'some event hint' }; - debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); + testEventLogged(debugIntegration, testEvent, testEventHint); + + expect(mockConsoleLog).toHaveBeenCalledTimes(2); + expect(mockConsoleLog).toBeCalledWith(testEvent); + expect(mockConsoleLog).toBeCalledWith(testEventHint); }); it('logs events in stringified format when `stringify` option was set', () => { const debugIntegration = new Debug({ stringify: true }); + const testEvent = { event_id: 'some event' }; - const captureEventProcessor = (eventProcessor: EventProcessor) => { - const testEvent = { event_id: 'some event' }; - void eventProcessor(testEvent, {}); - expect(mockConsoleLog).toHaveBeenCalledTimes(1); - expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2)); - }; + testEventLogged(debugIntegration, testEvent); - debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); + expect(mockConsoleLog).toHaveBeenCalledTimes(1); + expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2)); }); it('logs event hints in stringified format when `stringify` option was set', () => { const debugIntegration = new Debug({ stringify: true }); - const captureEventProcessor = (eventProcessor: EventProcessor) => { - const testEvent = { event_id: 'some event' }; - const testEventHint = { event_id: 'some event hint' }; - void eventProcessor(testEvent, testEventHint); - expect(mockConsoleLog).toHaveBeenCalledTimes(2); - expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2)); - }; + const testEvent = { event_id: 'some event' }; + const testEventHint = { event_id: 'some event hint' }; + + testEventLogged(debugIntegration, testEvent, testEventHint); - debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any); + expect(mockConsoleLog).toHaveBeenCalledTimes(2); + expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2)); }); });