Skip to content

Commit ba61707

Browse files
committed
add tests for integration setup in browser init
1 parent 9371d48 commit ba61707

File tree

2 files changed

+129
-66
lines changed

2 files changed

+129
-66
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { NoopTransport, Scope } from '@sentry/core';
2+
import { Client, Integration } from '@sentry/types';
3+
import { BrowserOptions } from '../../src/client';
4+
5+
import { MockIntegration } from '@sentry/core/test/lib/sdk.test';
6+
7+
import { init } from '../../src/sdk';
8+
// eslint-disable-next-line no-var
9+
declare var global: any;
10+
11+
const PUBLIC_DSN = 'https://username@domain/123';
12+
13+
function getDefaultBrowserOptions(options: Partial<BrowserOptions> = {}): BrowserOptions {
14+
return {
15+
integrations: [],
16+
transport: NoopTransport,
17+
stackParser: () => [],
18+
...options,
19+
};
20+
}
21+
22+
jest.mock('@sentry/hub', () => {
23+
const original = jest.requireActual('@sentry/hub');
24+
return {
25+
...original,
26+
getCurrentHub(): {
27+
bindClient(client: Client): boolean;
28+
getClient(): boolean;
29+
getScope(): Scope;
30+
} {
31+
return {
32+
getClient(): boolean {
33+
return false;
34+
},
35+
getScope(): Scope {
36+
return new Scope();
37+
},
38+
bindClient(client: Client): boolean {
39+
client.setupIntegrations();
40+
return true;
41+
},
42+
};
43+
},
44+
};
45+
});
46+
47+
describe('init', () => {
48+
beforeEach(() => {
49+
jest.clearAllMocks();
50+
});
51+
52+
afterAll(() => {
53+
jest.resetAllMocks();
54+
});
55+
56+
test('installs default integrations', () => {
57+
const DEFAULT_INTEGRATIONS: Integration[] = [
58+
new MockIntegration('MockIntegration 0.1'),
59+
new MockIntegration('MockIntegration 0.2'),
60+
];
61+
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS });
62+
63+
init(options);
64+
65+
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
66+
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
67+
});
68+
69+
test("doesn't install default integrations if told not to", () => {
70+
const DEFAULT_INTEGRATIONS: Integration[] = [
71+
new MockIntegration('MockIntegration 0.3'),
72+
new MockIntegration('MockIntegration 0.4'),
73+
];
74+
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: false });
75+
init(options);
76+
77+
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
78+
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(0);
79+
});
80+
81+
it('installs merged default integrations, with overrides provided through options', () => {
82+
const DEFAULT_INTEGRATIONS: Integration[] = [
83+
new MockIntegration('MockIntegration 1.1'),
84+
new MockIntegration('MockIntegration 1.2'),
85+
];
86+
87+
const integrations: Integration[] = [
88+
new MockIntegration('MockIntegration 1.1'),
89+
new MockIntegration('MockIntegration 1.3'),
90+
];
91+
const options = getDefaultBrowserOptions({
92+
dsn: PUBLIC_DSN,
93+
defaultIntegrations: DEFAULT_INTEGRATIONS,
94+
integrations,
95+
});
96+
97+
init(options);
98+
// 'MockIntegration 1' should be overridden by the one with the same name provided through options
99+
expect(DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
100+
expect(DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
101+
expect(integrations[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
102+
expect(integrations[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
103+
});
104+
105+
it('installs integrations returned from a callback function', () => {
106+
const DEFAULT_INTEGRATIONS: Integration[] = [
107+
new MockIntegration('MockIntegration 2.1'),
108+
new MockIntegration('MockIntegration 2.2'),
109+
];
110+
111+
const newIntegration = new MockIntegration('MockIntegration 2.3');
112+
const options = getDefaultBrowserOptions({
113+
defaultIntegrations: DEFAULT_INTEGRATIONS,
114+
dsn: PUBLIC_DSN,
115+
integrations: (integrations: Integration[]) => {
116+
const t = integrations.slice(0, 1).concat(newIntegration);
117+
return t;
118+
},
119+
});
120+
121+
init(options);
122+
123+
expect(DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
124+
expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
125+
expect(DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(0);
126+
});
127+
});

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

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Scope } from '@sentry/hub';
22
import { Client, Integration } from '@sentry/types';
33

4-
import { getIntegrationsToSetup, installedIntegrations } from '../../src/integration';
4+
import { installedIntegrations } from '../../src/integration';
55
import { initAndBind } from '../../src/sdk';
66
import { setupTestTransport, TestClient, getDefaultTestClientOptions } from '../mocks/client';
77

@@ -35,7 +35,7 @@ jest.mock('@sentry/hub', () => {
3535
};
3636
});
3737

38-
class MockIntegration implements Integration {
38+
export class MockIntegration implements Integration {
3939
public name: string;
4040
public setupOnce: () => void = jest.fn();
4141
public constructor(name: string) {
@@ -50,28 +50,6 @@ describe('SDK', () => {
5050
});
5151

5252
describe('initAndBind', () => {
53-
test('installs default integrations', () => {
54-
const DEFAULT_INTEGRATIONS: Integration[] = [
55-
new MockIntegration('MockIntegration 1'),
56-
new MockIntegration('MockIntegration 2'),
57-
];
58-
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, integrations: DEFAULT_INTEGRATIONS });
59-
initAndBind(TestClient, options, setupTestTransport(options).transport);
60-
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
61-
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
62-
});
63-
64-
test("doesn't install default integrations if told not to", () => {
65-
const DEFAULT_INTEGRATIONS: Integration[] = [
66-
new MockIntegration('MockIntegration 1'),
67-
new MockIntegration('MockIntegration 2'),
68-
];
69-
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, defaultIntegrations: false });
70-
initAndBind(TestClient, options, setupTestTransport(options).transport);
71-
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
72-
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(0);
73-
});
74-
7553
test('installs integrations provided through options', () => {
7654
const integrations: Integration[] = [
7755
new MockIntegration('MockIntegration 1'),
@@ -82,47 +60,5 @@ describe('SDK', () => {
8260
expect((integrations[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
8361
expect((integrations[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
8462
});
85-
86-
// TODO:
87-
test('installs merged default integrations, with overrides provided through options', () => {
88-
const DEFAULT_INTEGRATIONS: Integration[] = [
89-
new MockIntegration('MockIntegration 1'),
90-
new MockIntegration('MockIntegration 2'),
91-
];
92-
const integrations: Integration[] = [
93-
new MockIntegration('MockIntegration 1'),
94-
new MockIntegration('MockIntegration 3'),
95-
];
96-
const options = getDefaultTestClientOptions({
97-
dsn: PUBLIC_DSN,
98-
defaultIntegrations: DEFAULT_INTEGRATIONS,
99-
integrations,
100-
});
101-
options.integrations = getIntegrationsToSetup(options);
102-
103-
initAndBind(TestClient, options, setupTestTransport(options).transport);
104-
// 'MockIntegration 1' should be overridden by the one with the same name provided through options
105-
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
106-
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
107-
expect((integrations[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
108-
expect((integrations[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
109-
});
110-
111-
test.skip('installs integrations returned from a callback function', () => {
112-
const DEFAULT_INTEGRATIONS: Integration[] = [
113-
new MockIntegration('MockIntegration 1'),
114-
new MockIntegration('MockIntegration 2'),
115-
];
116-
const newIntegration = new MockIntegration('MockIntegration 3');
117-
const options = getDefaultTestClientOptions({
118-
defaultIntegrations: DEFAULT_INTEGRATIONS,
119-
dsn: PUBLIC_DSN,
120-
// integrations: (integrations: Integration[]) => integrations.slice(0, 1).concat(newIntegration),
121-
});
122-
initAndBind(TestClient, options, setupTestTransport(options).transport);
123-
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
124-
expect((newIntegration.setupOnce as jest.Mock).mock.calls.length).toBe(1);
125-
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(0);
126-
});
12763
});
12864
});

0 commit comments

Comments
 (0)