diff --git a/packages/core/src/baseclient.ts b/packages/core/src/baseclient.ts index 9418ec21a130..7ae94741b16c 100644 --- a/packages/core/src/baseclient.ts +++ b/packages/core/src/baseclient.ts @@ -152,6 +152,11 @@ export abstract class BaseClient implement * @inheritDoc */ public captureSession(session: Session): void { + if (!this._isEnabled()) { + logger.warn('SDK not enabled, will not capture session.'); + return; + } + if (!(typeof session.release === 'string')) { logger.warn('Discarded session because of missing or non-string release'); } else { @@ -485,7 +490,7 @@ export abstract class BaseClient implement const { beforeSend, sampleRate } = this.getOptions(); if (!this._isEnabled()) { - return SyncPromise.reject(new SentryError('SDK not enabled, will not send event.')); + return SyncPromise.reject(new SentryError('SDK not enabled, will not capture event.')); } const isTransaction = event.type === 'transaction'; diff --git a/packages/core/test/lib/base.test.ts b/packages/core/test/lib/base.test.ts index 638f3127a076..ffc0ee634c29 100644 --- a/packages/core/test/lib/base.test.ts +++ b/packages/core/test/lib/base.test.ts @@ -1,4 +1,4 @@ -import { Hub, Scope } from '@sentry/hub'; +import { Hub, Scope, Session } from '@sentry/hub'; import { Event, Severity, Span } from '@sentry/types'; import { logger, SentryError, SyncPromise } from '@sentry/utils'; @@ -964,4 +964,22 @@ describe('BaseClient', () => { ]); }); }); + + describe('captureSession()', () => { + test('sends sessions to the backend', () => { + expect.assertions(1); + const client = new TestClient({ dsn: PUBLIC_DSN }); + const session = new Session({ release: 'test' }); + client.captureSession(session); + expect(TestBackend.instance!.session).toEqual(session); + }); + + test('skips when disabled', () => { + expect.assertions(1); + const client = new TestClient({ enabled: false, dsn: PUBLIC_DSN }); + const session = new Session({ release: 'test' }); + client.captureSession(session); + expect(TestBackend.instance!.session).toBeUndefined(); + }); + }); }); diff --git a/packages/core/test/mocks/backend.ts b/packages/core/test/mocks/backend.ts index d4929d9d306a..7d21f4bfa323 100644 --- a/packages/core/test/mocks/backend.ts +++ b/packages/core/test/mocks/backend.ts @@ -1,3 +1,4 @@ +import { Session } from '@sentry/hub'; import { Event, Options, Severity, Transport } from '@sentry/types'; import { SyncPromise } from '@sentry/utils'; @@ -14,6 +15,7 @@ export class TestBackend extends BaseBackend { public static sendEventCalled?: (event: Event) => void; public event?: Event; + public session?: Session; public constructor(protected readonly _options: TestOptions) { super(_options); @@ -50,6 +52,10 @@ export class TestBackend extends BaseBackend { TestBackend.sendEventCalled && TestBackend.sendEventCalled(event); } + public sendSession(session: Session): void { + this.session = session; + } + protected _setupTransport(): Transport { if (!this._options.dsn) { // We return the noop transport here in case there is no Dsn.