@@ -2,15 +2,15 @@ import * as http from 'http';
2
2
import * as sentryCore from '@sentry/core' ;
3
3
import {
4
4
Hub ,
5
- Scope as ScopeClass ,
6
5
Transaction ,
7
6
getClient ,
8
7
getCurrentScope ,
9
8
getIsolationScope ,
10
9
getMainCarrier ,
11
- makeMain ,
12
10
mergeScopeData ,
11
+ setCurrentClient ,
13
12
spanToJSON ,
13
+ withScope ,
14
14
} from '@sentry/core' ;
15
15
import type { Event , PropagationContext , Scope } from '@sentry/types' ;
16
16
import { SentryError } from '@sentry/utils' ;
@@ -21,10 +21,14 @@ import { getDefaultNodeClientOptions } from './helper/node-client-options';
21
21
22
22
describe ( 'requestHandler' , ( ) => {
23
23
beforeEach ( ( ) => {
24
+ getCurrentScope ( ) . clear ( ) ;
25
+ getIsolationScope ( ) . clear ( ) ;
26
+
24
27
// Ensure we reset a potentially set acs to use the default
25
28
const sentry = getMainCarrier ( ) . __SENTRY__ ;
26
29
if ( sentry ) {
27
30
sentry . acs = undefined ;
31
+ sentry . hub = undefined ;
28
32
}
29
33
} ) ;
30
34
@@ -38,7 +42,6 @@ describe('requestHandler', () => {
38
42
const sentryRequestMiddleware = requestHandler ( ) ;
39
43
40
44
let req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => undefined ;
41
- let client : NodeClient ;
42
45
43
46
function createNoOpSpy ( ) {
44
47
const noop = { noop : ( ) => undefined } ; // this is wrapped in an object so jest can spy on it
@@ -63,11 +66,8 @@ describe('requestHandler', () => {
63
66
64
67
it ( 'autoSessionTracking is enabled, sets requestSession status to ok, when handling a request' , done => {
65
68
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 ) ;
71
71
72
72
let isolationScope : Scope ;
73
73
sentryRequestMiddleware ( req , res , ( ) => {
@@ -83,11 +83,8 @@ describe('requestHandler', () => {
83
83
84
84
it ( 'autoSessionTracking is disabled, does not set requestSession, when handling a request' , done => {
85
85
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 ) ;
91
88
92
89
let isolationScope : Scope ;
93
90
sentryRequestMiddleware ( req , res , ( ) => {
@@ -103,11 +100,8 @@ describe('requestHandler', () => {
103
100
104
101
it ( 'autoSessionTracking is enabled, calls _captureRequestSession, on response finish' , done => {
105
102
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 ) ;
111
105
112
106
const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
113
107
@@ -128,11 +122,8 @@ describe('requestHandler', () => {
128
122
129
123
it ( 'autoSessionTracking is disabled, does not call _captureRequestSession, on response finish' , done => {
130
124
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 ) ;
136
127
137
128
const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
138
129
@@ -179,10 +170,8 @@ describe('requestHandler', () => {
179
170
} ) ;
180
171
181
172
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 ) ;
186
175
187
176
const requestHandlerOptions = { include : { ip : false } } ;
188
177
const sentryRequestMiddleware = requestHandler ( requestHandlerOptions ) ;
@@ -208,6 +197,18 @@ describe('requestHandler', () => {
208
197
} ) ;
209
198
210
199
describe ( '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
+
211
212
const headers = { ears : 'furry' , nose : 'wet' , tongue : 'spotted' , cookie : 'favorite=zukes' } ;
212
213
const method = 'wagging' ;
213
214
const protocol = 'mutualsniffing' ;
@@ -218,18 +219,16 @@ describe('tracingHandler', () => {
218
219
219
220
const sentryTracingMiddleware = tracingHandler ( ) ;
220
221
221
- let hub : Hub , req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => undefined ;
222
+ let req : http . IncomingMessage , res : http . ServerResponse , next : ( ) => undefined ;
222
223
223
224
function createNoOpSpy ( ) {
224
225
const noop = { noop : ( ) => undefined } ; // this is wrapped in an object so jest can spy on it
225
226
return jest . spyOn ( noop , 'noop' ) as any ;
226
227
}
227
228
228
229
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 ) ;
233
232
234
233
req = {
235
234
headers,
@@ -349,12 +348,10 @@ describe('tracingHandler', () => {
349
348
it ( 'extracts request data for sampling context' , ( ) => {
350
349
const tracesSampler = jest . fn ( ) ;
351
350
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 ) ;
356
353
357
- hub . run ( ( ) => {
354
+ withScope ( ( ) => {
358
355
sentryTracingMiddleware ( req , res , next ) ;
359
356
360
357
expect ( tracesSampler ) . toHaveBeenCalledWith (
@@ -373,10 +370,8 @@ describe('tracingHandler', () => {
373
370
374
371
it ( 'puts its transaction on the scope' , ( ) => {
375
372
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 ) ;
380
375
381
376
sentryTracingMiddleware ( req , res , next ) ;
382
377
@@ -521,6 +516,18 @@ describe('tracingHandler', () => {
521
516
} ) ;
522
517
523
518
describe ( '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
+
524
531
const headers = { ears : 'furry' , nose : 'wet' , tongue : 'spotted' , cookie : 'favorite=zukes' } ;
525
532
const method = 'wagging' ;
526
533
const protocol = 'mutualsniffing' ;
@@ -561,10 +568,7 @@ describe('errorHandler()', () => {
561
568
// by the`requestHandler`)
562
569
client . initSessionFlusher ( ) ;
563
570
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 ) ;
568
572
569
573
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
570
574
@@ -585,11 +589,9 @@ describe('errorHandler()', () => {
585
589
it ( 'autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash' , done => {
586
590
const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '3.3' } ) ;
587
591
client = new NodeClient ( options ) ;
588
- // eslint-disable-next-line deprecation/deprecation
589
- const hub = new Hub ( client ) ;
592
+ setCurrentClient ( client ) ;
590
593
591
594
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
592
- jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
593
595
594
596
getIsolationScope ( ) . setRequestSession ( { status : 'ok' } ) ;
595
597
@@ -611,15 +613,12 @@ describe('errorHandler()', () => {
611
613
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
612
614
// by the`requestHandler`)
613
615
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 ) ;
619
618
620
619
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
621
620
622
- hub . run ( ( ) => {
621
+ withScope ( ( ) => {
623
622
getIsolationScope ( ) . setRequestSession ( { status : 'ok' } ) ;
624
623
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
625
624
expect ( getIsolationScope ( ) . getRequestSession ( ) ) . toEqual ( { status : 'crashed' } ) ;
@@ -633,11 +632,7 @@ describe('errorHandler()', () => {
633
632
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
634
633
// by the`requestHandler`)
635
634
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 ) ;
641
636
642
637
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
643
638
@@ -656,11 +651,7 @@ describe('errorHandler()', () => {
656
651
it ( 'stores request in `sdkProcessingMetadata`' , done => {
657
652
const options = getDefaultNodeClientOptions ( { } ) ;
658
653
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 ) ;
664
655
665
656
let isolationScope : Scope ;
666
657
sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
0 commit comments