@@ -2,15 +2,15 @@ import * as http from 'http';
22import * as sentryCore from '@sentry/core' ;
33import {
44 Hub ,
5- Scope as ScopeClass ,
65 Transaction ,
76 getClient ,
87 getCurrentScope ,
98 getIsolationScope ,
109 getMainCarrier ,
11- makeMain ,
1210 mergeScopeData ,
11+ setCurrentClient ,
1312 spanToJSON ,
13+ withScope ,
1414} from '@sentry/core' ;
1515import type { Event , PropagationContext , Scope } from '@sentry/types' ;
1616import { SentryError } from '@sentry/utils' ;
@@ -21,10 +21,14 @@ import { getDefaultNodeClientOptions } from './helper/node-client-options';
2121
2222describe ( 'requestHandler' , ( ) => {
2323 beforeEach ( ( ) => {
24+ getCurrentScope ( ) . clear ( ) ;
25+ getIsolationScope ( ) . clear ( ) ;
26+
2427 // Ensure we reset a potentially set acs to use the default
2528 const sentry = getMainCarrier ( ) . __SENTRY__ ;
2629 if ( sentry ) {
2730 sentry . acs = undefined ;
31+ sentry . hub = undefined ;
2832 }
2933 } ) ;
3034
@@ -38,7 +42,6 @@ describe('requestHandler', () => {
3842 const sentryRequestMiddleware = requestHandler ( ) ;
3943
4044 let req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => undefined ;
41- let client : NodeClient ;
4245
4346 function createNoOpSpy ( ) {
4447 const noop = { noop : ( ) => undefined } ; // this is wrapped in an object so jest can spy on it
@@ -63,11 +66,8 @@ describe('requestHandler', () => {
6366
6467 it ( 'autoSessionTracking is enabled, sets requestSession status to ok, when handling a request' , done => {
6568 const options = getDefaultNodeClientOptions ( { autoSessionTracking : true , release : '1.2' } ) ;
66- client = new NodeClient ( options ) ;
67- // eslint-disable-next-line deprecation/deprecation
68- const hub = new Hub ( client ) ;
69- // eslint-disable-next-line deprecation/deprecation
70- makeMain ( hub ) ;
69+ const client = new NodeClient ( options ) ;
70+ setCurrentClient ( client ) ;
7171
7272 let isolationScope : Scope ;
7373 sentryRequestMiddleware ( req , res , ( ) => {
@@ -83,11 +83,8 @@ describe('requestHandler', () => {
8383
8484 it ( 'autoSessionTracking is disabled, does not set requestSession, when handling a request' , done => {
8585 const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '1.2' } ) ;
86- client = new NodeClient ( options ) ;
87- // eslint-disable-next-line deprecation/deprecation
88- const hub = new Hub ( client ) ;
89- // eslint-disable-next-line deprecation/deprecation
90- makeMain ( hub ) ;
86+ const client = new NodeClient ( options ) ;
87+ setCurrentClient ( client ) ;
9188
9289 let isolationScope : Scope ;
9390 sentryRequestMiddleware ( req , res , ( ) => {
@@ -103,11 +100,8 @@ describe('requestHandler', () => {
103100
104101 it ( 'autoSessionTracking is enabled, calls _captureRequestSession, on response finish' , done => {
105102 const options = getDefaultNodeClientOptions ( { autoSessionTracking : true , release : '1.2' } ) ;
106- client = new NodeClient ( options ) ;
107- // eslint-disable-next-line deprecation/deprecation
108- const hub = new Hub ( client ) ;
109- // eslint-disable-next-line deprecation/deprecation
110- makeMain ( hub ) ;
103+ const client = new NodeClient ( options ) ;
104+ setCurrentClient ( client ) ;
111105
112106 const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
113107
@@ -128,11 +122,8 @@ describe('requestHandler', () => {
128122
129123 it ( 'autoSessionTracking is disabled, does not call _captureRequestSession, on response finish' , done => {
130124 const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '1.2' } ) ;
131- client = new NodeClient ( options ) ;
132- // eslint-disable-next-line deprecation/deprecation
133- const hub = new Hub ( client ) ;
134- // eslint-disable-next-line deprecation/deprecation
135- makeMain ( hub ) ;
125+ const client = new NodeClient ( options ) ;
126+ setCurrentClient ( client ) ;
136127
137128 const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
138129
@@ -179,10 +170,8 @@ describe('requestHandler', () => {
179170 } ) ;
180171
181172 it ( 'stores request and request data options in `sdkProcessingMetadata`' , done => {
182- // eslint-disable-next-line deprecation/deprecation
183- const hub = new Hub ( new NodeClient ( getDefaultNodeClientOptions ( ) ) ) ;
184- // eslint-disable-next-line deprecation/deprecation
185- makeMain ( hub ) ;
173+ const client = new NodeClient ( getDefaultNodeClientOptions ( ) ) ;
174+ setCurrentClient ( client ) ;
186175
187176 const requestHandlerOptions = { include : { ip : false } } ;
188177 const sentryRequestMiddleware = requestHandler ( requestHandlerOptions ) ;
@@ -208,6 +197,18 @@ describe('requestHandler', () => {
208197} ) ;
209198
210199describe ( 'tracingHandler' , ( ) => {
200+ beforeEach ( ( ) => {
201+ getCurrentScope ( ) . clear ( ) ;
202+ getIsolationScope ( ) . clear ( ) ;
203+
204+ // Ensure we reset a potentially set acs to use the default
205+ const sentry = getMainCarrier ( ) . __SENTRY__ ;
206+ if ( sentry ) {
207+ sentry . acs = undefined ;
208+ sentry . hub = undefined ;
209+ }
210+ } ) ;
211+
211212 const headers = { ears : 'furry' , nose : 'wet' , tongue : 'spotted' , cookie : 'favorite=zukes' } ;
212213 const method = 'wagging' ;
213214 const protocol = 'mutualsniffing' ;
@@ -218,18 +219,16 @@ describe('tracingHandler', () => {
218219
219220 const sentryTracingMiddleware = tracingHandler ( ) ;
220221
221- let hub : Hub , req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => undefined ;
222+ let req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => undefined ;
222223
223224 function createNoOpSpy ( ) {
224225 const noop = { noop : ( ) => undefined } ; // this is wrapped in an object so jest can spy on it
225226 return jest . spyOn ( noop , 'noop' ) as any ;
226227 }
227228
228229 beforeEach ( ( ) => {
229- // eslint-disable-next-line deprecation/deprecation
230- hub = new Hub ( new NodeClient ( getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ) ) ;
231- // eslint-disable-next-line deprecation/deprecation
232- makeMain ( hub ) ;
230+ const client = new NodeClient ( getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ) ;
231+ setCurrentClient ( client ) ;
233232
234233 req = {
235234 headers,
@@ -349,12 +348,10 @@ describe('tracingHandler', () => {
349348 it ( 'extracts request data for sampling context' , ( ) => {
350349 const tracesSampler = jest . fn ( ) ;
351350 const options = getDefaultNodeClientOptions ( { tracesSampler } ) ;
352- // eslint-disable-next-line deprecation/deprecation
353- const hub = new Hub ( new NodeClient ( options ) ) ;
354- // eslint-disable-next-line deprecation/deprecation
355- makeMain ( hub ) ;
351+ const client = new NodeClient ( options ) ;
352+ setCurrentClient ( client ) ;
356353
357- hub . run ( ( ) => {
354+ withScope ( ( ) => {
358355 sentryTracingMiddleware ( req , res , next ) ;
359356
360357 expect ( tracesSampler ) . toHaveBeenCalledWith (
@@ -373,10 +370,8 @@ describe('tracingHandler', () => {
373370
374371 it ( 'puts its transaction on the scope' , ( ) => {
375372 const options = getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ;
376- // eslint-disable-next-line deprecation/deprecation
377- const hub = new Hub ( new NodeClient ( options ) ) ;
378- // eslint-disable-next-line deprecation/deprecation
379- makeMain ( hub ) ;
373+ const client = new NodeClient ( options ) ;
374+ setCurrentClient ( client ) ;
380375
381376 sentryTracingMiddleware ( req , res , next ) ;
382377
@@ -521,6 +516,18 @@ describe('tracingHandler', () => {
521516} ) ;
522517
523518describe ( 'errorHandler()' , ( ) => {
519+ beforeEach ( ( ) => {
520+ getCurrentScope ( ) . clear ( ) ;
521+ getIsolationScope ( ) . clear ( ) ;
522+
523+ // Ensure we reset a potentially set acs to use the default
524+ const sentry = getMainCarrier ( ) . __SENTRY__ ;
525+ if ( sentry ) {
526+ sentry . acs = undefined ;
527+ sentry . hub = undefined ;
528+ }
529+ } ) ;
530+
524531 const headers = { ears : 'furry' , nose : 'wet' , tongue : 'spotted' , cookie : 'favorite=zukes' } ;
525532 const method = 'wagging' ;
526533 const protocol = 'mutualsniffing' ;
@@ -561,10 +568,7 @@ describe('errorHandler()', () => {
561568 // by the`requestHandler`)
562569 client . initSessionFlusher ( ) ;
563570
564- // eslint-disable-next-line deprecation/deprecation
565- const hub = new Hub ( client ) ;
566- // eslint-disable-next-line deprecation/deprecation
567- makeMain ( hub ) ;
571+ setCurrentClient ( client ) ;
568572
569573 jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
570574
@@ -585,11 +589,9 @@ describe('errorHandler()', () => {
585589 it ( 'autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash' , done => {
586590 const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '3.3' } ) ;
587591 client = new NodeClient ( options ) ;
588- // eslint-disable-next-line deprecation/deprecation
589- const hub = new Hub ( client ) ;
592+ setCurrentClient ( client ) ;
590593
591594 jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
592- jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
593595
594596 getIsolationScope ( ) . setRequestSession ( { status : 'ok' } ) ;
595597
@@ -611,15 +613,12 @@ describe('errorHandler()', () => {
611613 // It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
612614 // by the`requestHandler`)
613615 client . initSessionFlusher ( ) ;
614- const scope = new ScopeClass ( ) ;
615- // eslint-disable-next-line deprecation/deprecation
616- const hub = new Hub ( client , scope ) ;
617- // eslint-disable-next-line deprecation/deprecation
618- makeMain ( hub ) ;
616+
617+ setCurrentClient ( client ) ;
619618
620619 jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
621620
622- hub . run ( ( ) => {
621+ withScope ( ( ) => {
623622 getIsolationScope ( ) . setRequestSession ( { status : 'ok' } ) ;
624623 sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
625624 expect ( getIsolationScope ( ) . getRequestSession ( ) ) . toEqual ( { status : 'crashed' } ) ;
@@ -633,11 +632,7 @@ describe('errorHandler()', () => {
633632 // It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
634633 // by the`requestHandler`)
635634 client . initSessionFlusher ( ) ;
636- const scope = new ScopeClass ( ) ;
637- // eslint-disable-next-line deprecation/deprecation
638- const hub = new Hub ( client , scope ) ;
639- // eslint-disable-next-line deprecation/deprecation
640- makeMain ( hub ) ;
635+ setCurrentClient ( client ) ;
641636
642637 jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
643638
@@ -656,11 +651,7 @@ describe('errorHandler()', () => {
656651 it ( 'stores request in `sdkProcessingMetadata`' , done => {
657652 const options = getDefaultNodeClientOptions ( { } ) ;
658653 client = new NodeClient ( options ) ;
659-
660- // eslint-disable-next-line deprecation/deprecation
661- const hub = new Hub ( client ) ;
662- // eslint-disable-next-line deprecation/deprecation
663- makeMain ( hub ) ;
654+ setCurrentClient ( client ) ;
664655
665656 let isolationScope : Scope ;
666657 sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
0 commit comments