Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/browser/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ describe('SentryBrowser initialization', () => {
delete global.SENTRY_RELEASE;
});

it('should use initialScope', () => {
init({ dsn, initialScope: { tags: { a: 'b' } } });
expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' });
});

it('should use initalScope Scope', () => {
const scope = new Scope();
scope.setTags({ a: 'b' });
init({ dsn, initialScope: scope });
expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' });
});

it('should use initalScope callback', () => {
init({
dsn,
initialScope: scope => {
scope.setTags({ a: 'b' });
return scope;
},
});
expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' });
});

it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => {
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
init({ dsn });
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function initAndBind<F extends Client, O extends Options>(clientClass: Cl
logger.enable();
}
const hub = getCurrentHub();
hub.getScope()?.update(options.initialScope);
const client = new clientClass(options);
hub.bindClient(client);
}
5 changes: 5 additions & 0 deletions packages/core/test/lib/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Scope } from '@sentry/hub';
import { Client, Integration } from '@sentry/types';

import { installedIntegrations } from '../../src/integration';
Expand All @@ -16,11 +17,15 @@ jest.mock('@sentry/hub', () => {
getCurrentHub(): {
bindClient(client: Client): boolean;
getClient(): boolean;
getScope(): Scope;
} {
return {
getClient(): boolean {
return false;
},
getScope(): Scope {
return new Scope();
},
bindClient(client: Client): boolean {
client.setupIntegrations();
return true;
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Breadcrumb, BreadcrumbHint } from './breadcrumb';
import { Event, EventHint } from './event';
import { Integration } from './integration';
import { LogLevel } from './loglevel';
import { CaptureContext } from './scope';
import { SdkMetadata } from './sdkmetadata';
import { SamplingContext } from './transaction';
import { Transport, TransportClass, TransportOptions } from './transport';
Expand Down Expand Up @@ -124,6 +125,11 @@ export interface Options {
*/
autoSessionTracking?: boolean;

/**
* Set data to the inital scope
*/
initialScope?: CaptureContext;

/**
* Set of metadata about the SDK that can be internally used to enhance envelopes and events,
* and provide additional data about every request.
Expand Down