11import * as os from 'os' ;
2- import { Scope , SessionFlusher } from '@sentry/core' ;
2+ import { SessionFlusher , getCurrentScope , getGlobalScope , getIsolationScope , withIsolationScope } from '@sentry/core' ;
33import type { Event , EventHint } from '@sentry/types' ;
44
5+ import type { Scope } from '@sentry/types' ;
56import { NodeClient } from '../src' ;
7+ import { setNodeAsyncContextStrategy } from '../src/async' ;
68import { getDefaultNodeClientOptions } from './helper/node-client-options' ;
79
810const PUBLIC_DSN = 'https://username@domain/123' ;
@@ -13,19 +15,30 @@ describe('NodeClient', () => {
1315 afterEach ( ( ) => {
1416 if ( '_sessionFlusher' in client ) clearInterval ( ( client as any ) . _sessionFlusher . _intervalId ) ;
1517 jest . restoreAllMocks ( ) ;
18+
19+ getIsolationScope ( ) . clear ( ) ;
20+ getGlobalScope ( ) . clear ( ) ;
21+ getCurrentScope ( ) . clear ( ) ;
22+ getCurrentScope ( ) . setClient ( undefined ) ;
23+ } ) ;
24+
25+ beforeEach ( ( ) => {
26+ setNodeAsyncContextStrategy ( ) ;
1627 } ) ;
1728
1829 describe ( 'captureException' , ( ) => {
1930 test ( 'when autoSessionTracking is enabled, and requestHandler is not used -> requestStatus should not be set' , ( ) => {
2031 const options = getDefaultNodeClientOptions ( { dsn : PUBLIC_DSN , autoSessionTracking : true , release : '1.4' } ) ;
2132 client = new NodeClient ( options ) ;
22- const scope = new Scope ( ) ;
23- scope . setRequestSession ( { status : 'ok' } ) ;
2433
25- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
34+ withIsolationScope ( isolationScope => {
35+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
2636
27- const requestSession = scope . getRequestSession ( ) ;
28- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
37+ client . captureException ( new Error ( 'test exception' ) ) ;
38+
39+ const requestSession = isolationScope . getRequestSession ( ) ;
40+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
41+ } ) ;
2942 } ) ;
3043
3144 test ( 'when autoSessionTracking is disabled -> requestStatus should not be set' , ( ) => {
@@ -35,13 +48,14 @@ describe('NodeClient', () => {
3548 // by the`requestHandler`)
3649 client . initSessionFlusher ( ) ;
3750
38- const scope = new Scope ( ) ;
39- scope . setRequestSession ( { status : 'ok' } ) ;
51+ withIsolationScope ( isolationScope => {
52+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
4053
41- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
54+ client . captureException ( new Error ( 'test exception' ) ) ;
4255
43- const requestSession = scope . getRequestSession ( ) ;
44- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
56+ const requestSession = isolationScope . getRequestSession ( ) ;
57+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
58+ } ) ;
4559 } ) ;
4660
4761 test ( 'when autoSessionTracking is enabled + requestSession status is Crashed -> requestStatus should not be overridden' , ( ) => {
@@ -51,13 +65,14 @@ describe('NodeClient', () => {
5165 // by the`requestHandler`)
5266 client . initSessionFlusher ( ) ;
5367
54- const scope = new Scope ( ) ;
55- scope . setRequestSession ( { status : 'crashed' } ) ;
68+ withIsolationScope ( isolationScope => {
69+ isolationScope . setRequestSession ( { status : 'crashed' } ) ;
5670
57- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
71+ client . captureException ( new Error ( 'test exception' ) ) ;
5872
59- const requestSession = scope . getRequestSession ( ) ;
60- expect ( requestSession ! . status ) . toEqual ( 'crashed' ) ;
73+ const requestSession = isolationScope . getRequestSession ( ) ;
74+ expect ( requestSession ! . status ) . toEqual ( 'crashed' ) ;
75+ } ) ;
6176 } ) ;
6277
6378 test ( 'when autoSessionTracking is enabled + error occurs within request bounds -> requestStatus should be set to Errored' , ( ) => {
@@ -67,28 +82,37 @@ describe('NodeClient', () => {
6782 // by the`requestHandler`)
6883 client . initSessionFlusher ( ) ;
6984
70- const scope = new Scope ( ) ;
71- scope . setRequestSession ( { status : 'ok' } ) ;
85+ withIsolationScope ( isolationScope => {
86+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
7287
73- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
88+ client . captureException ( new Error ( 'test exception' ) ) ;
7489
75- const requestSession = scope . getRequestSession ( ) ;
76- expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
90+ const requestSession = isolationScope . getRequestSession ( ) ;
91+ expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
92+ } ) ;
7793 } ) ;
7894
79- test ( 'when autoSessionTracking is enabled + error occurs outside of request bounds -> requestStatus should not be set to Errored' , ( ) => {
95+ test ( 'when autoSessionTracking is enabled + error occurs outside of request bounds -> requestStatus should not be set to Errored' , done => {
8096 const options = getDefaultNodeClientOptions ( { dsn : PUBLIC_DSN , autoSessionTracking : true , release : '1.4' } ) ;
8197 client = new NodeClient ( options ) ;
98+
8299 // It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
83100 // by the`requestHandler`)
84101 client . initSessionFlusher ( ) ;
85102
86- const scope = new Scope ( ) ;
103+ let isolationScope : Scope ;
104+ withIsolationScope ( _isolationScope => {
105+ _isolationScope . setRequestSession ( { status : 'ok' } ) ;
106+ isolationScope = _isolationScope ;
107+ } ) ;
87108
88- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
109+ client . captureException ( new Error ( 'test exception' ) ) ;
89110
90- const requestSession = scope . getRequestSession ( ) ;
91- expect ( requestSession ) . toEqual ( undefined ) ;
111+ setImmediate ( ( ) => {
112+ const requestSession = isolationScope . getRequestSession ( ) ;
113+ expect ( requestSession ) . toEqual ( { status : 'ok' } ) ;
114+ done ( ) ;
115+ } ) ;
92116 } ) ;
93117 } ) ;
94118
@@ -100,16 +124,12 @@ describe('NodeClient', () => {
100124 // by the`requestHandler`)
101125 client . initSessionFlusher ( ) ;
102126
103- const scope = new Scope ( ) ;
104- scope . setRequestSession ( { status : 'ok' } ) ;
105- client . captureEvent (
106- { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ,
107- undefined ,
108- scope ,
109- ) ;
110-
111- const requestSession = scope . getRequestSession ( ) ;
112- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
127+ withIsolationScope ( isolationScope => {
128+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
129+ client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ) ;
130+ const requestSession = isolationScope . getRequestSession ( ) ;
131+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
132+ } ) ;
113133 } ) ;
114134
115135 test ( 'When captureEvent is called with an exception, requestSession status should be set to Errored' , ( ) => {
@@ -119,13 +139,14 @@ describe('NodeClient', () => {
119139 // by the`requestHandler`)
120140 client . initSessionFlusher ( ) ;
121141
122- const scope = new Scope ( ) ;
123- scope . setRequestSession ( { status : 'ok' } ) ;
142+ withIsolationScope ( isolationScope => {
143+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
124144
125- client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } , { } , scope ) ;
145+ client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ) ;
126146
127- const requestSession = scope . getRequestSession ( ) ;
128- expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
147+ const requestSession = isolationScope . getRequestSession ( ) ;
148+ expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
149+ } ) ;
129150 } ) ;
130151
131152 test ( 'When captureEvent is called without an exception, requestSession status should not be set to Errored' , ( ) => {
@@ -135,13 +156,14 @@ describe('NodeClient', () => {
135156 // by the`requestHandler`)
136157 client . initSessionFlusher ( ) ;
137158
138- const scope = new Scope ( ) ;
139- scope . setRequestSession ( { status : 'ok' } ) ;
159+ withIsolationScope ( isolationScope => {
160+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
140161
141- client . captureEvent ( { message : 'message' } , { } , scope ) ;
162+ client . captureEvent ( { message : 'message' } ) ;
142163
143- const requestSession = scope . getRequestSession ( ) ;
144- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
164+ const requestSession = isolationScope . getRequestSession ( ) ;
165+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
166+ } ) ;
145167 } ) ;
146168
147169 test ( 'When captureEvent is called with an exception but outside of a request, then requestStatus should not be set' , ( ) => {
@@ -151,15 +173,12 @@ describe('NodeClient', () => {
151173 // by the`requestHandler`)
152174 client . initSessionFlusher ( ) ;
153175
154- const scope = new Scope ( ) ;
155-
156- client . captureEvent (
157- { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ,
158- undefined ,
159- scope ,
160- ) ;
176+ withIsolationScope ( isolationScope => {
177+ isolationScope . clear ( ) ;
178+ client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ) ;
161179
162- expect ( scope . getRequestSession ( ) ) . toEqual ( undefined ) ;
180+ expect ( isolationScope . getRequestSession ( ) ) . toEqual ( undefined ) ;
181+ } ) ;
163182 } ) ;
164183
165184 test ( 'When captureEvent is called with a transaction, then requestSession status should not be set' , ( ) => {
@@ -169,28 +188,28 @@ describe('NodeClient', () => {
169188 // by the`requestHandler`)
170189 client . initSessionFlusher ( ) ;
171190
172- const scope = new Scope ( ) ;
173- scope . setRequestSession ( { status : 'ok' } ) ;
174- client . captureEvent ( { message : 'message' , type : 'transaction' } , undefined , scope ) ;
191+ withIsolationScope ( isolationScope => {
192+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
193+
194+ client . captureEvent ( { message : 'message' , type : 'transaction' } ) ;
175195
176- const requestSession = scope . getRequestSession ( ) ;
177- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
196+ const requestSession = isolationScope . getRequestSession ( ) ;
197+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
198+ } ) ;
178199 } ) ;
179200
180201 test ( 'When captureEvent is called with an exception but requestHandler is not used, then requestSession status should not be set' , ( ) => {
181202 const options = getDefaultNodeClientOptions ( { dsn : PUBLIC_DSN , autoSessionTracking : true , release : '1.3' } ) ;
182203 client = new NodeClient ( options ) ;
183204
184- const scope = new Scope ( ) ;
185- scope . setRequestSession ( { status : 'ok' } ) ;
186- client . captureEvent (
187- { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ,
188- undefined ,
189- scope ,
190- ) ;
205+ withIsolationScope ( isolationScope => {
206+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
207+
208+ client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ) ;
191209
192- const requestSession = scope . getRequestSession ( ) ;
193- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
210+ const requestSession = isolationScope . getRequestSession ( ) ;
211+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
212+ } ) ;
194213 } ) ;
195214 } ) ;
196215
0 commit comments