8000 fix & add test · getsentry/sentry-javascript@7fd601a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fd601a

Browse files
committed
fix & add test
1 parent e507097 commit 7fd601a

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed

packages/browser/src/sdk.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ export function init(browserOptions: BrowserOptions = {}): Client | undefined {
130130
const options = applyDefaultOptions(browserOptions);
131131
const defaultIntegrations = getDefaultIntegrations(browserOptions);
132132

133-
const showBrowserExtensionError = !options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError();
133+
const isBrowserExtension = !options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError();
134134

135135
if (DEBUG_BUILD) {
136136
logBrowserEnvironmentWarnings({
137-
browserExtension: showBrowserExtensionError,
137+
browserExtension: isBrowserExtension,
138138
fetch: !supportsFetch(),
139139
});
140140
}
141141

142-
if (showBrowserExtensionError) {
142+
if (isBrowserExtension) {
143143
return;
144144
}
145145

@@ -162,16 +162,16 @@ export function initWithDefaultIntegrations(
162162
const options = applyDefaultOptions(browserOptions);
163163
const defaultIntegrations = getDefaultIntegrationsImpl(browserOptions);
164164

165-
const showBrowserExtensionError = !options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError();
165+
const isBrowserExtension = !options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError();
166166

167167
if (DEBUG_BUILD) {
168168
logBrowserEnvironmentWarnings({
169-
browserExtension: showBrowserExtensionError,
169+
browserExtension: isBrowserExtension,
170170
fetch: !supportsFetch(),
171171
});
172172
}
173173

174-
if (showBrowserExtensionError) {
174+
if (isBrowserExtension) {
175175
return;
176176
}
177177
const clientOptions = getClientOptions(options, defaultIntegrations);

packages/browser/test/sdk.test.ts

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/* eslint-disable @typescript-eslint/unbound-method */
66
import type { Mock } from 'vitest';
7-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
7+
import { afterEach, describe, expect, it, vi } from 'vitest';
88

99
import * as SentryCore from '@sentry/core';
1010
import { createTransport } from '@sentry/core';
@@ -13,7 +13,7 @@ import type { Integration } from '@sentry/core';
1313

1414
import type { BrowserOptions } from '../src';
1515
import { WINDOW } from '../src';
16-
import { applyDefaultOptions, init } from '../src/sdk';
16+
import { applyDefaultOptions, init, initWithDefaultIntegrations } from '../src/sdk';
1717

1818
const PUBLIC_DSN = 'https://username@domain/123';
1919

@@ -35,15 +35,11 @@ export class MockIntegration implements Integration {
3535
}
3636

3737
describe('init', () => {
38-
beforeEach(() => {
39-
vi.clearAllMocks();
38+
afterEach(() => {
39+
vi.restoreAllMocks();
4040
});
4141

42-
afterAll(() => {
43-
vi.resetAllMocks();
44-
});
45-
46-
test('installs default integrations', () => {
42+
test('installs passed default integrations', () => {
4743
const DEFAULT_INTEGRATIONS: Integration[] = [
4844
new MockIntegration('MockIntegration 0.1'),
4945
new MockIntegration('MockIntegration 0.2'),
@@ -56,28 +52,41 @@ describe('init', () => {
5652
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
5753
});
5854

55+
it('installs default integrations', () => {
56+
// Note: We need to prevent this from actually adding all the default integrations, as otherwise
57+
// following tests may fail (e.g. because console is monkey patched etc.)
58+
const spyGetIntegrationsToSetup = vi.spyOn(SentryCore, 'getIntegrationsToSetup').mockImplementation(() => []);
59+
60+
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN });
61+
init(options);
62+
63+
expect(spyGetIntegrationsToSetup).toHaveBeenCalledTimes(1);
64+
expect(spyGetIntegrationsToSetup).toHaveBeenCalledWith(
65+
expect.objectContaining(options),
66+
expect.arrayContaining([expect.objectContaining({ name: 'InboundFilters' })]),
67+
);
68+
});
69+
5970
it('installs default integrations if `defaultIntegrations: undefined`', () => {
60-
// @ts-expect-error this is fine for testing
61-
const initAndBindSpy = vi.spyOn(SentryCore, 'initAndBind').mockImplementationOnce(() => {});
71+
// Note: We need to prevent this from actually adding all the default integrations, as otherwise
72+
// following tests may fail (e.g. because console is monkey patched etc.)
73+
const spyGetIntegrationsToSetup = vi.spyOn(SentryCore, 'getIntegrationsToSetup').mockImplementation(() => []);
74+
6275
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: undefined });
6376
init(options);
6477

65-
expect(initAndBindSpy).toHaveBeenCalledTimes(1);
66-
67-
const optionsPassed = initAndBindSpy.mock.calls[0]?.[1];
68-
expect(optionsPassed?.integrations.length).toBeGreaterThan(0);
78+
expect(spyGetIntegrationsToSetup).toHaveBeenCalledTimes(1);
79+
expect(spyGetIntegrationsToSetup).toHaveBeenCalledWith(
80+
expect.objectContaining(options),
81+
expect.arrayContaining([expect.objectContaining({ name: 'InboundFilters' })]),
82+
);
6983
});
7084

71-
test("doesn't install default integrations if told not to", () => {
72-
const DEFAULT_INTEGRATIONS: Integration[] = [
73-
new MockIntegration('MockIntegration 0.3'),
74-
new MockIntegration('MockIntegration 0.4'),
75-
];
85+
test("doesn't install any default integrations if told not to", () => {
7686
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: false });
77-
init(options);
87+
const client = init(options);
7888

79-
expect(DEFAULT_INTEGRATIONS[0]!.setupOnce as Mock).toHaveBeenCalledTimes(0);
80-
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(0);
89+
expect(client?.['_integrations']).toEqual({});
8190
});
8291

8392
it('installs merged default integrations, with overrides provided through options', () => {
@@ -137,7 +146,7 @@ describe('init', () => {
137146
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true });
138147
Object.defineProperty(WINDOW, 'nw', { value: undefined, writable: true });
139148
Object.defineProperty(WINDOW, 'window', { value: WINDOW, writable: true });
140-
vi.clearAllMocks();
149+
vi.restoreAllMocks();
141150
});
142151

143152
it('logs a browser extension error if executed inside a Chrome extension', () => {
@@ -154,8 +163,6 @@ describe('init', () => {
154163
expect(consoleErrorSpy).toHaveBeenCalledWith(
155164
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
156165
);
157-
158-
consoleErrorSpy.mockRestore();
159166
});
160167

161168
it('logs a browser extension error if executed inside a Firefox/Safari extension', () => {
@@ -169,8 +176,6 @@ describe('init', () => {
169176
expect(consoleErrorSpy).toHaveBeenCalledWith(
170177
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
171178
);
172-
173-
consoleErrorSpy.mockRestore();
174179
});
175180

176181
it.each(['chrome-extension', 'moz-extension', 'ms-browser-extension', 'safari-web-extension'])(
@@ -249,6 +254,32 @@ describe('init', () => {
249254
});
250255
});
251256

257+
describe('initWithDefaultIntegrations', () => {
258+
afterEach(() => {
259+
vi.restoreAllMocks();
260+
});
261+
262+
test('installs with provided getDefaultIntegrations function', () => {
263+
const integration1 = new MockIntegration(SentryCore.uuid4());
264+
const integration2 = new MockIntegration(SentryCore.uuid4());
265+
const getDefaultIntegrations = vi.fn(() => [integration1, integration2]);
266+
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN });
267+
268+
const client = initWithDefaultIntegrations(options, getDefaultIntegrations);
269+
270+
expect(getDefaultIntegrations).toHaveBeenCalledTimes(1);
271+
expect(getDefaultIntegrations).toHaveBeenCalledWith(options);
272+
273+
expect(client).toBeDefined();
274+
expect(client?.['_integrations']).toEqual({
275+
[integration1.name]: integration1,
276+
[integration2.name]: integration2,
277+
});
278+
expect(integration1.setupOnce).toHaveBeenCalledTimes(1);
279+
expect(integration2.setupOnce).toHaveBeenCalledTimes(1);
280+
});
281+
});
282+
252283
describe('applyDefaultOptions', () => {
253284
test('it works with empty options', () => {
254285
const options = {};

0 commit comments

Comments
 (0)
0