33
44import type { Client , Event , EventType } from '@sentry/types' ;
55
6+ import { getCurrentScope , makeMain } from '@sentry/core' ;
67import { Hub , Scope , getCurrentHub } from '../src' ;
78
89const 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