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' ;
24import * as domain from 'domain' ;
35
46import {
@@ -15,6 +17,14 @@ import {
1517} from '../src' ;
1618import { NodeBackend } from '../src/backend' ;
1719
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+
1828const dsn = 'https://[email protected] /4291' ; 1929
2030// eslint-disable-next-line no-var
@@ -26,6 +36,7 @@ describe('SentryNode', () => {
2636 } ) ;
2737
2838 beforeEach ( ( ) => {
39+ jest . clearAllMocks ( ) ;
2940 getCurrentHub ( ) . pushScope ( ) ;
3041 } ) ;
3142
@@ -270,7 +281,32 @@ describe('SentryNode', () => {
270281 } ) ;
271282} ) ;
272283
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+
273305describe ( 'SentryNode initialization' , ( ) => {
306+ beforeEach ( ( ) => {
307+ jest . clearAllMocks ( ) ;
308+ } ) ;
309+
274310 test ( 'global.SENTRY_RELEASE is used to set release on initialization if available' , ( ) => {
275311 global . SENTRY_RELEASE = { id : 'foobar' } ;
276312 init ( { dsn } ) ;
@@ -333,4 +369,36 @@ describe('SentryNode initialization', () => {
333369 expect ( sdkData . version ) . toEqual ( SDK_VERSION ) ;
334370 } ) ;
335371 } ) ;
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+ } ) ;
336404} ) ;
0 commit comments