-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
exports.test.ts
339 lines (302 loc) · 10.4 KB
/
exports.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
import { getCurrentHub, getHubFromCarrier, Scope } from '../src';
import {
captureEvent,
captureException,
captureMessage,
configureScope,
setContext,
setExtra,
setExtras,
setTag,
setTags,
setUser,
startTransaction,
withScope,
} from '../src/exports';
export class TestClient {
public static instance?: TestClient;
public constructor(public options: Record<string, unknown>) {
TestClient.instance = this;
}
public mySecretPublicMethod(str: string): string {
return `secret: ${str}`;
}
}
export class TestClient2 {}
export function init(options: Record<string, unknown>): void {
getCurrentHub().bindClient(new TestClient(options) as any);
}
// eslint-disable-next-line no-var
declare var global: any;
describe('Top Level API', () => {
beforeEach(() => {
global.__SENTRY__ = {
hub: undefined,
};
});
describe('Capture', () => {
test('Return an event_id', () => {
const client: any = {
captureException: jest.fn(async () => Promise.resolve()),
};
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const e = new Error('test exception');
const eventId = captureException(e);
expect(eventId).toBeTruthy();
});
});
test('Exception', () => {
const client: any = {
captureException: jest.fn(async () => Promise.resolve()),
};
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const e = new Error('test exception');
captureException(e);
expect(client.captureException.mock.calls[0][0]).toBe(e);
});
});
test('Exception with explicit scope', () => {
const client: any = {
captureException: jest.fn(async () => Promise.resolve()),
};
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const e = new Error('test exception');
const captureContext = { extra: { foo: 'wat' } };
captureException(e, captureContext);
expect(client.captureException.mock.calls[0][0]).toBe(e);
expect(client.captureException.mock.calls[0][1].captureContext).toBe(captureContext);
});
});
test('Message', () => {
const client: any = { captureMessage: jest.fn(async () => Promise.resolve()) };
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const message = 'yo';
captureMessage(message);
expect(client.captureMessage.mock.calls[0][0]).toBe(message);
});
});
test('Message with explicit scope', () => {
const client: any = { captureMessage: jest.fn(async () => Promise.resolve()) };
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const message = 'yo';
const captureContext = { extra: { foo: 'wat' } };
captureMessage(message, captureContext);
expect(client.captureMessage.mock.calls[0][0]).toBe(message);
// Skip the level if explicit content is provided
expect(client.captureMessage.mock.calls[0][1]).toBe(undefined);
expect(client.captureMessage.mock.calls[0][2].captureContext).toBe(captureContext);
});
});
// NOTE: We left custom level as 2nd argument to not break the API. Should be removed and unified in v6.
// TODO: Before we release v8, check if this is still a thing
test('Message with custom level', () => {
const client: any = { captureMessage: jest.fn(async () => Promise.resolve()) };
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const message = 'yo';
const level = 'warning';
captureMessage(message, level);
expect(client.captureMessage.mock.calls[0][0]).toBe(message);
expect(client.captureMessage.mock.calls[0][1]).toBe('warning');
});
});
test('Event', () => {
const client: any = { captureEvent: jest.fn(async () => Promise.resolve()) };
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
const e = { message: 'test' };
captureEvent(e);
expect(client.captureEvent.mock.calls[0][0]).toBe(e);
});
});
});
describe('configureScope', () => {
test('User Context', () => {
const client: any = new TestClient({});
getCurrentHub().pushScope();
getCurrentHub().bindClient(client);
configureScope((scope: Scope) => {
scope.setUser({ id: '1234' });
});
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
id: '1234',
});
getCurrentHub().popScope();
});
test('Extra Context', () => {
const client: any = new TestClient({});
getCurrentHub().pushScope();
getCurrentHub().bindClient(client);
configureScope((scope: Scope) => {
scope.setExtra('id', '1234');
});
expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({
id: '1234',
});
getCurrentHub().popScope();
});
test('Tags Context', () => {
init({});
configureScope((scope: Scope) => {
scope.setTag('id', '1234');
});
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
id: '1234',
});
});
test('Fingerprint', () => {
const client: any = new TestClient({});
getCurrentHub().pushScope();
getCurrentHub().bindClient(client);
configureScope((scope: Scope) => {
scope.setFingerprint(['abcd']);
});
expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['abcd']);
});
test('Level', () => {
const client: any = new TestClient({});
const scope = getCurrentHub().pushScope();
getCurrentHub().bindClient(client);
scope.setLevel('warning');
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning');
});
});
describe('startTransaction', () => {
beforeEach(() => {
global.__SENTRY__ = {
hub: undefined,
extensions: {
startTransaction: (context: any) => ({
name: context.name,
// Spread rather than assign in case it's undefined
metadata: { ...context.metadata },
}),
},
};
});
it("sets source to `'custom'` if no source provided", () => {
const transaction = startTransaction({ name: 'dogpark' });
expect(transaction.name).toEqual('dogpark');
expect(transaction.metadata.source).toEqual('custom');
});
it('uses given `source` value', () => {
const transaction = startTransaction({ name: 'dogpark', metadata: { source: 'route' } });
expect(transaction.name).toEqual('dogpark');
expect(transaction.metadata.source).toEqual('route');
});
});
test('Clear Scope', () => {
const client: any = new TestClient({});
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(client);
expect(global.__SENTRY__.hub._stack.length).toBe(2);
configureScope((scope: Scope) => {
scope.setUser({ id: '1234' });
});
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
id: '1234',
});
configureScope((scope: Scope) => {
scope.clear();
});
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({});
});
});
test('returns undefined before binding a client', () => {
expect(getCurrentHub().getClient()).toBeUndefined();
});
test('returns the bound client', () => {
init({});
expect(getCurrentHub().getClient()).toBe(TestClient.instance);
});
test('does not throw an error when pushing different clients', () => {
init({});
expect(() => {
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(new TestClient2() as any);
});
}).not.toThrow();
});
test('does not throw an error when pushing same clients', () => {
init({});
expect(() => {
getCurrentHub().withScope(() => {
getCurrentHub().bindClient(new TestClient({}) as any);
});
}).not.toThrow();
});
test('custom carrier', () => {
const iAmSomeGlobalVarTheUserHasToManage = {
state: {},
};
const hub = getHubFromCarrier(iAmSomeGlobalVarTheUserHasToManage.state);
hub.pushScope();
hub.bindClient(new TestClient({}) as any);
hub.configureScope((scope: Scope) => {
scope.setUser({ id: '1234' });
});
expect((iAmSomeGlobalVarTheUserHasToManage.state as any).__SENTRY__.hub._stack[1].scope._user).toEqual({
id: '1234',
});
hub.popScope();
expect((iAmSomeGlobalVarTheUserHasToManage.state as any).__SENTRY__.hub._stack[1]).toBeUndefined();
});
test('withScope', () => {
withScope(scope => {
scope.setLevel('warning');
scope.setFingerprint(['1']);
withScope(scope2 => {
scope2.setLevel('info');
scope2.setFingerprint(['2']);
withScope(scope3 => {
scope3.clear();
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning');
expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']);
expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual('info');
expect(global.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']);
expect(global.__SENTRY__.hub._stack[3].scope._level).toBeUndefined();
});
expect(global.__SENTRY__.hub._stack).toHaveLength(3);
});
expect(global.__SENTRY__.hub._stack).toHaveLength(2);
});
expect(global.__SENTRY__.hub._stack).toHaveLength(1);
});
test('setExtras', () => {
init({});
setExtras({ a: 'b' });
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ a: 'b' });
});
test('setTags', () => {
init({});
setTags({ a: 'b' });
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' });
});
test('setExtra', () => {
init({});
setExtra('a', 'b');
// prettier-ignore
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({ 'a': 'b' });
});
test('setTag', () => {
init({});
setTag('a', 'b');
// prettier-ignore
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ 'a': 'b' });
});
test('setUser', () => {
init({});
setUser({ id: 'b' });
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({ id: 'b' });
});
test('setContext', () => {
init({});
setContext('test', { id: 'b' });
expect(global.__SENTRY__.hub._stack[0].scope._contexts).toEqual({ test: { id: 'b' } });
});
});