@@ -12,7 +12,7 @@ import {
12
12
mergeScopeData ,
13
13
spanToJSON ,
14
14
} from '@sentry/core' ;
15
- import type { Event , PropagationContext } from '@sentry/types' ;
15
+ import type { Event , PropagationContext , Scope } from '@sentry/types' ;
16
16
import { SentryError } from '@sentry/utils' ;
17
17
18
18
import { NodeClient } from '../src/client' ;
@@ -61,18 +61,24 @@ describe('requestHandler', () => {
61
61
jest . restoreAllMocks ( ) ;
62
62
} ) ;
63
63
64
- it ( 'autoSessionTracking is enabled, sets requestSession status to ok, when handling a request' , ( ) => {
64
+ it ( 'autoSessionTracking is enabled, sets requestSession status to ok, when handling a request' , done => {
65
65
const options = getDefaultNodeClientOptions ( { autoSessionTracking : true , release : '1.2' } ) ;
66
66
client = new NodeClient ( options ) ;
67
67
// eslint-disable-next-line deprecation/deprecation
68
68
const hub = new Hub ( client ) ;
69
69
// eslint-disable-next-line deprecation/deprecation
70
70
makeMain ( hub ) ;
71
71
72
- sentryRequestMiddleware ( req , res , next ) ;
72
+ let scope : Scope ;
73
+ sentryRequestMiddleware ( req , res , ( ) => {
74
+ scope = getCurrentScope ( ) ;
75
+ return next ( ) ;
76
+ } ) ;
73
77
74
- const isolationScope = getIsolationScope ( ) ;
75
- expect ( isolationScope . getRequestSession ( ) ) . toEqual ( { status : 'ok' } ) ;
78
+ setImmediate ( ( ) => {
79
+ expect ( scope . getRequestSession ( ) ) . toEqual ( { status : 'ok' } ) ;
80
+ done ( ) ;
81
+ } ) ;
76
82
} ) ;
77
83
78
84
it ( 'autoSessionTracking is disabled, does not set requestSession, when handling a request' , ( ) => {
@@ -83,13 +89,18 @@ describe('requestHandler', () => {
83
89
// eslint-disable-next-line deprecation/deprecation
84
90
makeMain ( hub ) ;
85
91
86
- sentryRequestMiddleware ( req , res , next ) ;
92
+ let scope : Scope ;
93
+ sentryRequestMiddleware ( req , res , ( ) => {
94
+ scope = getCurrentScope ( ) ;
95
+ return next ( ) ;
96
+ } ) ;
87
97
88
- const scope = getCurrentScope ( ) ;
89
- expect ( scope ?. getRequestSession ( ) ) . toBeUndefined ( ) ;
98
+ setImmediate ( ( ) => {
99
+ expect ( scope . getRequestSession ( ) ) . toBeUndefined ( ) ;
100
+ } ) ;
90
101
} ) ;
91
102
92
- it ( 'autoSessionTracking is enabled, calls _captureRequestSession, on response finish' , done => {
103
+ it ( 'autoSessionTracking is enabled, calls _captureRequestSession, on response finish xxx ' , done => {
93
104
const options = getDefaultNodeClientOptions ( { autoSessionTracking : true , release : '1.2' } ) ;
94
105
client = new NodeClient ( options ) ;
95
106
// eslint-disable-next-line deprecation/deprecation
@@ -99,13 +110,16 @@ describe('requestHandler', () => {
99
110
100
111
const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
101
112
102
- sentryRequestMiddleware ( req , res , next ) ;
113
+ let scope : Scope ;
114
+ sentryRequestMiddleware ( req , res , ( ) => {
115
+ scope = getCurrentScope ( ) ;
116
+ return next ( ) ;
117
+ } ) ;
103
118
104
- const isolationScope = getIsolationScope ( ) ;
105
119
res . emit ( 'finish' ) ;
106
120
107
121
setImmediate ( ( ) => {
108
- expect ( isolationScope . getRequestSession ( ) ) . toEqual ( { status : 'ok' } ) ;
122
+ expect ( scope . getRequestSession ( ) ) . toEqual ( { status : 'ok' } ) ;
109
123
expect ( captureRequestSession ) . toHaveBeenCalled ( ) ;
110
124
done ( ) ;
111
125
} ) ;
@@ -122,11 +136,10 @@ describe('requestHandler', () => {
122
136
const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
123
137
124
138
sentryRequestMiddleware ( req , res , next ) ;
125
- const scope = getCurrentScope ( ) ;
126
139
res . emit ( 'finish' ) ;
127
140
128
141
setImmediate ( ( ) => {
129
- expect ( scope ? .getRequestSession ( ) ) . toBeUndefined ( ) ;
142
+ expect ( getCurrentScope ( ) . getRequestSession ( ) ) . toBeUndefined ( ) ;
130
143
expect ( captureRequestSession ) . not . toHaveBeenCalled ( ) ;
131
144
done ( ) ;
132
145
} ) ;
@@ -479,10 +492,8 @@ describe('tracingHandler', () => {
479
492
const options = getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ;
480
493
// eslint-disable-next-line deprecation/deprecation
481
494
const hub = new Hub ( new NodeClient ( options ) ) ;
482
-
483
- jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
484
495
// eslint-disable-next-line deprecation/deprecation
485
- jest . spyOn ( sentryCore , 'getCurrentScope' ) . mockImplementation ( ( ) => hub . getScope ( ) ) ;
496
+ makeMain ( hub ) ;
486
497
487
498
sentryTracingMiddleware ( req , res , next ) ;
488
499
@@ -538,31 +549,40 @@ describe('errorHandler()', () => {
538
549
const scope = getCurrentScope ( ) ;
539
550
// eslint-disable-next-line deprecation/deprecation
540
551
const hub = new Hub ( client ) ;
552
+ // eslint-disable-next-line deprecation/deprecation
553
+ makeMain ( hub ) ;
541
554
542
555
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
543
- jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
544
556
545
- scope ? .setRequestSession ( { status : 'ok' } ) ;
557
+ scope . setRequestSession ( { status : 'ok' } ) ;
546
558
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
547
- const requestSession = scope ? .getRequestSession ( ) ;
559
+ const requestSession = scope . getRequestSession ( ) ;
548
560
expect ( requestSession ) . toEqual ( { status : 'ok' } ) ;
549
561
} ) ;
550
562
551
- it ( 'autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash' , ( ) => {
563
+ it ( 'autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash' , done => {
552
564
const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '3.3' } ) ;
553
565
client = new NodeClient ( options ) ;
554
566
555
- const scope = getCurrentScope ( ) ;
556
567
// eslint-disable-next-line deprecation/deprecation
557
568
const hub = new Hub ( client ) ;
569
+ // eslint-disable-next-line deprecation/deprecation
570
+ makeMain ( hub ) ;
558
571
559
572
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
560
- jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
561
573
562
- scope ?. setRequestSession ( { status : 'ok' } ) ;
563
- sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
564
- const requestSession = scope ?. getRequestSession ( ) ;
565
- expect ( requestSession ) . toEqual ( { status : 'ok' } ) ;
574
+ getCurrentScope ( ) . setRequestSession ( { status : 'ok' } ) ;
575
+
576
+ let scope : Scope ;
577
+ sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
578
+ scope = getCurrentScope ( ) ;
579
+ return next ( ) ;
580
+ } ) ;
581
+
582
+ setImmediate ( ( ) => {
583
+ expect ( scope . getRequestSession ( ) ) . toEqual ( { status : 'ok' } ) ;
584
+ done ( ) ;
585
+ } ) ;
566
586
} ) ;
567
587
568
588
it ( 'when autoSessionTracking is enabled, should set requestSession status to Crashed when an unhandled error occurs within the bounds of a request' , ( ) => {
@@ -580,16 +600,16 @@ describe('errorHandler()', () => {
580
600
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
581
601
582
602
hub . run ( ( ) => {
583
- scope ? .setRequestSession ( { status : 'ok' } ) ;
603
+ scope . setRequestSession ( { status : 'ok' } ) ;
584
604
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
585
605
const scope = getCurrentScope ( ) ;
586
- const requestSession = scope ? .getRequestSession ( ) ;
606
+ const requestSession = scope . getRequestSession ( ) ;
587
607
expect ( requestSession ) . toEqual ( { status : 'crashed' } ) ;
588
608
} ) ;
589
609
} ) ;
590
610
} ) ;
591
611
592
- it ( 'when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request' , ( ) => {
612
+ it ( 'when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request' , done => {
593
613
const options = getDefaultNodeClientOptions ( { autoSessionTracking : true , release : '2.2' } ) ;
594
614
client = new NodeClient ( options ) ;
595
615
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
@@ -598,13 +618,21 @@ describe('errorHandler()', () => {
598
618
const scope = new ScopeClass ( ) ;
599
619
// eslint-disable-next-line deprecation/deprecation
600
620
const hub = new Hub ( client , scope ) ;
621
+ // eslint-disable-next-line deprecation/deprecation
622
+ makeMain ( hub ) ;
601
623
602
624
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
603
- jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
604
625
605
- sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
606
- const requestSession = scope ?. getRequestSession ( ) ;
607
- expect ( requestSession ) . toEqual ( undefined ) ;
626
+ let currentScope : Scope ;
627
+ sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
628
+ currentScope = getCurrentScope ( ) ;
629
+ return next ( ) ;
630
+ } ) ;
631
+
632
+ setImmediate ( ( ) => {
633
+ expect ( currentScope . getRequestSession ( ) ) . toEqual ( undefined ) ;
634
+ done ( ) ;
635
+ } ) ;
608
636
} ) ;
609
637
610
638
it ( 'stores request in `sdkProcessingMetadata`' , ( ) => {
@@ -620,9 +648,8 @@ describe('errorHandler()', () => {
620
648
// `captureException` in order to examine the scope as it exists inside the `withScope` callback
621
649
// eslint-disable-next-line deprecation/deprecation
622
650
hub . captureException = function ( this : Hub , _exception : any ) {
623
- // eslint-disable-next-line deprecation/deprecation
624
- const scope = this . getScope ( ) ;
625
- expect ( ( scope as any ) . _sdkProcessingMetadata . request ) . toEqual ( req ) ;
651
+ const scope = getIsolationScope ( ) ;
652
+ expect ( scope . getScopeData ( ) . sdkProcessingMetadata . request ) . toEqual ( req ) ;
626
653
} as any ;
627
654
628
655
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
0 commit comments