@@ -4,17 +4,30 @@ import { getCurrentHub, Hub, Scope } from '../src';
4
4
5
5
const clientFn : any = jest . fn ( ) ;
6
6
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
+
7
21
describe ( 'Hub' , ( ) => {
8
22
afterEach ( ( ) => {
9
23
jest . restoreAllMocks ( ) ;
10
24
jest . useRealTimers ( ) ;
11
25
} ) ;
12
26
13
27
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 ) ;
18
31
} ) ;
19
32
20
33
test ( 'push process into stack' , ( ) => {
@@ -60,8 +73,8 @@ describe('Hub', () => {
60
73
61
74
describe ( 'bindClient' , ( ) => {
62
75
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 ( ) ;
65
78
const hub = new Hub ( testClient ) ;
66
79
hub . bindClient ( nextClient ) ;
67
80
expect ( hub . getStack ( ) ) . toHaveLength ( 1 ) ;
@@ -80,8 +93,8 @@ describe('Hub', () => {
80
93
} ) ;
81
94
82
95
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 ( ) ;
85
98
const hub = new Hub ( testClient ) ;
86
99
hub . bindClient ( nextClient ) ;
87
100
expect ( testClient . setupIntegrations ) . toHaveBeenCalled ( ) ;
@@ -195,64 +208,53 @@ describe('Hub', () => {
195
208
196
209
describe ( 'captureException' , ( ) => {
197
210
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 ) ;
200
213
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' ) ;
204
216
} ) ;
205
217
206
218
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 ) ;
209
221
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 ( ) ;
212
223
} ) ;
213
224
214
225
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 ) ;
217
228
const ex = new Error ( 'foo' ) ;
218
229
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' ) ;
225
233
} ) ;
226
234
} ) ;
227
235
228
236
describe ( 'captureMessage' , ( ) => {
229
237
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 ) ;
232
240
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' ) ;
236
242
} ) ;
237
243
238
244
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 ) ;
241
247
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 ( ) ;
244
249
} ) ;
245
250
246
251
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 ) ;
249
254
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' ) ;
256
258
} ) ;
257
259
} ) ;
258
260
@@ -261,46 +263,41 @@ describe('Hub', () => {
261
263
const event : Event = {
262
264
extra : { b : 3 } ,
263
265
} ;
264
- const hub = new Hub ( ) ;
265
- const spy = jest . spyOn ( hub as any , '_invokeClient' ) ;
266
+ const testClient = makeClient ( ) ;
267
+ const hub = new Hub ( testClient ) ;
266
268
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 ) ;
270
270
} ) ;
271
271
272
272
test ( 'should set event_id in hint' , ( ) => {
273
273
const event : Event = {
274
274
extra : { b : 3 } ,
275
275
} ;
276
- const hub = new Hub ( ) ;
277
- const spy = jest . spyOn ( hub as any , '_invokeClient' ) ;
276
+ const testClient = makeClient ( ) ;
277
+ const hub = new Hub ( testClient ) ;
278
278
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 ( ) ;
281
280
} ) ;
282
281
283
282
test ( 'sets lastEventId' , ( ) => {
284
283
const event : Event = {
285
284
extra : { b : 3 } ,
286
285
} ;
287
- const hub = new Hub ( ) ;
288
- const spy = jest . spyOn ( hub as any , '_invokeClient' ) ;
286
+ const testClient = makeClient ( ) ;
287
+ const hub = new Hub ( testClient ) ;
289
288
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 ( ) ) ;
292
290
} ) ;
293
291
294
292
test ( 'transactions do not set lastEventId' , ( ) => {
295
293
const event : Event = {
296
294
extra : { b : 3 } ,
297
295
type : 'transaction' ,
298
296
} ;
299
- const hub = new Hub ( ) ;
300
- const spy = jest . spyOn ( hub as any , '_invokeClient' ) ;
297
+ const testClient = makeClient ( ) ;
298
+ const hub = new Hub ( testClient ) ;
301
299
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 ( ) ) ;
304
301
} ) ;
305
302
} ) ;
306
303
0 commit comments