Skip to content

Commit 35b50d6

Browse files
mydeabillyvg
authored andcommitted
ref(integrations): Do not use event processor for Debug integration (#9014)
Instead we can ensure this is called after all processing finished via a hook. This does not actually process an event, so this is much cleaner.
1 parent 338ae3d commit 35b50d6

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

packages/integrations/src/debug.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,32 @@ export class Debug implements Integration {
3838
/**
3939
* @inheritDoc
4040
*/
41-
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
42-
addGlobalEventProcessor((event: Event, hint: EventHint) => {
43-
const self = getCurrentHub().getIntegration(Debug);
44-
if (self) {
45-
if (self._options.debugger) {
41+
public setupOnce(_addGlobalEventProcessor: (eventProcessor: EventProcessor) => void, getCurrentHub: () => Hub): void {
42+
const client = getCurrentHub().getClient();
43+
44+
if (client && client.on) {
45+
client.on('beforeSendEvent', (event: Event, hint?: EventHint) => {
46+
if (this._options.debugger) {
4647
// eslint-disable-next-line no-debugger
4748
debugger;
4849
}
4950

5051
/* eslint-disable no-console */
5152
consoleSandbox(() => {
52-
if (self._options.stringify) {
53+
if (this._options.stringify) {
5354
console.log(JSON.stringify(event, null, 2));
54-
if (Object.keys(hint).length) {
55+
if (hint && Object.keys(hint).length) {
5556
console.log(JSON.stringify(hint, null, 2));
5657
}
5758
} else {
5859
console.log(event);
59-
if (Object.keys(hint).length) {
60+
if (hint && Object.keys(hint).length) {
6061
console.log(hint);
6162
}
6263
}
6364
});
6465
/* eslint-enable no-console */
65-
}
66-
return event;
67-
});
66+
});
67+
}
6868
}
6969
}
Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1-
import type { EventProcessor, Integration } from '@sentry/types';
1+
import type { Client, Event, EventHint, Hub, Integration } from '@sentry/types';
22

33
import { Debug } from '../src/debug';
44

5-
const mockGetCurrentHub = (getIntegrationResult: Integration) => ({
6-
getIntegration: jest.fn(() => getIntegrationResult),
7-
});
5+
function testEventLogged(integration: Integration, testEvent?: Event, testEventHint?: EventHint) {
6+
const callbacks: ((event: Event, hint?: EventHint) => void)[] = [];
7+
8+
const client: Client = {
9+
on(hook: string, callback: (event: Event, hint?: EventHint) => void) {
10+
expect(hook).toEqual('beforeSendEvent');
11+
callbacks.push(callback);
12+
},
13+
} as Client;
14+
15+
function getCurrentHub() {
16+
return {
17+
getClient: jest.fn(() => {
18+
return client;
19+
}),
20+
} as unknown as Hub;
21+
}
22+
23+
integration.setupOnce(() => {}, getCurrentHub);
24+
25+
expect(callbacks.length).toEqual(1);
26+
27+
if (testEvent) {
28+
callbacks[0](testEvent, testEventHint);
29+
}
30+
}
831

932
// Replace console log with a mock so we can check for invocations
1033
const mockConsoleLog = jest.fn();
@@ -24,56 +47,46 @@ describe('Debug integration setup should register an event processor that', () =
2447

2548
it('logs an event', () => {
2649
const debugIntegration = new Debug();
50+
const testEvent = { event_id: 'some event' };
2751

28-
const captureEventProcessor = (eventProcessor: EventProcessor) => {
29-
const testEvent = { event_id: 'some event' };
30-
void eventProcessor(testEvent, {});
31-
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
32-
expect(mockConsoleLog).toBeCalledWith(testEvent);
33-
};
52+
testEventLogged(debugIntegration, testEvent);
3453

35-
debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
54+
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
55+
expect(mockConsoleLog).toBeCalledWith(testEvent);
3656
});
3757

3858
it('logs an event hint if available', () => {
3959
const debugIntegration = new Debug();
4060

41-
const captureEventProcessor = (eventProcessor: EventProcessor) => {
42-
const testEvent = { event_id: 'some event' };
43-
const testEventHint = { event_id: 'some event hint' };
44-
void eventProcessor(testEvent, testEventHint);
45-
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
46-
expect(mockConsoleLog).toBeCalledWith(testEvent);
47-
expect(mockConsoleLog).toBeCalledWith(testEventHint);
48-
};
61+
const testEvent = { event_id: 'some event' };
62+
const testEventHint = { event_id: 'some event hint' };
4963

50-
debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
64+
testEventLogged(debugIntegration, testEvent, testEventHint);
65+
66+
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
67+
expect(mockConsoleLog).toBeCalledWith(testEvent);
68+
expect(mockConsoleLog).toBeCalledWith(testEventHint);
5169
});
5270

5371
it('logs events in stringified format when `stringify` option was set', () => {
5472
const debugIntegration = new Debug({ stringify: true });
73+
const testEvent = { event_id: 'some event' };
5574

56-
const captureEventProcessor = (eventProcessor: EventProcessor) => {
57-
const testEvent = { event_id: 'some event' };
58-
void eventProcessor(testEvent, {});
59-
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
60-
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2));
61-
};
75+
testEventLogged(debugIntegration, testEvent);
6276

63-
debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
77+
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
78+
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEvent, null, 2));
6479
});
6580

6681
it('logs event hints in stringified format when `stringify` option was set', () => {
6782
const debugIntegration = new Debug({ stringify: true });
6883

69-
const captureEventProcessor = (eventProcessor: EventProcessor) => {
70-
const testEvent = { event_id: 'some event' };
71-
const testEventHint = { event_id: 'some event hint' };
72-
void eventProcessor(testEvent, testEventHint);
73-
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
74-
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2));
75-
};
84+
const testEvent = { event_id: 'some event' };
85+
const testEventHint = { event_id: 'some event hint' };
86+
87+
testEventLogged(debugIntegration, testEvent, testEventHint);
7688

77-
debugIntegration.setupOnce(captureEventProcessor, () => mockGetCurrentHub(debugIntegration) as any);
89+
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
90+
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2));
7891
});
7992
});

0 commit comments

Comments
 (0)