@@ -2,6 +2,7 @@ import { Hub, Scope, Session } from '@sentry/hub';
2
2
import { Event , Outcome , Severity , Span , Transport } from '@sentry/types' ;
3
3
import { logger , SentryError , SyncPromise } from '@sentry/utils' ;
4
4
5
+ import { BaseClient } from '../../src/baseclient' ;
5
6
import * as integrationModule from '../../src/integration' ;
6
7
import { TestBackend } from '../mocks/backend' ;
7
8
import { TestClient } from '../mocks/client' ;
@@ -12,6 +13,9 @@ const PUBLIC_DSN = 'https://username@domain/123';
12
13
// eslint-disable-next-line no-var
13
14
declare var global : any ;
14
15
16
+ const backendEventFromException = jest . spyOn ( TestBackend . prototype , 'eventFromException' ) ;
17
+ const clientProcess = jest . spyOn ( TestClient . prototype as any , '_process' ) ;
18
+
15
19
jest . mock ( '@sentry/utils' , ( ) => {
16
20
const original = jest . requireActual ( '@sentry/utils' ) ;
17
21
return {
@@ -57,7 +61,7 @@ describe('BaseClient', () => {
57
61
} ) ;
58
62
59
63
afterEach ( ( ) => {
60
- jest . restoreAllMocks ( ) ;
64
+ jest . clearAllMocks ( ) ;
61
65
} ) ;
62
66
63
67
describe ( 'constructor() / getDsn()' , ( ) => {
@@ -249,6 +253,43 @@ describe('BaseClient', () => {
249
253
} ) ,
250
254
) ;
251
255
} ) ;
256
+
257
+ test . each ( [
258
+ [ '`Error` instance' , new Error ( 'Will I get caught twice?' ) ] ,
259
+ [ 'plain object' , { 'Will I' : 'get caught twice?' } ] ,
260
+ [ 'primitive wrapper' , new String ( 'Will I get caught twice?' ) ] ,
261
+ ] ) ( "doesn't capture the same exception twice - %s" , ( _name : string , thrown : any ) => {
262
+ const client = new TestClient ( { dsn : PUBLIC_DSN } ) ;
263
+
264
+ expect ( thrown . __sentry_captured__ ) . toBeUndefined ( ) ;
265
+
266
+ client . captureException ( thrown ) ;
267
+
268
+ expect ( thrown . __sentry_captured__ ) . toBe ( true ) ;
269
+ expect ( backendEventFromException ) . toHaveBeenCalledTimes ( 1 ) ;
270
+
271
+ client . captureException ( thrown ) ;
272
+
273
+ // `captureException` should bail right away this second time around and not get as far as calling this again
274
+ expect ( backendEventFromException ) . toHaveBeenCalledTimes ( 1 ) ;
275
+ } ) ;
276
+
277
+ test ( "doesn't capture the same exception twice" , ( ) => {
278
+ const client = new TestClient ( { dsn : PUBLIC_DSN } ) ;
279
+ const err = new Error ( 'Will I get caught twice?' ) as any ;
280
+
281
+ expect ( err . __sentry_captured__ ) . toBeUndefined ( ) ;
282
+
283
+ client . captureException ( err ) ;
284
+
285
+ expect ( err . __sentry_captured__ ) . toBe ( true ) ;
286
+ expect ( backendEventFromException ) . toHaveBeenCalledTimes ( 1 ) ;
287
+
288
+ client . captureException ( err ) ;
289
+
290
+ // `captureException` should bail right away this second time around and not get as far as calling this again
291
+ expect ( backendEventFromException ) . toHaveBeenCalledTimes ( 1 ) ;
292
+ } ) ;
252
293
} ) ;
253
294
254
295
describe ( 'captureMessage' , ( ) => {
@@ -325,6 +366,28 @@ describe('BaseClient', () => {
325
366
expect ( TestBackend . instance ! . event ) . toBeUndefined ( ) ;
326
367
} ) ;
327
368
369
+ test . each ( [
370
+ [ '`Error` instance' , new Error ( 'Will I get caught twice?' ) ] ,
371
+ [ 'plain object' , { 'Will I' : 'get caught twice?' } ] ,
372
+ [ 'primitive wrapper' , new String ( 'Will I get caught twice?' ) ] ,
373
+ ] ) ( "doesn't capture the same exception twice - %s" , ( _name : string , thrown : any ) => {
374
+ const client = new TestClient ( { dsn : PUBLIC_DSN } ) ;
375
+ const event = { exception : { values : [ { type : 'Error' , message : 'Will I get caught twice?' } ] } } ;
376
+ const hint = { originalException : thrown } ;
377
+
378
+ expect ( thrown . __sentry_captured__ ) . toBeUndefined ( ) ;
379
+
380
+ client . captureEvent ( event , hint ) ;
381
+
382
+ expect ( thrown . __sentry_captured__ ) . toBe ( true ) ;
383
+ expect ( clientProcess ) . toHaveBeenCalledTimes ( 1 ) ;
384
+
385
+ client . captureEvent ( event , hint ) ;
386
+
387
+ // `captureEvent` should bail right away this second time around and not get as far as calling this again
388
+ expect ( clientProcess ) . toHaveBeenCalledTimes ( 1 ) ;
389
+ } ) ;
390
+
328
391
test ( 'sends an event' , ( ) => {
329
392
expect . assertions ( 2 ) ;
330
393
const client = new TestClient ( { dsn : PUBLIC_DSN } ) ;
@@ -798,7 +861,7 @@ describe('BaseClient', () => {
798
861
expect ( TestBackend . instance ! . event ) . toBeUndefined ( ) ;
799
862
} ) ;
800
863
801
- test ( 'calls beforeSend gets an access to a hint as a second argument' , ( ) => {
864
+ test ( 'beforeSend gets access to a hint as a second argument' , ( ) => {
802
865
expect . assertions ( 2 ) ;
803
866
const beforeSend = jest . fn ( ( event , hint ) => ( { ...event , data : hint . data } ) ) ;
804
867
const client = new TestClient ( { dsn : PUBLIC_DSN , beforeSend } ) ;
0 commit comments