3
3
4
4
import type { Client , Event , EventType } from '@sentry/types' ;
5
5
6
+ import { getCurrentScope , makeMain } from '@sentry/core' ;
6
7
import { Hub , Scope , getCurrentHub } from '../src' ;
7
8
8
9
const clientFn : any = jest . fn ( ) ;
@@ -18,6 +19,7 @@ function makeClient() {
18
19
getIntegration : jest . fn ( ) ,
19
20
setupIntegrations : jest . fn ( ) ,
20
21
captureMessage : jest . fn ( ) ,
22
+ captureSession : jest . fn ( ) ,
21
23
} as unknown as Client ;
22
24
}
23
25
@@ -453,4 +455,102 @@ describe('Hub', () => {
453
455
expect ( hub . shouldSendDefaultPii ( ) ) . toBe ( true ) ;
454
456
} ) ;
455
457
} ) ;
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
+ } ) ;
456
556
} ) ;
0 commit comments