1
1
import * as os from 'os' ;
2
- import { Scope , SessionFlusher } from '@sentry/core' ;
2
+ import { SessionFlusher , getCurrentScope , getGlobalScope , getIsolationScope , withIsolationScope } from '@sentry/core' ;
3
3
import type { Event , EventHint } from '@sentry/types' ;
4
4
5
+ import type { Scope } from '@sentry/types' ;
5
6
import { NodeClient } from '../src' ;
7
+ import { setNodeAsyncContextStrategy } from '../src/async' ;
6
8
import { getDefaultNodeClientOptions } from './helper/node-client-options' ;
7
9
8
10
const PUBLIC_DSN = 'https://username@domain/123' ;
@@ -13,19 +15,30 @@ describe('NodeClient', () => {
13
15
afterEach ( ( ) => {
14
16
if ( '_sessionFlusher' in client ) clearInterval ( ( client as any ) . _sessionFlusher . _intervalId ) ;
15
17
jest . restoreAllMocks ( ) ;
18
+
19
+ getIsolationScope ( ) . clear ( ) ;
20
+ getGlobalScope ( ) . clear ( ) ;
21
+ getCurrentScope ( ) . clear ( ) ;
22
+ getCurrentScope ( ) . setClient ( undefined ) ;
23
+ } ) ;
24
+
25
+ beforeEach ( ( ) => {
26
+ setNodeAsyncContextStrategy ( ) ;
16
27
} ) ;
17
28
18
29
describe ( 'captureException' , ( ) => {
19
30
test ( 'when autoSessionTracking is enabled, and requestHandler is not used -> requestStatus should not be set' , ( ) => {
20
31
const options = getDefaultNodeClientOptions ( { dsn : PUBLIC_DSN , autoSessionTracking : true , release : '1.4' } ) ;
21
32
client = new NodeClient ( options ) ;
22
- const scope = new Scope ( ) ;
23
- scope . setRequestSession ( { status : 'ok' } ) ;
24
33
25
- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
34
+ withIsolationScope ( isolationScope => {
35
+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
26
36
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
+ } ) ;
29
42
} ) ;
30
43
31
44
test ( 'when autoSessionTracking is disabled -> requestStatus should not be set' , ( ) => {
@@ -35,13 +48,14 @@ describe('NodeClient', () => {
35
48
// by the`requestHandler`)
36
49
client . initSessionFlusher ( ) ;
37
50
38
- const scope = new Scope ( ) ;
39
- scope . setRequestSession ( { status : 'ok' } ) ;
51
+ withIsolationScope ( isolationScope => {
52
+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
40
53
41
- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
54
+ client . captureException ( new Error ( 'test exception' ) ) ;
42
55
43
- const requestSession = scope . getRequestSession ( ) ;
44
- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
56
+ const requestSession = isolationScope . getRequestSession ( ) ;
57
+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
58
+ } ) ;
45
59
} ) ;
46
60
47
61
test ( 'when autoSessionTracking is enabled + requestSession status is Crashed -> requestStatus should not be overridden' , ( ) => {
@@ -51,13 +65,14 @@ describe('NodeClient', () => {
51
65
// by the`requestHandler`)
52
66
client . initSessionFlusher ( ) ;
53
67
54
- const scope = new Scope ( ) ;
55
- scope . setRequestSession ( { status : 'crashed' } ) ;
68
+ withIsolationScope ( isolationScope => {
69
+ isolationScope . setRequestSession ( { status : 'crashed' } ) ;
56
70
57
- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
71
+ client . captureException ( new Error ( 'test exception' ) ) ;
58
72
59
- const requestSession = scope . getRequestSession ( ) ;
60
- expect ( requestSession ! . status ) . toEqual ( 'crashed' ) ;
73
+ const requestSession = isolationScope . getRequestSession ( ) ;
74
+ expect ( requestSession ! . status ) . toEqual ( 'crashed' ) ;
75
+ } ) ;
61
76
} ) ;
62
77
63
78
test ( 'when autoSessionTracking is enabled + error occurs within request bounds -> requestStatus should be set to Errored' , ( ) => {
@@ -67,28 +82,37 @@ describe('NodeClient', () => {
67
82
// by the`requestHandler`)
68
83
client . initSessionFlusher ( ) ;
69
84
70
- const scope = new Scope ( ) ;
71
- scope . setRequestSession ( { status : 'ok' } ) ;
85
+ withIsolationScope ( isolationScope => {
86
+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
72
87
73
- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
88
+ client . captureException ( new Error ( 'test exception' ) ) ;
74
89
75
- const requestSession = scope . getRequestSession ( ) ;
76
- expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
90
+ const requestSession = isolationScope . getRequestSession ( ) ;
91
+ expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
92
+ } ) ;
77
93
} ) ;
78
94
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 => {
80
96
const options = getDefaultNodeClientOptions ( { dsn : PUBLIC_DSN , autoSessionTracking : true , release : '1.4' } ) ;
81
97
client = new NodeClient ( options ) ;
98
+
82
99
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
83
100
// by the`requestHandler`)
84
101
client . initSessionFlusher ( ) ;
85
102
86
- const scope = new Scope ( ) ;
103
+ let isolationScope : Scope ;
104
+ withIsolationScope ( _isolationScope => {
105
+ _isolationScope . setRequestSession ( { status : 'ok' } ) ;
106
+ isolationScope = _isolationScope ;
107
+ } ) ;
87
108
88
- client . captureException ( new Error ( 'test exception' ) , undefined , scope ) ;
109
+ client . captureException ( new Error ( 'test exception' ) ) ;
89
110
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
+ } ) ;
92
116
} ) ;
93
117
} ) ;
94
118
@@ -100,16 +124,12 @@ describe('NodeClient', () => {
100
124
// by the`requestHandler`)
101
125
client . initSessionFlusher ( ) ;
102
126
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
+ } ) ;
113
133
} ) ;
114
134
115
135
test ( 'When captureEvent is called with an exception, requestSession status should be set to Errored' , ( ) => {
@@ -119,13 +139,14 @@ describe('NodeClient', () => {
119
139
// by the`requestHandler`)
120
140
client . initSessionFlusher ( ) ;
121
141
122
- const scope = new Scope ( ) ;
123
- scope . setRequestSession ( { status : 'ok' } ) ;
142
+ withIsolationScope ( isolationScope => {
143
+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
124
144
125
- client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } , { } , scope ) ;
145
+ client . captureEvent ( { message : 'message' , exception : { values : [ { type : 'exception type 1' } ] } } ) ;
126
146
127
- const requestSession = scope . getRequestSession ( ) ;
128
- expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
147
+ const requestSession = isolationScope . getRequestSession ( ) ;
148
+ expect ( requestSession ! . status ) . toEqual ( 'errored' ) ;
149
+ } ) ;
129
150
} ) ;
130
151
131
152
test ( 'When captureEvent is called without an exception, requestSession status should not be set to Errored' , ( ) => {
@@ -135,13 +156,14 @@ describe('NodeClient', () => {
135
156
// by the`requestHandler`)
136
157
client . initSessionFlusher ( ) ;
137
158
138
- const scope = new Scope ( ) ;
139
- scope . setRequestSession ( { status : 'ok' } ) ;
159
+ withIsolationScope ( isolationScope => {
160
+ isolationScope . setRequestSession ( { status : 'ok' } ) ;
140
161
141
- client . captureEvent ( { message : 'message' } , { } , scope ) ;
162
+ client . captureEvent ( { message : 'message' } ) ;
142
163
143
- const requestSession = scope . getRequestSession ( ) ;
144
- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
164
+ const requestSession = isolationScope . getRequestSession ( ) ;
165
+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
166
+ } ) ;
145
167
} ) ;
146
168
147
169
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', () => {
151
173
// by the`requestHandler`)
152
174
client . initSessionFlusher ( ) ;
153
175
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' } ] } } ) ;
161
179
162
- expect ( scope . getRequestSession ( ) ) . toEqual ( undefined ) ;
180
+ expect ( isolationScope . getRequestSession ( ) ) . toEqual ( undefined ) ;
181
+ } ) ;
163
182
} ) ;
164
183
165
184
test ( 'When captureEvent is called with a transaction, then requestSession status should not be set' , ( ) => {
@@ -169,28 +188,28 @@ describe('NodeClient', () => {
169
188
// by the`requestHandler`)
170
189
client . initSessionFlusher ( ) ;
171
190
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' } ) ;
175
195
176
- const requestSession = scope . getRequestSession ( ) ;
177
- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
196
+ const requestSession = isolationScope . getRequestSession ( ) ;
197
+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
198
+ } ) ;
178
199
} ) ;
179
200
180
201
test ( 'When captureEvent is called with an exception but requestHandler is not used, then requestSession status should not be set' , ( ) => {
181
202
const options = getDefaultNodeClientOptions ( { dsn : PUBLIC_DSN , autoSessionTracking : true , release : '1.3' } ) ;
182
203
client = new NodeClient ( options ) ;
183
204
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' } ] } } ) ;
191
209
192
- const requestSession = scope . getRequestSession ( ) ;
193
- expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
210
+ const requestSession = isolationScope . getRequestSession ( ) ;
211
+ expect ( requestSession ! . status ) . toEqual ( 'ok' ) ;
212
+ } ) ;
194
213
} ) ;
195
214
} ) ;
196
215
0 commit comments