1
+ import type { Hub } from '@sentry/core' ;
1
2
import * as sentryCore from '@sentry/core' ;
2
- import { Transaction } from '@sentry/core' ;
3
+ import { setAsyncContextStrategy , Transaction } from '@sentry/core' ;
3
4
import type { Event } from '@sentry/types' ;
4
5
import { SentryError } from '@sentry/utils' ;
5
6
import * as http from 'http' ;
6
7
7
- import { setDomainAsyncContextStrategy } from '../src/async/domain' ;
8
8
import { NodeClient } from '../src/client' ;
9
9
import { errorHandler , requestHandler , tracingHandler } from '../src/handlers' ;
10
10
import * as SDK from '../src/sdk' ;
11
11
import { getDefaultNodeClientOptions } from './helper/node-client-options' ;
12
12
13
- setDomainAsyncContextStrategy ( ) ;
13
+ function mockAsyncContextStrategy ( getHub : ( ) => Hub ) : void {
14
+ function getCurrentHub ( ) : Hub | undefined {
15
+ return getHub ( ) ;
16
+ }
17
+
18
+ function runWithAsyncContext < T > ( fn : ( hub : Hub ) => T ) : T {
19
+ return fn ( getHub ( ) ) ;
20
+ }
21
+
22
+ setAsyncContextStrategy ( { getCurrentHub, runWithAsyncContext } ) ;
23
+ }
14
24
15
25
describe ( 'requestHandler' , ( ) => {
16
26
const headers = { ears : 'furry' , nose : 'wet' , tongue : 'spotted' , cookie : 'favorite=zukes' } ;
@@ -52,6 +62,7 @@ describe('requestHandler', () => {
52
62
const hub = new sentryCore . Hub ( client ) ;
53
63
54
64
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
65
+ mockAsyncContextStrategy ( ( ) => hub ) ;
55
66
56
67
sentryRequestMiddleware ( req , res , next ) ;
57
68
@@ -65,6 +76,7 @@ describe('requestHandler', () => {
65
76
const hub = new sentryCore . Hub ( client ) ;
66
77
67
78
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
79
+ mockAsyncContextStrategy ( ( ) => hub ) ;
68
80
69
81
sentryRequestMiddleware ( req , res , next ) ;
70
82
@@ -78,6 +90,7 @@ describe('requestHandler', () => {
78
90
const hub = new sentryCore . Hub ( client ) ;
79
91
80
92
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
93
+ mockAsyncContextStrategy ( ( ) => hub ) ;
81
94
82
95
const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
83
96
@@ -97,7 +110,9 @@ describe('requestHandler', () => {
97
110
const options = getDefaultNodeClientOptions ( { autoSessionTracking : false , release : '1.2' } ) ;
98
111
client = new NodeClient ( options ) ;
99
112
const hub = new sentryCore . Hub ( client ) ;
113
+
100
114
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
115
+ mockAsyncContextStrategy ( ( ) => hub ) ;
101
116
102
117
const captureRequestSession = jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
103
118
@@ -142,6 +157,7 @@ describe('requestHandler', () => {
142
157
it ( 'stores request and request data options in `sdkProcessingMetadata`' , ( ) => {
143
158
const hub = new sentryCore . Hub ( new NodeClient ( getDefaultNodeClientOptions ( ) ) ) ;
144
159
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
160
+ mockAsyncContextStrategy ( ( ) => hub ) ;
145
161
146
162
const requestHandlerOptions = { include : { ip : false } } ;
147
163
const sentryRequestMiddleware = requestHandler ( requestHandlerOptions ) ;
@@ -177,6 +193,7 @@ describe('tracingHandler', () => {
177
193
beforeEach ( ( ) => {
178
194
hub = new sentryCore . Hub ( new NodeClient ( getDefaultNodeClientOptions ( { tracesSampleRate : 1.0 } ) ) ) ;
179
195
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
196
+ mockAsyncContextStrategy ( ( ) => hub ) ;
180
197
req = {
181
198
headers,
182
199
method,
@@ -274,6 +291,8 @@ describe('tracingHandler', () => {
274
291
const tracesSampler = jest . fn ( ) ;
275
292
const options = getDefaultNodeClientOptions ( { tracesSampler } ) ;
276
293
const hub = new sentryCore . Hub ( new NodeClient ( options ) ) ;
294
+ mockAsyncContextStrategy ( ( ) => hub ) ;
295
+
277
296
hub . run ( ( ) => {
278
297
sentryTracingMiddleware ( req , res , next ) ;
279
298
@@ -296,6 +315,7 @@ describe('tracingHandler', () => {
296
315
const hub = new sentryCore . Hub ( new NodeClient ( options ) ) ;
297
316
298
317
jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
318
+ mockAsyncContextStrategy ( ( ) => hub ) ;
299
319
300
320
sentryTracingMiddleware ( req , res , next ) ;
301
321
@@ -502,14 +522,17 @@ describe('errorHandler()', () => {
502
522
client . initSessionFlusher ( ) ;
503
523
const scope = new sentryCore . Scope ( ) ;
504
524
const hub = new sentryCore . Hub ( client , scope ) ;
525
+ mockAsyncContextStrategy ( ( ) => hub ) ;
505
526
506
527
jest . spyOn < any , any > ( client , '_captureRequestSession' ) ;
507
528
508
529
hub . run ( ( ) => {
509
530
scope ?. setRequestSession ( { status : 'ok' } ) ;
510
- sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , next ) ;
511
- const requestSession = scope ?. getRequestSession ( ) ;
512
- expect ( requestSession ) . toEqual ( { status : 'crashed' } ) ;
531
+ sentryErrorMiddleware ( { name : 'error' , message : 'this is an error' } , req , res , ( ) => {
532
+ const scope = sentryCore . getCurrentHub ( ) . getScope ( ) ;
533
+ const requestSession = scope ?. getRequestSession ( ) ;
534
+ expect ( requestSession ) . toEqual ( { status : 'crashed' } ) ;
535
+ } ) ;
513
536
} ) ;
514
537
} ) ;
515
538
@@ -535,6 +558,7 @@ describe('errorHandler()', () => {
535
558
client = new NodeClient ( options ) ;
536
559
537
560
const hub = new sentryCore . Hub ( client ) ;
561
+ mockAsyncContextStrategy ( ( ) => hub ) ;
538
562
sentryCore . makeMain ( hub ) ;
539
563
540
564
// `sentryErrorMiddleware` uses `withScope`, and we need access to the temporary scope it creates, so monkeypatch
0 commit comments