Skip to content

Commit 197c0be

Browse files
authored
test(hub): improve hub test cases (#4398)
Avoided the need to spy on the internal method called and tested the behavior as thru the public API only.
1 parent 8c29ecf commit 197c0be

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

packages/hub/test/hub.test.ts

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@ import { getCurrentHub, Hub, Scope } from '../src';
44

55
const clientFn: any = jest.fn();
66

7+
function makeClient() {
8+
return {
9+
getOptions: jest.fn(),
10+
captureEvent: jest.fn(),
11+
captureException: jest.fn(),
12+
close: jest.fn(),
13+
flush: jest.fn(),
14+
getDsn: jest.fn(),
15+
getIntegration: jest.fn(),
16+
setupIntegrations: jest.fn(),
17+
captureMessage: jest.fn(),
18+
};
19+
}
20+
721
describe('Hub', () => {
822
afterEach(() => {
923
jest.restoreAllMocks();
1024
jest.useRealTimers();
1125
});
1226

1327
test('call bindClient with provided client when constructing new instance', () => {
14-
const testClient: any = { setupIntegrations: jest.fn() };
15-
const spy = jest.spyOn(Hub.prototype, 'bindClient');
16-
new Hub(testClient);
17-
expect(spy).toHaveBeenCalledWith(testClient);
28+
const testClient = makeClient();
29+
const hub = new Hub(testClient);
30+
expect(hub.getClient()).toBe(testClient);
1831
});
1932

2033
test('push process into stack', () => {
@@ -60,8 +73,8 @@ describe('Hub', () => {
6073

6174
describe('bindClient', () => {
6275
test('should override current client', () => {
63-
const testClient: any = { setupIntegrations: jest.fn() };
64-
const nextClient: any = { setupIntegrations: jest.fn() };
76+
const testClient = makeClient();
77+
const nextClient = makeClient();
6578
const hub = new Hub(testClient);
6679
hub.bindClient(nextClient);
6780
expect(hub.getStack()).toHaveLength(1);
@@ -80,8 +93,8 @@ describe('Hub', () => {
8093
});
8194

8295
test('should call setupIntegration method of passed client', () => {
83-
const testClient: any = { setupIntegrations: jest.fn() };
84-
const nextClient: any = { setupIntegrations: jest.fn() };
96+
const testClient = makeClient();
97+
const nextClient = makeClient();
8598
const hub = new Hub(testClient);
8699
hub.bindClient(nextClient);
87100
expect(testClient.setupIntegrations).toHaveBeenCalled();
@@ -195,64 +208,53 @@ describe('Hub', () => {
195208

196209
describe('captureException', () => {
197210
test('simple', () => {
198-
const hub = new Hub();
199-
const spy = jest.spyOn(hub as any, '_invokeClient');
211+
const testClient = makeClient();
212+
const hub = new Hub(testClient);
200213
hub.captureException('a');
201-
expect(spy).toHaveBeenCalled();
202-
expect(spy.mock.calls[0][0]).toBe('captureException');
203-
expect(spy.mock.calls[0][1]).toBe('a');
214+
expect(testClient.captureException).toHaveBeenCalled();
215+
expect(testClient.captureException.mock.calls[0][0]).toBe('a');
204216
});
205217

206218
test('should set event_id in hint', () => {
207-
const hub = new Hub();
208-
const spy = jest.spyOn(hub as any, '_invokeClient');
219+
const testClient = makeClient();
220+
const hub = new Hub(testClient);
209221
hub.captureException('a');
210-
// @ts-ignore Says mock object is type unknown
211-
expect(spy.mock.calls[0][2].event_id).toBeTruthy();
222+
expect(testClient.captureException.mock.calls[0][1].event_id).toBeTruthy();
212223
});
213224

214225
test('should generate hint if not provided in the call', () => {
215-
const hub = new Hub();
216-
const spy = jest.spyOn(hub as any, '_invokeClient');
226+
const testClient = makeClient();
227+
const hub = new Hub(testClient);
217228
const ex = new Error('foo');
218229
hub.captureException(ex);
219-
// @ts-ignore Says mock object is type unknown
220-
expect(spy.mock.calls[0][2].originalException).toBe(ex);
221-
// @ts-ignore Says mock object is type unknown
222-
expect(spy.mock.calls[0][2].syntheticException).toBeInstanceOf(Error);
223-
// @ts-ignore Says mock object is type unknown
224-
expect(spy.mock.calls[0][2].syntheticException.message).toBe('Sentry syntheticException');
230+
expect(testClient.captureException.mock.calls[0][1].originalException).toBe(ex);
231+
expect(testClient.captureException.mock.calls[0][1].syntheticException).toBeInstanceOf(Error);
232+
expect(testClient.captureException.mock.calls[0][1].syntheticException.message).toBe('Sentry syntheticException');
225233
});
226234
});
227235

228236
describe('captureMessage', () => {
229237
test('simple', () => {
230-
const hub = new Hub();
231-
const spy = jest.spyOn(hub as any, '_invokeClient');
238+
const testClient = makeClient();
239+
const hub = new Hub(testClient);
232240
hub.captureMessage('a');
233-
expect(spy).toHaveBeenCalled();
234-
expect(spy.mock.calls[0][0]).toBe('captureMessage');
235-
expect(spy.mock.calls[0][1]).toBe('a');
241+
expect(testClient.captureMessage.mock.calls[0][0]).toBe('a');
236242
});
237243

238244
test('should set event_id in hint', () => {
239-
const hub = new Hub();
240-
const spy = jest.spyOn(hub as any, '_invokeClient');
245+
const testClient = makeClient();
246+
const hub = new Hub(testClient);
241247
hub.captureMessage('a');
242-
// @ts-ignore Says mock object is type unknown
243-
expect(spy.mock.calls[0][3].event_id).toBeTruthy();
248+
expect(testClient.captureMessage.mock.calls[0][2].event_id).toBeTruthy();
244249
});
245250

246251
test('should generate hint if not provided in the call', () => {
247-
const hub = new Hub();
248-
const spy = jest.spyOn(hub as any, '_invokeClient');
252+
const testClient = makeClient();
253+
const hub = new Hub(testClient);
249254
hub.captureMessage('foo');
250-
// @ts-ignore Says mock object is type unknown
251-
expect(spy.mock.calls[0][3].originalException).toBe('foo');
252-
// @ts-ignore Says mock object is type unknown
253-
expect(spy.mock.calls[0][3].syntheticException).toBeInstanceOf(Error);
254-
// @ts-ignore Says mock object is type unknown
255-
expect(spy.mock.calls[0][3].syntheticException.message).toBe('foo');
255+
expect(testClient.captureMessage.mock.calls[0][2].originalException).toBe('foo');
256+
expect(testClient.captureMessage.mock.calls[0][2].syntheticException).toBeInstanceOf(Error);
257+
expect(testClient.captureMessage.mock.calls[0][2].syntheticException.message).toBe('foo');
256258
});
257259
});
258260

@@ -261,46 +263,41 @@ describe('Hub', () => {
261263
const event: Event = {
262264
extra: { b: 3 },
263265
};
264-
const hub = new Hub();
265-
const spy = jest.spyOn(hub as any, '_invokeClient');
266+
const testClient = makeClient();
267+
const hub = new Hub(testClient);
266268
hub.captureEvent(event);
267-
expect(spy).toHaveBeenCalled();
268-
expect(spy.mock.calls[0][0]).toBe('captureEvent');
269-
expect(spy.mock.calls[0][1]).toBe(event);
269+
expect(testClient.captureEvent.mock.calls[0][0]).toBe(event);
270270
});
271271

272272
test('should set event_id in hint', () => {
273273
const event: Event = {
274274
extra: { b: 3 },
275275
};
276-
const hub = new Hub();
277-
const spy = jest.spyOn(hub as any, '_invokeClient');
276+
const testClient = makeClient();
277+
const hub = new Hub(testClient);
278278
hub.captureEvent(event);
279-
// @ts-ignore Says mock object is type unknown
280-
expect(spy.mock.calls[0][2].event_id).toBeTruthy();
279+
expect(testClient.captureEvent.mock.calls[0][1].event_id).toBeTruthy();
281280
});
282281

283282
test('sets lastEventId', () => {
284283
const event: Event = {
285284
extra: { b: 3 },
286285
};
287-
const hub = new Hub();
288-
const spy = jest.spyOn(hub as any, '_invokeClient');
286+
const testClient = makeClient();
287+
const hub = new Hub(testClient);
289288
hub.captureEvent(event);
290-
// @ts-ignore Says mock object is type unknown
291-
expect(spy.mock.calls[0][2].event_id).toEqual(hub.lastEventId());
289+
expect(testClient.captureEvent.mock.calls[0][1].event_id).toEqual(hub.lastEventId());
292290
});
293291

294292
test('transactions do not set lastEventId', () => {
295293
const event: Event = {
296294
extra: { b: 3 },
297295
type: 'transaction',
298296
};
299-
const hub = new Hub();
300-
const spy = jest.spyOn(hub as any, '_invokeClient');
297+
const testClient = makeClient();
298+
const hub = new Hub(testClient);
301299
hub.captureEvent(event);
302-
// @ts-ignore Says mock object is type unknown
303-
expect(spy.mock.calls[0][2].event_id).not.toEqual(hub.lastEventId());
300+
expect(testClient.captureEvent.mock.calls[0][1].event_id).not.toEqual(hub.lastEventId());
304301
});
305302
});
306303

0 commit comments

Comments
 (0)