Skip to content

Commit 061042a

Browse files
Lms24mydea
andauthored
fix(browser): Initialize default integration if defaultIntegrations: undefined (#13261)
If users or our higher-level SDKs pass `defaultIntegrations: undefined` to the Browser SDK's init options, it deactivates all default integrations. As per our docs, this should only happen if you explicitly pass `defaultIntegrations: false`. This PR fixes this by removing the `defaultIntegrations` key from the user options object before merging the options together. --------- Co-authored-by: Francesco Novy <francesco.novy@sentry.io>
1 parent 9ed2112 commit 061042a

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.size-limit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = [
2222
path: 'packages/browser/build/npm/esm/index.js',
2323
import: createImport('init', 'browserTracingIntegration', 'replayIntegration'),
2424
gzip: true,
25-
limit: '72 KB',
25+
limit: '73 KB',
2626
},
2727
{
2828
name: '@sentry/browser (incl. Tracing, Replay) - with treeshaking flags',

packages/browser/src/sdk.ts

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions {
5757
sendClientReports: true,
5858
};
5959

60+
// TODO: Instead of dropping just `defaultIntegrations`, we should simply
61+
// call `dropUndefinedKeys` on the entire `optionsArg`.
62+
// However, for this to work we need to adjust the `hasTracingEnabled()` logic
63+
// first as it differentiates between `undefined` and the key not being in the object.
64+
if (optionsArg.defaultIntegrations == null) {
65+
delete optionsArg.defaultIntegrations;
66+
}
67+
6068
return { ...defaultOptions, ...optionsArg };
6169
}
6270

packages/browser/test/sdk.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import type { Mock } from 'vitest';
77
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
88

9+
import * as SentryCore from '@sentry/core';
910
import { Scope, createTransport } from '@sentry/core';
1011
import type { Client, Integration } from '@sentry/types';
1112
import { resolvedSyncPromise } from '@sentry/utils';
@@ -79,6 +80,18 @@ describe('init', () => {
7980
expect(DEFAULT_INTEGRATIONS[1]!.setupOnce as Mock).toHaveBeenCalledTimes(1);
8081
});
8182

83+
it('installs default integrations if `defaultIntegrations: undefined`', () => {
84+
// @ts-expect-error this is fine for testing
85+
const initAndBindSpy = vi.spyOn(SentryCore, 'initAndBind').mockImplementationOnce(() => {});
86+
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: undefined });
87+
init(options);
88+
89+
expect(initAndBindSpy).toHaveBeenCalledTimes(1);
90+
91+
const optionsPassed = initAndBindSpy.mock.calls[0]?.[1];
92+
expect(optionsPassed?.integrations?.length).toBeGreaterThan(0);
93+
});
94+
8295
test("doesn't install default integrations if told not to", () => {
8396
const DEFAULT_INTEGRATIONS: Integration[] = [
8497
new MockIntegration('MockIntegration 0.3'),
@@ -150,6 +163,7 @@ describe('init', () => {
150163
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true });
151164
Object.defineProperty(WINDOW, 'nw', { value: undefined, writable: true });
152165
Object.defineProperty(WINDOW, 'window', { value: WINDOW, writable: true });
166+
vi.clearAllMocks();
153167
});
154168

155169
it('logs a browser extension error if executed inside a Chrome extension', () => {

0 commit comments

Comments
 (0)