Skip to content

Commit c12ff03

Browse files
committed
fix type errors in hub tests
1 parent a1a8bd6 commit c12ff03

File tree

3 files changed

+86
-32
lines changed

3 files changed

+86
-32
lines changed

packages/hub/test/global.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { getGlobalObject } from '@sentry/utils';
2+
13
import { getCurrentHub, getHubFromCarrier, Hub } from '../src';
24

5+
const global = getGlobalObject();
6+
37
describe('global', () => {
48
test('getGlobalHub', () => {
59
expect(getCurrentHub()).toBeTruthy();

packages/hub/test/hub.test.ts

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Event } from '@sentry/types';
1+
/* eslint-disable @typescript-eslint/unbound-method */
2+
import { Client, Event } from '@sentry/types';
23

34
import { getCurrentHub, Hub, Scope } from '../src';
45

@@ -15,7 +16,18 @@ function makeClient() {
1516
getIntegration: jest.fn(),
1617
setupIntegrations: jest.fn(),
1718
captureMessage: jest.fn(),
18-
};
19+
} as unknown as Client;
20+
}
21+
22+
/**
23+
* Return an array containing the arguments passed to the given mocked or spied-upon function.
24+
*
25+
* By default, the args passed to the first call of the function are returned, but it is also possible to retrieve the
26+
* nth call by passing `callIndex`. If the function wasn't called, an error message is returned instead.
27+
*/
28+
function getPassedArgs(mock: (...args: any[]) => any, callIndex: number = 0): any[] {
29+
const asMock = mock as jest.MockedFunction<(...args: any[]) => any>;
30+
return asMock.mock.calls[callIndex] || ["Error: Function wasn't called."];
1931
}
2032

2133
describe('Hub', () => {
@@ -102,7 +114,7 @@ describe('Hub', () => {
102114
});
103115
});
104116

105-
test('inherit processors', () => {
117+
test('inherit processors', async () => {
106118
expect.assertions(1);
107119
const event: Event = {
108120
extra: { b: 3 },
@@ -210,67 +222,90 @@ describe('Hub', () => {
210222
test('simple', () => {
211223
const testClient = makeClient();
212224
const hub = new Hub(testClient);
225+
213226
hub.captureException('a');
214-
expect(testClient.captureException).toHaveBeenCalled();
215-
expect(testClient.captureException.mock.calls[0][0]).toBe('a');
227+
const args = getPassedArgs(testClient.captureException);
228+
229+
expect(args[0]).toBe('a');
216230
});
217231

218232
test('should set event_id in hint', () => {
219233
const testClient = makeClient();
220234
const hub = new Hub(testClient);
235+
221236
hub.captureException('a');
222-
expect(testClient.captureException.mock.calls[0][1].event_id).toBeTruthy();
237+
const args = getPassedArgs(testClient.captureException);
238+
239+
expect(args[1].event_id).toBeTruthy();
223240
});
224241

225242
test('should keep event_id from hint', () => {
226243
const testClient = makeClient();
227244
const hub = new Hub(testClient);
228245
const id = Math.random().toString();
246+
229247
hub.captureException('a', { event_id: id });
230-
expect(testClient.captureException.mock.calls[0][1].event_id).toBe(id);
248+
const args = getPassedArgs(testClient.captureException);
249+
250+
expect(args[1].event_id).toBe(id);
231251
});
232252

233253
test('should generate hint if not provided in the call', () => {
234254
const testClient = makeClient();
235255
const hub = new Hub(testClient);
236256
const ex = new Error('foo');
257+
237258
hub.captureException(ex);
238-
expect(testClient.captureException.mock.calls[0][1].originalException).toBe(ex);
239-
expect(testClient.captureException.mock.calls[0][1].syntheticException).toBeInstanceOf(Error);
240-
expect(testClient.captureException.mock.calls[0][1].syntheticException.message).toBe('Sentry syntheticException');
259+
const args = getPassedArgs(testClient.captureException);
260+
261+
expect(args[1].originalException).toBe(ex);
262+
expect(args[1].syntheticException).toBeInstanceOf(Error);
263+
expect(args[1].syntheticException.message).toBe('Sentry syntheticException');
241264
});
242265
});
243266

244267
describe('captureMessage', () => {
245268
test('simple', () => {
246269
const testClient = makeClient();
247270
const hub = new Hub(testClient);
271+
248272
hub.captureMessage('a');
249-
expect(testClient.captureMessage.mock.calls[0][0]).toBe('a');
273+
const args = getPassedArgs(testClient.captureMessage);
274+
275+
expect(args[0]).toBe('a');
250276
});
251277

252278
test('should set event_id in hint', () => {
253279
const testClient = makeClient();
254280
const hub = new Hub(testClient);
281+
255282
hub.captureMessage('a');
256-
expect(testClient.captureMessage.mock.calls[0][2].event_id).toBeTruthy();
283+
const args = getPassedArgs(testClient.captureMessage);
284+
285+
expect(args[2].event_id).toBeTruthy();
257286
});
258287

259288
test('should keep event_id from hint', () => {
260289
const testClient = makeClient();
261290
const hub = new Hub(testClient);
262291
const id = Math.random().toString();
292+
263293
hub.captureMessage('a', undefined, { event_id: id });
264-
expect(testClient.captureMessage.mock.calls[0][2].event_id).toBe(id);
294+
const args = getPassedArgs(testClient.captureMessage);
295+
296+
expect(args[2].event_id).toBe(id);
265297
});
266298

267299
test('should generate hint if not provided in the call', () => {
268300
const testClient = makeClient();
269301
const hub = new Hub(testClient);
302+
270303
hub.captureMessage('foo');
271-
expect(testClient.captureMessage.mock.calls[0][2].originalException).toBe('foo');
272-
expect(testClient.captureMessage.mock.calls[0][2].syntheticException).toBeInstanceOf(Error);
273-
expect(testClient.captureMessage.mock.calls[0][2].syntheticException.message).toBe('foo');
304+
const args = getPassedArgs(testClient.captureMessage);
305+
306+
expect(args[2].originalException).toBe('foo');
307+
expect(args[2].syntheticException).toBeInstanceOf(Error);
308+
expect(args[2].syntheticException.message).toBe('foo');
274309
});
275310
});
276311

@@ -281,8 +316,11 @@ describe('Hub', () => {
281316
};
282317
const testClient = makeClient();
283318
const hub = new Hub(testClient);
319+
284320
hub.captureEvent(event);
285-
expect(testClient.captureEvent.mock.calls[0][0]).toBe(event);
321+
const args = getPassedArgs(testClient.captureEvent);
322+
323+
expect(args[0]).toBe(event);
286324
});
287325

288326
test('should set event_id in hint', () => {
@@ -291,8 +329,11 @@ describe('Hub', () => {
291329
};
292330
const testClient = makeClient();
293331
const hub = new Hub(testClient);
332+
294333
hub.captureEvent(event);
295-
expect(testClient.captureEvent.mock.calls[0][1].event_id).toBeTruthy();
334+
const args = getPassedArgs(testClient.captureEvent);
335+
336+
expect(args[1].event_id).toBeTruthy();
296337
});
297338

298339
test('should keep event_id from hint', () => {
@@ -302,8 +343,11 @@ describe('Hub', () => {
302343
const testClient = makeClient();
303344
const hub = new Hub(testClient);
304345
const id = Math.random().toString();
346+
305347
hub.captureEvent(event, { event_id: id });
306-
expect(testClient.captureEvent.mock.calls[0][1].event_id).toBe(id);
348+
const args = getPassedArgs(testClient.captureEvent);
349+
350+
expect(args[1].event_id).toBe(id);
307351
});
308352

309353
test('sets lastEventId', () => {
@@ -312,8 +356,11 @@ describe('Hub', () => {
312356
};
313357
const testClient = makeClient();
314358
const hub = new Hub(testClient);
359+
315360
hub.captureEvent(event);
316-
expect(testClient.captureEvent.mock.calls[0][1].event_id).toEqual(hub.lastEventId());
361+
const args = getPassedArgs(testClient.captureEvent);
362+
363+
expect(args[1].event_id).toEqual(hub.lastEventId());
317364
});
318365

319366
test('transactions do not set lastEventId', () => {
@@ -323,8 +370,11 @@ describe('Hub', () => {
323370
};
324371
const testClient = makeClient();
325372
const hub = new Hub(testClient);
373+
326374
hub.captureEvent(event);
327-
expect(testClient.captureEvent.mock.calls[0][1].event_id).not.toEqual(hub.lastEventId());
375+
const args = getPassedArgs(testClient.captureEvent);
376+
377+
expect(args[1].event_id).not.toEqual(hub.lastEventId());
328378
});
329379
});
330380

packages/hub/test/scope.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, EventHint } from '@sentry/types';
1+
import { Event, EventHint, RequestSessionStatus } from '@sentry/types';
22
import { getGlobalObject } from '@sentry/utils';
33

44
import { addGlobalEventProcessor, Scope } from '../src';
@@ -194,7 +194,7 @@ describe('Scope', () => {
194194
});
195195

196196
describe('applyToEvent', () => {
197-
test('basic usage', () => {
197+
test('basic usage', async () => {
198198
expect.assertions(9);
199199

200200
const scope = new Scope();
@@ -222,7 +222,7 @@ describe('Scope', () => {
222222
});
223223
});
224224

225-
test('merge with existing event data', () => {
225+
test('merge with existing event data', async () => {
226226
expect.assertions(8);
227227
const scope = new Scope();
228228
scope.setExtra('a', 2);
@@ -291,7 +291,7 @@ describe('Scope', () => {
291291
});
292292
});
293293

294-
test('scope level should have priority over event level', () => {
294+
test('scope level should have priority over event level', async () => {
295295
expect.assertions(1);
296296
const scope = new Scope();
297297
scope.setLevel('warning');
@@ -302,7 +302,7 @@ describe('Scope', () => {
302302
});
303303
});
304304

305-
test('scope transaction should have priority over event transaction', () => {
305+
test('scope transaction should have priority over event transaction', async () => {
306306
expect.assertions(1);
307307
const scope = new Scope();
308308
scope.setTransactionName('/abc');
@@ -511,10 +511,10 @@ describe('Scope', () => {
511511
contexts: { bar: { id: '3' }, baz: { id: '4' } },
512512
extra: { bar: '3', baz: '4' },
513513
fingerprint: ['bar'],
514-
level: 'warning',
514+
level: 'warning' as const,
515515
tags: { bar: '3', baz: '4' },
516516
user: { id: '42' },
517-
requestSession: { status: 'errored' },
517+
requestSession: { status: 'errored' as RequestSessionStatus },
518518
};
519519
const updatedScope = scope.update(localAttributes) as any;
520520

@@ -541,7 +541,7 @@ describe('Scope', () => {
541541
});
542542

543543
describe('addEventProcessor', () => {
544-
test('should allow for basic event manipulation', () => {
544+
test('should allow for basic event manipulation', async () => {
545545
expect.assertions(3);
546546
const event: Event = {
547547
extra: { b: 3 },
@@ -566,7 +566,7 @@ describe('Scope', () => {
566566
});
567567
});
568568

569-
test('should work alongside global event processors', () => {
569+
test('should work alongside global event processors', async () => {
570570
expect.assertions(3);
571571
const event: Event = {
572572
extra: { b: 3 },
@@ -667,7 +667,7 @@ describe('Scope', () => {
667667
});
668668
});
669669

670-
test('should drop an event when any of processors return null', () => {
670+
test('should drop an event when any of processors return null', async () => {
671671
expect.assertions(1);
672672
const event: Event = {
673673
extra: { b: 3 },
@@ -680,7 +680,7 @@ describe('Scope', () => {
680680
});
681681
});
682682

683-
test('should have an access to the EventHint', () => {
683+
test('should have an access to the EventHint', async () => {
684684
expect.assertions(3);
685685
const event: Event = {
686686
extra: { b: 3 },

0 commit comments

Comments
 (0)