Skip to content

Commit 7c8f841

Browse files
committed
add unit tests for deprecated API
1 parent fbe06f6 commit 7c8f841

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

packages/core/test/lib/exports.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ describe('session APIs', () => {
200200

201201
captureSession();
202202

203-
// this flag indicates the session was sent
203+
// this flag indicates the session was sent via BaseClient
204204
expect(session.init).toBe(false);
205205

206206
// session is still active and on the scope
207207
expect(session.status).toBe('ok');
208208
expect(getIsolationScope().getSession()).toBe(session);
209209
});
210210

211-
it('captures a session and ends it if requested', () => {
211+
it('captures a session and ends it if end is `true`', () => {
212212
const session = startSession({ release: '1.0.0' });
213213

214214
expect(session.status).toBe('ok');
@@ -217,7 +217,7 @@ describe('session APIs', () => {
217217

218218
captureSession(true);
219219

220-
// this flag indicates the session was sent
220+
// this flag indicates the session was sent via BaseClient
221221
expect(session.init).toBe(false);
222222

223223
// session is still active and on the scope

packages/hub/test/hub.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import type { Client, Event, EventType } from '@sentry/types';
55

6+
import { getCurrentScope, makeMain } from '@sentry/core';
67
import { Hub, Scope, getCurrentHub } from '../src';
78

89
const clientFn: any = jest.fn();
@@ -18,6 +19,7 @@ function makeClient() {
1819
getIntegration: jest.fn(),
1920
setupIntegrations: jest.fn(),
2021
captureMessage: jest.fn(),
22+
captureSession: jest.fn(),
2123
} as unknown as Client;
2224
}
2325

@@ -453,4 +455,102 @@ describe('Hub', () => {
453455
expect(hub.shouldSendDefaultPii()).toBe(true);
454456
});
455457
});
458+
459+
describe('session APIs', () => {
460+
beforeEach(() => {
461+
const testClient = makeClient();
462+
const hub = new Hub(testClient);
463+
makeMain(hub);
464+
});
465+
466+
describe('startSession', () => {
467+
it('starts a session', () => {
468+
const testClient = makeClient();
469+
const hub = new Hub(testClient);
470+
makeMain(hub);
471+
const session = hub.startSession();
472+
473+
expect(session).toMatchObject({
474+
status: 'ok',
475+
errors: 0,
476+
init: true,
477+
environment: 'production',
478+
ignoreDuration: false,
479+
sid: expect.any(String),
480+
did: undefined,
481+
timestamp: expect.any(Number),
482+
started: expect.any(Number),
483+
duration: expect.any(Number),
484+
toJSON: expect.any(Function),
485+
});
486+
});
487+
488+
it('ends a previously active session and removes it from the scope', () => {
489+
const testClient = makeClient();
490+
const hub = new Hub(testClient);
491+
makeMain(hub);
492+
493+
const session1 = hub.startSession();
494+
495+
expect(session1.status).toBe('ok');
496+
expect(getCurrentScope().getSession()).toBe(session1);
497+
498+
const session2 = hub.startSession();
499+
500+
expect(session2.status).toBe('ok');
501+
expect(session1.status).toBe('exited');
502+
expect(getCurrentHub().getScope().getSession()).toBe(session2);
503+
});
504+
});
505+
506+
describe('endSession', () => {
507+
it('ends a session and removes it from the scope', () => {
508+
const testClient = makeClient();
509+
const hub = new Hub(testClient);
510+
makeMain(hub);
511+
512+
const session = hub.startSession();
513+
514+
expect(session.status).toBe('ok');
515+
expect(getCurrentScope().getSession()).toBe(session);
516+
517+
hub.endSession();
518+
519+
expect(session.status).toBe('exited');
520+
expect(getCurrentHub().getScope().getSession()).toBe(undefined);
521+
});
522+
});
523+
524+
describe('captureSession', () => {
525+
it('captures a session without ending it by default', () => {
526+
const testClient = makeClient();
527+
const hub = new Hub(testClient);
528+
makeMain(hub);
529+
530+
const session = hub.startSession();
531+
532+
expect(session.status).toBe('ok');
533+
expect(getCurrentScope().getSession()).toBe(session);
534+
535+
hub.captureSession();
536+
537+
expect(testClient.captureSession).toHaveBeenCalledWith(expect.objectContaining({ status: 'ok' }));
538+
});
539+
540+
it('captures a session and ends it if end is `true`', () => {
541+
const testClient = makeClient();
542+
const hub = new Hub(testClient);
543+
makeMain(hub);
544+
545+
const session = hub.startSession();
546+
547+
expect(session.status).toBe('ok');
548+
expect(hub.getScope().getSession()).toBe(session);
549+
550+
hub.captureSession(true);
551+
552+
expect(testClient.captureSession).toHaveBeenCalledWith(expect.objectContaining({ status: 'exited' }));
553+
});
554+
});
555+
});
456556
});

0 commit comments

Comments
 (0)