/
index.test.ts
425 lines (377 loc) · 12.9 KB
/
index.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { initAndBind, SDK_VERSION } from '@sentry/core';
import { getMainCarrier } from '@sentry/hub';
import { Integration } from '@sentry/types';
import * as domain from 'domain';
import {
addBreadcrumb,
captureEvent,
captureException,
captureMessage,
configureScope,
Event,
getCurrentHub,
init,
NodeClient,
Scope,
} from '../src';
import { NodeBackend } from '../src/backend';
jest.mock('@sentry/core', () => {
const original = jest.requireActual('@sentry/core');
return {
...original,
initAndBind: jest.fn().mockImplementation(original.initAndBind),
};
});
const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291';
// eslint-disable-next-line no-var
declare var global: any;
describe('SentryNode', () => {
beforeAll(() => {
init({ dsn });
});
beforeEach(() => {
jest.clearAllMocks();
getCurrentHub().pushScope();
});
afterEach(() => {
getCurrentHub().popScope();
});
describe('getContext() / setContext()', () => {
test('store/load extra', async () => {
configureScope((scope: Scope) => {
scope.setExtra('abc', { def: [1] });
});
expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({
abc: { def: [1] },
});
});
test('store/load tags', async () => {
configureScope((scope: Scope) => {
scope.setTag('abc', 'def');
});
expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({
abc: 'def',
});
});
test('store/load user', async () => {
configureScope((scope: Scope) => {
scope.setUser({ id: 'def' });
});
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
id: 'def',
});
});
});
describe('breadcrumbs', () => {
let s: jest.SpyInstance<void, Event[]>;
beforeEach(() => {
s = jest.spyOn(NodeBackend.prototype, 'sendEvent').mockImplementation(async () => Promise.resolve({ code: 200 }));
});
afterEach(() => {
s.mockRestore();
});
test('record auto breadcrumbs', done => {
const client = new NodeClient({
beforeSend: (event: Event) => {
// TODO: It should be 3, but we don't capture a breadcrumb
// for our own captureMessage/captureException calls yet
expect(event.breadcrumbs!).toHaveLength(2);
done();
return null;
},
dsn,
});
getCurrentHub().bindClient(client);
addBreadcrumb({ message: 'test1' });
addBreadcrumb({ message: 'test2' });
captureMessage('event');
});
});
describe('capture', () => {
let s: jest.SpyInstance<void, Event[]>;
beforeEach(() => {
s = jest.spyOn(NodeBackend.prototype, 'sendEvent').mockImplementation(async () => Promise.resolve({ code: 200 }));
});
afterEach(() => {
s.mockRestore();
});
test('capture an exception', done => {
expect.assertions(6);
getCurrentHub().bindClient(
new NodeClient({
beforeSend: (event: Event) => {
expect(event.tags).toEqual({ test: '1' });
expect(event.exception).not.toBeUndefined();
expect(event.exception!.values![0]).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!.frames![2]).not.toBeUndefined();
expect(event.exception!.values![0].value).toEqual('test');
done();
return null;
},
dsn,
}),
);
configureScope((scope: Scope) => {
scope.setTag('test', '1');
});
try {
throw new Error('test');
} catch (e) {
captureException(e);
}
});
test('capture a string exception', done => {
expect.assertions(6);
getCurrentHub().bindClient(
new NodeClient({
beforeSend: (event: Event) => {
expect(event.tags).toEqual({ test: '1' });
expect(event.exception).not.toBeUndefined();
expect(event.exception!.values![0]).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!.frames![2]).not.toBeUndefined();
expect(event.exception!.values![0].value).toEqual('test string exception');
done();
return null;
},
dsn,
}),
);
configureScope((scope: Scope) => {
scope.setTag('test', '1');
});
try {
throw 'test string exception';
} catch (e) {
captureException(e);
}
});
test('capture an exception no pre/post context', done => {
expect.assertions(10);
getCurrentHub().bindClient(
new NodeClient({
beforeSend: (event: Event) => {
expect(event.tags).toEqual({ test: '1' });
expect(event.exception).not.toBeUndefined();
expect(event.exception!.values![0]).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!.frames![2]).not.toBeUndefined();
expect(event.exception!.values![0].stacktrace!.frames![2].pre_context).toBeUndefined();
expect(event.exception!.values![0].stacktrace!.frames![2].post_context).toBeUndefined();
expect(event.exception!.values![0].type).toBe('Error');
expect(event.exception!.values![0].value).toBe('test');
expect(event.exception!.values![0].stacktrace).toBeTruthy();
done();
return null;
},
dsn,
frameContextLines: 0,
}),
);
configureScope((scope: Scope) => {
scope.setTag('test', '1');
});
try {
throw new Error('test');
} catch (e) {
captureException(e);
}
});
test('capture a message', done => {
expect.assertions(2);
getCurrentHub().bindClient(
new NodeClient({
beforeSend: (event: Event) => {
expect(event.message).toBe('test');
expect(event.exception).toBeUndefined();
done();
return null;
},
dsn,
}),
);
captureMessage('test');
});
test('capture an event', done => {
expect.assertions(2);
getCurrentHub().bindClient(
new NodeClient({
beforeSend: (event: Event) => {
expect(event.message).toBe('test event');
expect(event.exception).toBeUndefined();
done();
return null;
},
dsn,
}),
);
captureEvent({ message: 'test event' });
});
test('capture an event in a domain', done => {
const d = domain.create();
const client = new NodeClient({
beforeSend: (event: Event) => {
expect(event.message).toBe('test domain');
expect(event.exception).toBeUndefined();
done();
return null;
},
dsn,
});
d.run(() => {
getCurrentHub().bindClient(client);
expect(getCurrentHub().getClient()).toBe(client);
getCurrentHub().captureEvent({ message: 'test domain' });
});
});
test('stacktrace order', done => {
expect.assertions(1);
getCurrentHub().bindClient(
new NodeClient({
beforeSend: (event: Event) => {
expect(
event.exception!.values![0].stacktrace!.frames![
event.exception!.values![0].stacktrace!.frames!.length - 1
].function,
).toEqual('testy');
done();
return null;
},
dsn,
}),
);
try {
// @ts-ignore allow function declarations in strict mode
// eslint-disable-next-line no-inner-declarations
function testy(): void {
throw new Error('test');
}
testy();
} catch (e) {
captureException(e);
}
});
});
});
function withAutoloadedIntegrations(integrations: Integration[], callback: () => void) {
const carrier = getMainCarrier();
carrier.__SENTRY__!.integrations = integrations;
callback();
carrier.__SENTRY__!.integrations = undefined;
delete carrier.__SENTRY__!.integrations;
}
/** JSDoc */
class MockIntegration implements Integration {
public name: string;
public constructor(name: string) {
this.name = name;
}
public setupOnce(): void {
// noop
}
}
describe('SentryNode initialization', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('global.SENTRY_RELEASE is used to set release on initialization if available', () => {
global.SENTRY_RELEASE = { id: 'foobar' };
init({ dsn });
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar');
// Unsure if this is needed under jest.
global.SENTRY_RELEASE = undefined;
});
describe('SDK metadata', () => {
it('should set SDK data when Sentry.init() is called', () => {
init({ dsn });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
expect(sdkData.name).toEqual('sentry.javascript.node');
expect(sdkData.packages[0].name).toEqual('npm:@sentry/node');
expect(sdkData.packages[0].version).toEqual(SDK_VERSION);
expect(sdkData.version).toEqual(SDK_VERSION);
});
it('should set SDK data when instantiating a client directly', () => {
const client = new NodeClient({ dsn });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sdkData = (client as any)._backend._transport._api.metadata?.sdk;
expect(sdkData.name).toEqual('sentry.javascript.node');
expect(sdkData.packages[0].name).toEqual('npm:@sentry/node');
expect(sdkData.packages[0].version).toEqual(SDK_VERSION);
expect(sdkData.version).toEqual(SDK_VERSION);
});
// wrapper packages (like @sentry/serverless) set their SDK data in their `init` methods, which are
// called before the client is instantiated, and we don't want to clobber that data
it("shouldn't overwrite SDK data that's already there", () => {
init({
dsn,
// this would normally be set by the wrapper SDK in init()
_metadata: {
sdk: {
name: 'sentry.javascript.serverless',
packages: [
{
name: 'npm:@sentry/serverless',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
},
},
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sdkData = (getCurrentHub().getClient() as any)._backend._transport._api.metadata?.sdk;
expect(sdkData.name).toEqual('sentry.javascript.serverless');
expect(sdkData.packages[0].name).toEqual('npm:@sentry/serverless');
expect(sdkData.packages[0].version).toEqual(SDK_VERSION);
expect(sdkData.version).toEqual(SDK_VERSION);
});
});
describe('autoloaded integrations', () => {
it('should attach single integration to default integrations', () => {
withAutoloadedIntegrations([new MockIntegration('foo')], () => {
init({
defaultIntegrations: [new MockIntegration('bar')],
});
const integrations = (initAndBind as jest.Mock).mock.calls[0][1].defaultIntegrations;
expect(integrations.map((i: { name: string }) => i.name)).toEqual(['bar', 'foo']);
});
});
it('should attach multiple integrations to default integrations', () => {
withAutoloadedIntegrations([new MockIntegration('foo'), new MockIntegration('bar')], () => {
init({
defaultIntegrations: [new MockIntegration('baz'), new MockIntegration('qux')],
});
const integrations = (initAndBind as jest.Mock).mock.calls[0][1].defaultIntegrations;
expect(integrations.map((i: { name: string }) => i.name)).toEqual(['baz', 'qux', 'foo', 'bar']);
});
});
it('should ignore autoloaded integrations when defaultIntegrations:false', () => {
withAutoloadedIntegrations([new MockIntegration('foo')], () => {
init({
defaultIntegrations: false,
});
const integrations = (initAndBind as jest.Mock).mock.calls[0][1].defaultIntegrations;
expect(integrations).toEqual([]);
});
});
});
describe.only('autoSessionTracking', () => {
it('enables autoSessionTracking if there is a release', () => {
init({
dsn: '',
release: '3.5.7',
});
const options = (initAndBind as jest.Mock).mock.calls[0][1];
expect(options.autoSessionTracking).toBe(true);
});
it('disables autoSessionTracking if dsn is undefined', () => {
init({
release: '3.5.7',
});
const options = (initAndBind as jest.Mock).mock.calls[0][1];
expect(options.autoSessionTracking).toBe(undefined);
});
});
});