1
- import { Event } from '@sentry/types' ;
1
+ /* eslint-disable @typescript-eslint/unbound-method */
2
+ import { Client , Event } from '@sentry/types' ;
2
3
3
4
import { getCurrentHub , Hub , Scope } from '../src' ;
4
5
@@ -15,7 +16,18 @@ function makeClient() {
15
16
getIntegration : jest . fn ( ) ,
16
17
setupIntegrations : jest . fn ( ) ,
17
18
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." ] ;
19
31
}
20
32
21
33
describe ( 'Hub' , ( ) => {
@@ -102,7 +114,7 @@ describe('Hub', () => {
102
114
} ) ;
103
115
} ) ;
104
116
105
- test ( 'inherit processors' , ( ) => {
117
+ test ( 'inherit processors' , async ( ) => {
106
118
expect . assertions ( 1 ) ;
107
119
const event : Event = {
108
120
extra : { b : 3 } ,
@@ -210,67 +222,90 @@ describe('Hub', () => {
210
222
test ( 'simple' , ( ) => {
211
223
const testClient = makeClient ( ) ;
212
224
const hub = new Hub ( testClient ) ;
225
+
213
226
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' ) ;
216
230
} ) ;
217
231
218
232
test ( 'should set event_id in hint' , ( ) => {
219
233
const testClient = makeClient ( ) ;
220
234
const hub = new Hub ( testClient ) ;
235
+
221
236
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 ( ) ;
223
240
} ) ;
224
241
225
242
test ( 'should keep event_id from hint' , ( ) => {
226
243
const testClient = makeClient ( ) ;
227
244
const hub = new Hub ( testClient ) ;
228
245
const id = Math . random ( ) . toString ( ) ;
246
+
229
247
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 ) ;
231
251
} ) ;
232
252
233
253
test ( 'should generate hint if not provided in the call' , ( ) => {
234
254
const testClient = makeClient ( ) ;
235
255
const hub = new Hub ( testClient ) ;
236
256
const ex = new Error ( 'foo' ) ;
257
+
237
258
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' ) ;
241
264
} ) ;
242
265
} ) ;
243
266
244
267
describe ( 'captureMessage' , ( ) => {
245
268
test ( 'simple' , ( ) => {
246
269
const testClient = makeClient ( ) ;
247
270
const hub = new Hub ( testClient ) ;
271
+
248
272
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' ) ;
250
276
} ) ;
251
277
252
278
test ( 'should set event_id in hint' , ( ) => {
253
279
const testClient = makeClient ( ) ;
254
280
const hub = new Hub ( testClient ) ;
281
+
255
282
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 ( ) ;
257
286
} ) ;
258
287
259
288
test ( 'should keep event_id from hint' , ( ) => {
260
289
const testClient = makeClient ( ) ;
261
290
const hub = new Hub ( testClient ) ;
262
291
const id = Math . random ( ) . toString ( ) ;
292
+
263
293
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 ) ;
265
297
} ) ;
266
298
267
299
test ( 'should generate hint if not provided in the call' , ( ) => {
268
300
const testClient = makeClient ( ) ;
269
301
const hub = new Hub ( testClient ) ;
302
+
270
303
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' ) ;
274
309
} ) ;
275
310
} ) ;
276
311
@@ -281,8 +316,11 @@ describe('Hub', () => {
281
316
} ;
282
317
const testClient = makeClient ( ) ;
283
318
const hub = new Hub ( testClient ) ;
319
+
284
320
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 ) ;
286
324
} ) ;
287
325
288
326
test ( 'should set event_id in hint' , ( ) => {
@@ -291,8 +329,11 @@ describe('Hub', () => {
291
329
} ;
292
330
const testClient = makeClient ( ) ;
293
331
const hub = new Hub ( testClient ) ;
332
+
294
333
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 ( ) ;
296
337
} ) ;
297
338
298
339
test ( 'should keep event_id from hint' , ( ) => {
@@ -302,8 +343,11 @@ describe('Hub', () => {
302
343
const testClient = makeClient ( ) ;
303
344
const hub = new Hub ( testClient ) ;
304
345
const id = Math . random ( ) . toString ( ) ;
346
+
305
347
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 ) ;
307
351
} ) ;
308
352
309
353
test ( 'sets lastEventId' , ( ) => {
@@ -312,8 +356,11 @@ describe('Hub', () => {
312
356
} ;
313
357
const testClient = makeClient ( ) ;
314
358
const hub = new Hub ( testClient ) ;
359
+
315
360
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 ( ) ) ;
317
364
} ) ;
318
365
319
366
test ( 'transactions do not set lastEventId' , ( ) => {
@@ -323,8 +370,11 @@ describe('Hub', () => {
323
370
} ;
324
371
const testClient = makeClient ( ) ;
325
372
const hub = new Hub ( testClient ) ;
373
+
326
374
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 ( ) ) ;
328
378
} ) ;
329
379
} ) ;
330
380
0 commit comments