Repository navigation
Expand file tree
/
Copy pathclient.test.ts
More file actions
306 lines (237 loc) · 12 KB
/
Copy pathclient.test.ts
File metadata and controls
306 lines (237 loc) · 12 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
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
import { Scope, SessionFlusher } from '@sentry/core';
import type { Event, EventHint } from '@sentry/types';
import * as os from 'os';
import { NodeClient } from '../src';
import { getDefaultNodeClientOptions } from './helper/node-client-options';
const PUBLIC_DSN = 'https://username@domain/123';
describe('NodeClient', () => {
let client: NodeClient;
afterEach(() => {
if ('_sessionFlusher' in client) clearInterval((client as any)._sessionFlusher._intervalId);
jest.restoreAllMocks();
});
describe('captureException', () => {
test('when autoSessionTracking is enabled, and requestHandler is not used -> requestStatus should not be set', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
client = new NodeClient(options);
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureException(new Error('test exception'), undefined, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('ok');
});
test('when autoSessionTracking is disabled -> requestStatus should not be set', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.4' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureException(new Error('test exception'), undefined, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('ok');
});
test('when autoSessionTracking is enabled + requestSession status is Crashed -> requestStatus should not be overridden', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'crashed' });
client.captureException(new Error('test exception'), undefined, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('crashed');
});
test('when autoSessionTracking is enabled + error occurs within request bounds -> requestStatus should be set to Errored', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureException(new Error('test exception'), undefined, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('errored');
});
test('when autoSessionTracking is enabled + error occurs outside of request bounds -> requestStatus should not be set to Errored', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.4' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
client.captureException(new Error('test exception'), undefined, scope);
const requestSession = scope.getRequestSession();
expect(requestSession).toEqual(undefined);
});
});
describe('captureEvent()', () => {
test('If autoSessionTracking is disabled, requestSession status should not be set', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: false, release: '1.4' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureEvent(
{ message: 'message', exception: { values: [{ type: 'exception type 1' }] } },
undefined,
scope,
);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('ok');
});
test('When captureEvent is called with an exception, requestSession status should be set to Errored', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureEvent({ message: 'message', exception: { values: [{ type: 'exception type 1' }] } }, {}, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('errored');
});
test('When captureEvent is called without an exception, requestSession status should not be set to Errored', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureEvent({ message: 'message' }, {}, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('ok');
});
test('When captureEvent is called with an exception but outside of a request, then requestStatus should not be set', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '2.2' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
client.captureEvent(
{ message: 'message', exception: { values: [{ type: 'exception type 1' }] } },
undefined,
scope,
);
expect(scope.getRequestSession()).toEqual(undefined);
});
test('When captureEvent is called with a transaction, then requestSession status should not be set', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.3' });
client = new NodeClient(options);
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
// by the`requestHandler`)
client.initSessionFlusher();
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureEvent({ message: 'message', type: 'transaction' }, undefined, scope);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('ok');
});
test('When captureEvent is called with an exception but requestHandler is not used, then requestSession status should not be set', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, autoSessionTracking: true, release: '1.3' });
client = new NodeClient(options);
const scope = new Scope();
scope.setRequestSession({ status: 'ok' });
client.captureEvent(
{ message: 'message', exception: { values: [{ type: 'exception type 1' }] } },
undefined,
scope,
);
const requestSession = scope.getRequestSession();
expect(requestSession!.status).toEqual('ok');
});
});
describe('_prepareEvent', () => {
test('adds platform to event', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
client = new NodeClient(options);
const event: Event = {};
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.platform).toEqual('node');
});
test('adds runtime context to event', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
client = new NodeClient(options);
const event: Event = {};
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.contexts?.runtime).toEqual({
name: 'node',
version: process.version,
});
});
test('adds server name to event when value passed in options', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, serverName: 'foo' });
client = new NodeClient(options);
const event: Event = {};
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.server_name).toEqual('foo');
});
test('adds server name to event when value given in env', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
client = new NodeClient(options);
process.env.SENTRY_NAME = 'foo';
const event: Event = {};
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.server_name).toEqual('foo');
delete process.env.SENTRY_NAME;
});
test('adds hostname as event server name when no value given', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN });
client = new NodeClient(options);
const event: Event = {};
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.server_name).toEqual(os.hostname());
});
test("doesn't clobber existing runtime data", () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, serverName: 'bar' });
client = new NodeClient(options);
const event: Event = { contexts: { runtime: { name: 'foo', version: '1.2.3' } } };
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.contexts?.runtime).toEqual({ name: 'foo', version: '1.2.3' });
expect(event.contexts?.runtime).not.toEqual({ name: 'node', version: process.version });
});
test("doesn't clobber existing server name", () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, serverName: 'bar' });
client = new NodeClient(options);
const event: Event = { server_name: 'foo' };
const hint: EventHint = {};
(client as any)._prepareEvent(event, hint);
expect(event.server_name).toEqual('foo');
expect(event.server_name).not.toEqual('bar');
});
});
});
describe('flush/close', () => {
test('client close function disables _sessionFlusher', async () => {
jest.useRealTimers();
const options = getDefaultNodeClientOptions({
dsn: PUBLIC_DSN,
autoSessionTracking: true,
release: '1.1',
});
const client = new NodeClient(options);
client.initSessionFlusher();
// Clearing interval is important here to ensure that the flush function later on is called by the `client.close()`
// not due to the interval running every 60s
clearInterval((client as any)._sessionFlusher._intervalId);
const sessionFlusherFlushFunc = jest.spyOn<any, any>(SessionFlusher.prototype, 'flush');
const delay = 1;
await client.close(delay);
expect((client as any)._sessionFlusher._isEnabled).toBeFalsy();
expect(sessionFlusherFlushFunc).toHaveBeenCalledTimes(1);
});
});