-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdebug.test.ts
93 lines (70 loc) · 2.93 KB
/
debug.test.ts
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
import type { Client, Event, EventHint } from '../../../src/types-hoist';
import { debugIntegration } from '../../../src/integrations/debug';
function testEventLogged(
// eslint-disable-next-line deprecation/deprecation
integration: ReturnType<typeof debugIntegration>,
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', () => {
// eslint-disable-next-line deprecation/deprecation
const debug = debugIntegration();
const testEvent = { event_id: 'some event' };
testEventLogged(debug, testEvent);
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toBeCalledWith(testEvent);
});
it('logs an event hint if available', () => {
// eslint-disable-next-line deprecation/deprecation
const debug = debugIntegration();
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };
testEventLogged(debug, 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', () => {
// eslint-disable-next-line deprecation/deprecation
const debug = debugIntegration({ stringify: true });
const testEvent = { event_id: 'some event' };
testEventLogged(debug, 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', () => {
// eslint-disable-next-line deprecation/deprecation
const debug = debugIntegration({ stringify: true });
const testEvent = { event_id: 'some event' };
const testEventHint = { event_id: 'some event hint' };
testEventLogged(debug, testEvent, testEventHint);
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toBeCalledWith(JSON.stringify(testEventHint, null, 2));
});
});