1
- import { SDK_VERSION } from '@sentry/core' ;
1
+ import { initAndBind , SDK_VERSION } from '@sentry/core' ;
2
+ import { getMainCarrier } from '@sentry/hub' ;
3
+ import { Integration } from '@sentry/types' ;
2
4
import * as domain from 'domain' ;
3
5
4
6
import {
@@ -15,6 +17,14 @@ import {
15
17
} from '../src' ;
16
18
import { NodeBackend } from '../src/backend' ;
17
19
20
+ jest . mock ( '@sentry/core' , ( ) => {
21
+ const original = jest . requireActual ( '@sentry/core' ) ;
22
+ return {
23
+ ...original ,
24
+ initAndBind : jest . fn ( ) . mockImplementation ( original . initAndBind ) ,
25
+ } ;
26
+ } ) ;
27
+
18
28
const dsn = 'https://[email protected] /4291' ;
19
29
20
30
// eslint-disable-next-line no-var
@@ -26,6 +36,7 @@ describe('SentryNode', () => {
26
36
} ) ;
27
37
28
38
beforeEach ( ( ) => {
39
+ jest . clearAllMocks ( ) ;
29
40
getCurrentHub ( ) . pushScope ( ) ;
30
41
} ) ;
31
42
@@ -270,7 +281,32 @@ describe('SentryNode', () => {
270
281
} ) ;
271
282
} ) ;
272
283
284
+ function withAutoloadedIntegrations ( integrations : Integration [ ] , callback : ( ) => void ) {
285
+ const carrier = getMainCarrier ( ) ;
286
+ carrier . __SENTRY__ ! . integrations = integrations ;
287
+ callback ( ) ;
288
+ carrier . __SENTRY__ ! . integrations = undefined ;
289
+ delete carrier . __SENTRY__ ! . integrations ;
290
+ }
291
+
292
+ /** JSDoc */
293
+ class MockIntegration implements Integration {
294
+ public name : string ;
295
+
296
+ public constructor ( name : string ) {
297
+ this . name = name ;
298
+ }
299
+
300
+ public setupOnce ( ) : void {
301
+ // noop
302
+ }
303
+ }
304
+
273
305
describe ( 'SentryNode initialization' , ( ) => {
306
+ beforeEach ( ( ) => {
307
+ jest . clearAllMocks ( ) ;
308
+ } ) ;
309
+
274
310
test ( 'global.SENTRY_RELEASE is used to set release on initialization if available' , ( ) => {
275
311
global . SENTRY_RELEASE = { id : 'foobar' } ;
276
312
init ( { dsn } ) ;
@@ -333,4 +369,36 @@ describe('SentryNode initialization', () => {
333
369
expect ( sdkData . version ) . toEqual ( SDK_VERSION ) ;
334
370
} ) ;
335
371
} ) ;
372
+
373
+ describe ( 'autoloaded integrations' , ( ) => {
374
+ it ( 'should attach single integration to default integrations' , ( ) => {
375
+ withAutoloadedIntegrations ( [ new MockIntegration ( 'foo' ) ] , ( ) => {
376
+ init ( {
377
+ defaultIntegrations : [ new MockIntegration ( 'bar' ) ] ,
378
+ } ) ;
379
+ const integrations = ( initAndBind as jest . Mock ) . mock . calls [ 0 ] [ 1 ] . defaultIntegrations ;
380
+ expect ( integrations . map ( i => i . name ) ) . toEqual ( [ 'bar' , 'foo' ] ) ;
381
+ } ) ;
382
+ } ) ;
383
+
384
+ it ( 'should attach multiple integrations to default integrations' , ( ) => {
385
+ withAutoloadedIntegrations ( [ new MockIntegration ( 'foo' ) , new MockIntegration ( 'bar' ) ] , ( ) => {
386
+ init ( {
387
+ defaultIntegrations : [ new MockIntegration ( 'baz' ) , new MockIntegration ( 'qux' ) ] ,
388
+ } ) ;
389
+ const integrations = ( initAndBind as jest . Mock ) . mock . calls [ 0 ] [ 1 ] . defaultIntegrations ;
390
+ expect ( integrations . map ( i => i . name ) ) . toEqual ( [ 'baz' , 'qux' , 'foo' , 'bar' ] ) ;
391
+ } ) ;
392
+ } ) ;
393
+
394
+ it ( 'should ignore autoloaded integrations when defaultIntegrations:false' , ( ) => {
395
+ withAutoloadedIntegrations ( [ new MockIntegration ( 'foo' ) ] , ( ) => {
396
+ init ( {
397
+ defaultIntegrations : false ,
398
+ } ) ;
399
+ const integrations = ( initAndBind as jest . Mock ) . mock . calls [ 0 ] [ 1 ] . defaultIntegrations ;
400
+ expect ( integrations ) . toEqual ( [ ] ) ;
401
+ } ) ;
402
+ } ) ;
403
+ } ) ;
336
404
} ) ;
0 commit comments