Repository navigation
Expand file tree
/
Copy pathdebug.test.ts
More file actions
94 lines (69 loc) · 2.98 KB
/
Copy pathdebug.test.ts
File metadata and controls
94 lines (69 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import type { Client, Event, EventHint, Integration } from '@sentry/types';
import type { debugIntegration } from '../src/debug';
import { Debug } from '../src/debug';
interface IntegrationWithSetup extends Integration {
setup: (client: Client) => void;
}
function getIntegration(...args: Parameters<typeof debugIntegration>) {
// eslint-disable-next-line deprecation/deprecation
return new Debug(...args);
}
function testEventLogged(integration: IntegrationWithSetup, 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;
integration.setup(client);
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();
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalConsoleLog = global.console.log;
global.console.log = mockConsoleLog;
describe('Debug integration setup should register an event processor that', () => {
afterAll(() => {
// Reset mocked console log to original one
global.console.log = originalConsoleLog;
});
afterEach(() => {
jest.clearAllMocks();
});
it('logs an event', () => {
const debugIntegration = getIntegration();
const testEvent = { event_id: 'some event' };
testEventLogged(debugIntegration, testEvent);
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toBeCalledWith(testEvent);
});
it('logs an event hint if available', () => {
const debugIntegration = getIntegration();
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };
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 = getIntegration({ stringify: true });
const testEvent = { event_id: 'some event' };
testEventLogged(debugIntegration, testEvent);
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 = getIntegration({ stringify: true });
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };
testEventLogged(debugIntegration, testEvent, testEventHint);
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2));
});
});