@@ -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
} ) ;
@@ -491,8 +478,10 @@ describe('tracingHandler', () => {
491
478
const options = getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ;
492
479
// eslint-disable-next-line deprecation/deprecation
493
480
const hub = new Hub ( new NodeClient ( options ) ) ;
481
+
482
+ jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
494
483
// eslint-disable-next-line deprecation/deprecation
495
- makeMain ( hub ) ;
484
+ jest . spyOn ( sentryCore , 'getCurrentScope' ) . mockImplementation ( ( ) => hub . getScope ( ) ) ;
496
485
497
486
sentryTracingMiddleware ( req , res , next ) ;
498
487
@@ -548,40 +537,31 @@ describe('errorHandler()', () => {
548
537
const scope = getCurrentScope ( ) ;
549
538
// eslint-disable-next-line deprecation/deprecation
550
539
const hub = new Hub ( client ) ;
551
- // eslint-disable-next-line deprecation/deprecation
552
- makeMain ( hub ) ;
553
540
554
541
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
542
+ jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
555
543
556
- scope . setRequestSession ( { status : 'ok' } ) ;
544
+ scope ? .setRequestSession ( { status : 'ok' } ) ;
557
545
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
558
- const requestSession = scope . getRequestSession ( ) ;
546
+ const requestSession = scope ? .getRequestSession ( ) ;
559
547
expect ( requestSession ) . toEqual ( { status : 'ok' } ) ;
560
548
} ) ;
561
549
562
- it ( 'autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash' , done => {
550
+ it ( 'autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash' , ( ) => {
563
551
const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '3.3' } ) ;
564
552
client = new NodeClient ( options ) ;
565
553
554
+ const scope = getCurrentScope ( ) ;
566
555
// eslint-disable-next-line deprecation/deprecation
567
556
const hub = new Hub ( client ) ;
568
- // eslint-disable-next-line deprecation/deprecation
569
- makeMain ( hub ) ;
570
557
571
558
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
559
+ jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
572
560
573
- getCurrentScope ( ) . setRequestSession ( { status : 'ok' } ) ;
574
-
575
- let scope : Scope ;
576
- sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
577
- scope = getCurrentScope ( ) ;
578
- return next ( ) ;
579
- } ) ;
580
-
581
- setImmediate ( ( ) => {
582
- expect ( scope . getRequestSession ( ) ) . toEqual ( { status : 'ok' } ) ;
583
- done ( ) ;
584
- } ) ;
561
+ scope ?. setRequestSession ( { status : 'ok' } ) ;
562
+ sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
563
+ const requestSession = scope ?. getRequestSession ( ) ;
564
+ expect ( requestSession ) . toEqual ( { status : 'ok' } ) ;
585
565
} ) ;
586
566
587
567
it ( 'when autoSessionTracking is enabled, should set requestSession status to Crashed when an unhandled error occurs within the bounds of a request' , ( ) => {
@@ -599,16 +579,16 @@ describe('errorHandler()', () => {
599
579
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
600
580
601
581
hub . run ( ( ) => {
602
- scope . setRequestSession ( { status : 'ok' } ) ;
582
+ scope ? .setRequestSession ( { status : 'ok' } ) ;
603
583
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
604
584
const scope = getCurrentScope ( ) ;
605
- const requestSession = scope . getRequestSession ( ) ;
585
+ const requestSession = scope ? .getRequestSession ( ) ;
606
586
expect ( requestSession ) . toEqual ( { status : 'crashed' } ) ;
607
587
} ) ;
608
588
} ) ;
609
589
} ) ;
610
590
611
- it ( 'when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request' , done => {
591
+ it ( 'when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request' , ( ) => {
612
592
const options = getDefaultNodeClientOptions ( { autoSessionTracking : true , release : '2.2' } ) ;
613
593
client = new NodeClient ( options ) ;
614
594
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
@@ -617,21 +597,13 @@ describe('errorHandler()', () => {
617
597
const scope = new ScopeClass ( ) ;
618
598
// eslint-disable-next-line deprecation/deprecation
619
599
const hub = new Hub ( client , scope ) ;
620
- // eslint-disable-next-line deprecation/deprecation
621
- makeMain ( hub ) ;
622
600
623
601
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
602
+ jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
624
603
625
- let currentScope : Scope ;
626
- sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
627
- currentScope = getCurrentScope ( ) ;
628
- return next ( ) ;
629
- } ) ;
630
-
631
- setImmediate ( ( ) => {
632
- expect ( currentScope . getRequestSession ( ) ) . toEqual ( undefined ) ;
633
- done ( ) ;
634
- } ) ;
604
+ sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
605
+ const requestSession = scope ?. getRequestSession ( ) ;
606
+ expect ( requestSession ) . toEqual ( undefined ) ;
635
607
} ) ;
636
608
637
609
it ( 'stores request in `sdkProcessingMetadata`' , ( ) => {
@@ -647,8 +619,9 @@ describe('errorHandler()', () => {
647
619
// `captureException` in order to examine the scope as it exists inside the `withScope` callback
648
620
// eslint-disable-next-line deprecation/deprecation
649
621
hub . captureException = function ( this : Hub , _exception : any ) {
650
- const scope = getIsolationScope ( ) ;
651
- expect ( scope . getScopeData ( ) . sdkProcessingMetadata . request ) . toEqual ( req ) ;
622
+ // eslint-disable-next-line deprecation/deprecation
623
+ const scope = this . getScope ( ) ;
624
+ expect ( ( scope as any ) . _sdkProcessingMetadata . request ) . toEqual ( req ) ;
652
625
} as any ;
653
626
654
627
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
0 commit comments