Skip to content

Commit 5561844

Browse files
HazATkamilogorek
authored andcommitted
ref: Attach autoloaded integrations to defaultIntegrations
1 parent 1f1ba83 commit 5561844

File tree

5 files changed

+83
-51
lines changed

5 files changed

+83
-51
lines changed

packages/core/src/integration.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addGlobalEventProcessor, getCurrentHub, getMainCarrier } from '@sentry/hub';
1+
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
22
import { Integration, Options } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44

@@ -23,12 +23,10 @@ function filterDuplicates(integrations: Integration[]): Integration[] {
2323

2424
/** Gets integration to install */
2525
export function getIntegrationsToSetup(options: Options): Integration[] {
26-
const carrier = getMainCarrier();
27-
const autoloadedIntegrations = carrier.__SENTRY__?.integrations || [];
2826
const defaultIntegrations = (options.defaultIntegrations && [...options.defaultIntegrations]) || [];
2927
const userIntegrations = options.integrations;
3028

31-
let integrations: Integration[] = [...filterDuplicates(defaultIntegrations), ...autoloadedIntegrations];
29+
let integrations: Integration[] = [...filterDuplicates(defaultIntegrations)];
3230

3331
if (Array.isArray(userIntegrations)) {
3432
// Filter out integrations that are also included in user options

packages/core/test/lib/integration.test.ts

-41
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ class MockIntegration implements Integration {
1515
}
1616
}
1717

18-
function withAutoloadedIntegrations(integrations: Integration[], callback: () => void) {
19-
(global as any).__SENTRY__ = { integrations };
20-
callback();
21-
(global as any).__SENTRY__ = undefined;
22-
delete (global as any).__SENTRY__;
23-
}
24-
2518
describe('getIntegrationsToSetup', () => {
2619
it('works with empty array', () => {
2720
const integrations = getIntegrationsToSetup({
@@ -131,40 +124,6 @@ describe('getIntegrationsToSetup', () => {
131124
expect((integrations[1] as any).order).toEqual('secondUser');
132125
});
133126

134-
it('work with single autoloaded integration', () => {
135-
withAutoloadedIntegrations([new MockIntegration('foo')], () => {
136-
const integrations = getIntegrationsToSetup({});
137-
expect(integrations.map(i => i.name)).toEqual(['foo']);
138-
});
139-
});
140-
141-
it('work with multiple autoloaded integrations', () => {
142-
withAutoloadedIntegrations([new MockIntegration('foo'), new MockIntegration('bar')], () => {
143-
const integrations = getIntegrationsToSetup({});
144-
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
145-
});
146-
});
147-
148-
it('user integrations override autoloaded', () => {
149-
const firstAutoloaded = new MockIntegration('foo');
150-
(firstAutoloaded as any).order = 'firstAutoloaded';
151-
const secondAutoloaded = new MockIntegration('bar');
152-
(secondAutoloaded as any).order = 'secondAutoloaded';
153-
const firstUser = new MockIntegration('foo');
154-
(firstUser as any).order = 'firstUser';
155-
const secondUser = new MockIntegration('bar');
156-
(secondUser as any).order = 'secondUser';
157-
158-
withAutoloadedIntegrations([firstAutoloaded, secondAutoloaded], () => {
159-
const integrations = getIntegrationsToSetup({
160-
integrations: [firstUser, secondUser],
161-
});
162-
expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']);
163-
expect((integrations[0] as any).order).toEqual('firstUser');
164-
expect((integrations[1] as any).order).toEqual('secondUser');
165-
});
166-
});
167-
168127
it('always moves Debug integration to the end of the list', () => {
169128
let integrations = getIntegrationsToSetup({
170129
defaultIntegrations: [new MockIntegration('Debug'), new MockIntegration('foo')],

packages/hub/src/hub.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { Session } from './session';
3535
*
3636
* @hidden
3737
*/
38-
export const API_VERSION = 3;
38+
export const API_VERSION = 4;
3939

4040
/**
4141
* Default maximum number of breadcrumbs added to an event. Can be overwritten

packages/node/src/sdk.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,16 @@ export const defaultIntegrations = [
7777
* @see {@link NodeOptions} for documentation on configuration options.
7878
*/
7979
export function init(options: NodeOptions = {}): void {
80-
if (options.defaultIntegrations === undefined) {
81-
options.defaultIntegrations = defaultIntegrations;
82-
}
80+
const carrier = getMainCarrier();
81+
const autoloadedIntegrations = carrier.__SENTRY__?.integrations || [];
82+
83+
options.defaultIntegrations =
84+
options.defaultIntegrations === false
85+
? []
86+
: [
87+
...(Array.isArray(options.defaultIntegrations) ? options.defaultIntegrations : defaultIntegrations),
88+
...autoloadedIntegrations,
89+
];
8390

8491
if (options.dsn === undefined && process.env.SENTRY_DSN) {
8592
options.dsn = process.env.SENTRY_DSN;
@@ -105,7 +112,7 @@ export function init(options: NodeOptions = {}): void {
105112

106113
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
107114
if ((domain as any).active) {
108-
setHubOnCarrier(getMainCarrier(), getCurrentHub());
115+
setHubOnCarrier(carrier, getCurrentHub());
109116
}
110117

111118
initAndBind(NodeClient, options);

packages/node/test/index.test.ts

+69-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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';
24
import * as domain from 'domain';
35

46
import {
@@ -15,6 +17,14 @@ import {
1517
} from '../src';
1618
import { 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+
1828
const 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+
273305
describe('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

Comments
 (0)