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