-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcaptureconsole.test.ts
382 lines (294 loc) · 13.2 KB
/
captureconsole.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* eslint-disable @typescript-eslint/unbound-method */
import * as CurrentScopes from '../../../src/currentScopes';
import * as SentryCore from '../../../src/exports';
import type { Client, ConsoleLevel, Event } from '../../../src/types-hoist';
import { captureConsoleIntegration } from '../../../src/integrations/captureconsole';
import { addConsoleInstrumentationHandler } from '../../../src/utils-hoist/instrument/console';
import { resetInstrumentationHandlers } from '../../../src/utils-hoist/instrument/handlers';
import { CONSOLE_LEVELS, originalConsoleMethods } from '../../../src/utils-hoist/logger';
import { GLOBAL_OBJ } from '../../../src/utils-hoist/worldwide';
const mockConsole: { [key in ConsoleLevel]: jest.Mock<any> } = {
debug: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
assert: jest.fn(),
info: jest.fn(),
trace: jest.fn(),
};
describe('CaptureConsole setup', () => {
// Ensure we've initialized the instrumentation so we can get the original one
addConsoleInstrumentationHandler(() => {});
const _originalConsoleMethods = Object.assign({}, originalConsoleMethods);
let mockClient: Client;
const mockScope = {
setExtra: jest.fn(),
addEventProcessor: jest.fn(),
};
const captureMessage = jest.fn();
const captureException = jest.fn();
const withScope = jest.fn(callback => {
return callback(mockScope);
});
beforeEach(() => {
mockClient = {} as Client;
jest.spyOn(SentryCore, 'captureMessage').mockImplementation(captureMessage);
jest.spyOn(SentryCore, 'captureException').mockImplementation(captureException);
jest.spyOn(CurrentScopes, 'getClient').mockImplementation(() => mockClient);
jest.spyOn(CurrentScopes, 'withScope').mockImplementation(withScope);
CONSOLE_LEVELS.forEach(key => {
originalConsoleMethods[key] = mockConsole[key];
});
});
afterEach(() => {
jest.clearAllMocks();
CONSOLE_LEVELS.forEach(key => {
originalConsoleMethods[key] = _originalConsoleMethods[key];
});
resetInstrumentationHandlers();
});
describe('monkeypatching', () => {
it('should patch user-configured console levels', () => {
const captureConsole = captureConsoleIntegration({ levels: ['log', 'warn'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.error('msg 1');
GLOBAL_OBJ.console.log('msg 2');
GLOBAL_OBJ.console.warn('msg 3');
expect(captureMessage).toHaveBeenCalledTimes(2);
});
it('should fall back to default console levels if none are provided', () => {
const captureConsole = captureConsoleIntegration();
captureConsole.setup?.(mockClient);
// Assert has a special handling
(['debug', 'info', 'warn', 'error', 'log', 'trace'] as const).forEach(key => {
GLOBAL_OBJ.console[key]('msg');
});
GLOBAL_OBJ.console.assert(false);
expect(captureMessage).toHaveBeenCalledTimes(7);
});
it('should not wrap any functions with an empty levels option', () => {
const captureConsole = captureConsoleIntegration({ levels: [] });
captureConsole.setup?.(mockClient);
CONSOLE_LEVELS.forEach(key => {
GLOBAL_OBJ.console[key]('msg');
});
expect(captureMessage).toHaveBeenCalledTimes(0);
});
});
it('setup should fail gracefully when console is not available', () => {
const consoleRef = GLOBAL_OBJ.console;
// @ts-expect-error remove console
delete GLOBAL_OBJ.console;
const captureConsole = captureConsoleIntegration();
expect(() => {
captureConsole.setup?.(mockClient);
}).not.toThrow();
// reinstate initial console
GLOBAL_OBJ.console = consoleRef;
});
it('should send empty arguments as extra data', () => {
const captureConsole = captureConsoleIntegration({ levels: ['log'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.log();
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureMessage).toHaveBeenCalledWith('', { extra: { arguments: [] }, level: 'log' });
});
it('should add an event processor that sets the `logger` field of events', () => {
const captureConsole = captureConsoleIntegration({ levels: ['log'] });
captureConsole.setup?.(mockClient);
// call a wrapped function
GLOBAL_OBJ.console.log('some message');
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {};
addedEventProcessor(someEvent);
expect(someEvent.logger).toBe('console');
});
it('should capture message on a failed assertion', () => {
const captureConsole = captureConsoleIntegration({ levels: ['assert'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.assert(1 + 1 === 3);
expect(mockScope.setExtra).toHaveBeenLastCalledWith('arguments', []);
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureMessage).toHaveBeenCalledWith('Assertion failed: console.assert', {
extra: { arguments: [false] },
level: 'log',
});
});
it('should capture correct message on a failed assertion with message', () => {
const captureConsole = captureConsoleIntegration({ levels: ['assert'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.assert(1 + 1 === 3, 'expression is false');
expect(mockScope.setExtra).toHaveBeenLastCalledWith('arguments', ['expression is false']);
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureMessage).toHaveBeenCalledWith('Assertion failed: expression is false', {
extra: { arguments: [false, 'expression is false'] },
level: 'log',
});
});
it('should not capture message on a successful assertion', () => {
const captureConsole = captureConsoleIntegration({ levels: ['assert'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.assert(1 + 1 === 2);
expect(captureMessage).toHaveBeenCalledTimes(0);
});
it('should capture exception when console logs an error object with level set to "error"', () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'] });
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);
expect(captureException).toHaveBeenCalledTimes(1);
expect(captureException).toHaveBeenCalledWith(someError, {
extra: { arguments: [someError] },
level: 'error',
});
});
it('should capture exception on `console.error` when no levels are provided in constructor', () => {
const captureConsole = captureConsoleIntegration();
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);
expect(captureException).toHaveBeenCalledTimes(1);
expect(captureException).toHaveBeenCalledWith(someError, {
extra: { arguments: [someError] },
level: 'error',
});
});
it('should capture exception when console logs an error object in any of the args when level set to "error"', () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'] });
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error('Something went wrong', someError);
expect(captureException).toHaveBeenCalledTimes(1);
expect(captureException).toHaveBeenCalledWith(someError, {
extra: { arguments: ['Something went wrong', someError] },
level: 'error',
});
});
it('should capture message on `console.log` when no levels are provided in constructor', () => {
const captureConsole = captureConsoleIntegration();
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.error('some message');
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureMessage).toHaveBeenCalledWith('some message', {
extra: { arguments: ['some message'] },
level: 'error',
});
});
it('should capture message when console logs a non-error object with level set to "error"', () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.error('some non-error message');
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureMessage).toHaveBeenCalledWith('some non-error message', {
extra: { arguments: ['some non-error message'] },
level: 'error',
});
expect(captureException).not.toHaveBeenCalled();
});
it('should capture a message for non-error log levels', () => {
const captureConsole = captureConsoleIntegration({ levels: ['info'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.info('some message');
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureMessage).toHaveBeenCalledWith('some message', {
extra: { arguments: ['some message'] },
level: 'info',
});
});
it('should call the original console function when console members are called', () => {
// Mock console log to test if it was called
const originalConsoleLog = GLOBAL_OBJ.console.log;
const mockConsoleLog = jest.fn();
GLOBAL_OBJ.console.log = mockConsoleLog;
const captureConsole = captureConsoleIntegration({ levels: ['log'] });
captureConsole.setup?.(mockClient);
GLOBAL_OBJ.console.log('some message 1', 'some message 2');
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toHaveBeenCalledWith('some message 1', 'some message 2');
// Reset console log
GLOBAL_OBJ.console.log = originalConsoleLog;
});
it('should not wrap any levels that are not members of console', () => {
const captureConsole = captureConsoleIntegration({ levels: ['log', 'someNonExistingLevel', 'error'] });
captureConsole.setup?.(mockClient);
// The provided level should not be created
expect((GLOBAL_OBJ.console as any)['someNonExistingLevel']).toBeUndefined();
});
it('should wrap the console when the client does not have a registered captureconsole integration, but not capture any messages', () => {
const captureConsole = captureConsoleIntegration({ levels: ['log', 'error'] });
// when `setup` is not called on the current client, it will not trigger
captureConsole.setup?.({} as Client);
// Should not capture messages
GLOBAL_OBJ.console.log('some message');
expect(captureMessage).not.toHaveBeenCalledWith();
});
it("should not crash when the original console methods don't exist at time of invocation", () => {
originalConsoleMethods.log = undefined;
const captureConsole = captureConsoleIntegration({ levels: ['log'] });
captureConsole.setup?.(mockClient);
expect(() => {
GLOBAL_OBJ.console.log('some message');
}).not.toThrow();
});
describe('exception mechanism', () => {
// TODO (v9): Flip this below after adjusting the default value for `handled` in the integration
it("marks captured exception's mechanism as unhandled by default", () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'] });
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);
expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: false,
type: 'console',
});
});
it("marks captured exception's mechanism as handled if set in the options", () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'], handled: true });
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);
expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: true,
type: 'console',
});
});
it("marks captured exception's mechanism as unhandled if set in the options", () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'], handled: false });
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);
expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: false,
type: 'console',
});
});
});
});