Skip to content

Commit 2e01497

Browse files
Luca Forstnerlforst
Luca Forstner
authored andcommitted
test(integrations): Add unit tests for Debug
1 parent 79a109c commit 2e01497

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

packages/integrations/src/debug.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
22
import { consoleSandbox } from '@sentry/utils';
33

4-
/** JSDoc */
54
interface DebugOptions {
5+
/** Controls whether console output created by this integration should be stringified. Default: `false` */
66
stringify?: boolean;
7+
/** Controls whether a debugger should be launched before an event is sent. Default: `false` */
78
debugger?: boolean;
89
}
910

10-
/** JSDoc */
11+
/**
12+
* Integration to debug sent Sentry events.
13+
* This integration should not be used in production
14+
*/
1115
export class Debug implements Integration {
1216
/**
1317
* @inheritDoc
@@ -19,12 +23,8 @@ export class Debug implements Integration {
1923
*/
2024
public name: string = Debug.id;
2125

22-
/** JSDoc */
2326
private readonly _options: DebugOptions;
2427

25-
/**
26-
* @inheritDoc
27-
*/
2828
public constructor(options?: DebugOptions) {
2929
this._options = {
3030
debugger: false,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)