Skip to content

Commit f37ed1a

Browse files
committed
feat: Override default integrations provided through array options
1 parent 2b8687f commit f37ed1a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

packages/core/src/sdk.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export function initAndBind<F extends Client, O extends Options>(
3838

3939
let integrations = [...defaultIntegrations];
4040
if (Array.isArray(options.integrations)) {
41-
integrations = [...integrations, ...options.integrations];
41+
const providedIntegrationsNames = options.integrations.map(i => i.name);
42+
integrations = [
43+
// Leave only unique integrations, that were not overridden with provided integrations with the same name
44+
...integrations.filter(integration => providedIntegrationsNames.indexOf(integration.name) === -1),
45+
...options.integrations,
46+
];
4247
} else if (typeof options.integrations === 'function') {
4348
integrations = options.integrations(integrations);
4449
}

packages/core/test/lib/sdk.test.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { TestClient } from '../mocks/client';
55
declare var global: any;
66

77
class MockIntegration implements Integration {
8-
public name: string = 'MockIntegration';
8+
public constructor(name: string) {
9+
this.name = name;
10+
}
11+
public name: string;
912
public handler: () => void = jest.fn();
1013
public install: () => void = () => {
1114
this.handler();
@@ -19,32 +22,48 @@ describe('SDK', () => {
1922

2023
describe('initAndBind', () => {
2124
test('installs default integrations', () => {
22-
const DEFAULT_INTEGRATIONS: Integration[] = [new MockIntegration(), new MockIntegration()];
25+
const DEFAULT_INTEGRATIONS: Integration[] = [
26+
new MockIntegration('MockIntegration 1'),
27+
new MockIntegration('MockIntegration 2'),
28+
];
2329
initAndBind(TestClient, {}, DEFAULT_INTEGRATIONS);
2430
expect(DEFAULT_INTEGRATIONS[0].handler.mock.calls.length).toBe(1);
2531
expect(DEFAULT_INTEGRATIONS[1].handler.mock.calls.length).toBe(1);
2632
});
2733

2834
test('installs integrations provided through options', () => {
29-
const integrations: Integration[] = [new MockIntegration(), new MockIntegration()];
35+
const integrations: Integration[] = [
36+
new MockIntegration('MockIntegration 1'),
37+
new MockIntegration('MockIntegration 2'),
38+
];
3039
initAndBind(TestClient, { integrations }, []);
3140
expect(integrations[0].handler.mock.calls.length).toBe(1);
3241
expect(integrations[1].handler.mock.calls.length).toBe(1);
3342
});
3443

35-
test('installs merged default integrations and one provided through options', () => {
36-
const DEFAULT_INTEGRATIONS: Integration[] = [new MockIntegration(), new MockIntegration()];
37-
const integrations: Integration[] = [new MockIntegration(), new MockIntegration()];
44+
test('installs merged default integrations, with overrides provided through options', () => {
45+
const DEFAULT_INTEGRATIONS: Integration[] = [
46+
new MockIntegration('MockIntegration 1'),
47+
new MockIntegration('MockIntegration 2'),
48+
];
49+
const integrations: Integration[] = [
50+
new MockIntegration('MockIntegration 1'),
51+
new MockIntegration('MockIntegration 3'),
52+
];
3853
initAndBind(TestClient, { integrations }, DEFAULT_INTEGRATIONS);
39-
expect(DEFAULT_INTEGRATIONS[0].handler.mock.calls.length).toBe(1);
54+
// 'MockIntegration 1' should be overridden by the one with the same name provided through options
55+
expect(DEFAULT_INTEGRATIONS[0].handler.mock.calls.length).toBe(0);
4056
expect(DEFAULT_INTEGRATIONS[1].handler.mock.calls.length).toBe(1);
4157
expect(integrations[0].handler.mock.calls.length).toBe(1);
4258
expect(integrations[1].handler.mock.calls.length).toBe(1);
4359
});
4460

4561
test('installs integrations returned from a callback function', () => {
46-
const DEFAULT_INTEGRATIONS: Integration[] = [new MockIntegration(), new MockIntegration()];
47-
const newIntegration = new MockIntegration();
62+
const DEFAULT_INTEGRATIONS: Integration[] = [
63+
new MockIntegration('MockIntegration 1'),
64+
new MockIntegration('MockIntegration 2'),
65+
];
66+
const newIntegration = new MockIntegration('MockIntegration 3');
4867
initAndBind(
4968
TestClient,
5069
{

0 commit comments

Comments
 (0)