@@ -16,9 +16,7 @@ describe('Integration | Scope', () => {
16
16
const beforeSend = vi . fn ( ( ) => null ) ;
17
17
const beforeSendTransaction = vi . fn ( ( ) => null ) ;
18
18
19
- const client = tracingEnabled
20
- ? mockSdkInit ( { tracesSampleRate : 1 , beforeSend, beforeSendTransaction } )
21
- : mockSdkInit ( { beforeSend, beforeSendTransaction } ) ;
19
+ const client = mockSdkInit ( { tracesSampleRate : tracingEnabled ? 1 : 0 , beforeSend, beforeSendTransaction } ) ;
22
20
23
21
const rootScope = getCurrentScope ( ) ;
24
22
@@ -38,29 +36,24 @@ describe('Integration | Scope', () => {
38
36
Sentry . withScope ( scope2 => {
39
37
scope2 . setTag ( 'tag3' , 'val3' ) ;
40
38
41
- if ( tracingEnabled ) {
42
- Sentry . startSpan ( { name : 'outer' } , span => {
43
- expect ( getCapturedScopesOnSpan ( span ) . scope ) . toBe ( scope2 ) ;
44
-
45
- spanId = span . spanContext ( ) . spanId ;
46
- traceId = span . spanContext ( ) . traceId ;
39
+ Sentry . startSpan ( { name : 'outer' } , span => {
40
+ expect ( getCapturedScopesOnSpan ( span ) . scope ) . toBe ( tracingEnabled ? scope2 : undefined ) ;
47
41
48
- Sentry . setTag ( 'tag4' , 'val4' ) ;
42
+ spanId = span . spanContext ( ) . spanId ;
43
+ traceId = span . spanContext ( ) . traceId ;
49
44
50
- Sentry . captureException ( error ) ;
51
- } ) ;
52
- } else {
53
45
Sentry . setTag ( 'tag4' , 'val4' ) ;
46
+
54
47
Sentry . captureException ( error ) ;
55
- }
48
+ } ) ;
56
49
} ) ;
57
50
} ) ;
58
51
59
52
await client ?. flush ( ) ;
60
53
61
54
expect ( beforeSend ) . toHaveBeenCalledTimes ( 1 ) ;
62
55
63
- if ( spanId && tracingEnabled ) {
56
+ if ( spanId ) {
64
57
expect ( beforeSend ) . toHaveBeenCalledWith (
65
58
expect . objectContaining ( {
66
59
contexts : expect . objectContaining ( {
@@ -135,9 +128,7 @@ describe('Integration | Scope', () => {
135
128
const beforeSend = vi . fn ( ( ) => null ) ;
136
129
const beforeSendTransaction = vi . fn ( ( ) => null ) ;
137
130
138
- const client = tracingEnabled
139
- ? mockSdkInit ( { tracesSampleRate : 1 , beforeSend, beforeSendTransaction } )
140
- : mockSdkInit ( { beforeSend, beforeSendTransaction } ) ;
131
+ const client = mockSdkInit ( { tracesSampleRate : tracingEnabled ? 1 : 0 , beforeSend, beforeSendTransaction } ) ;
141
132
142
133
const rootScope = getCurrentScope ( ) ;
143
134
@@ -235,17 +226,124 @@ describe('Integration | Scope', () => {
235
226
) ;
236
227
237
228
if ( tracingEnabled ) {
238
- expect ( traceId1 ) . not . toEqual ( traceId2 ) ;
239
229
expect ( beforeSendTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
240
230
}
241
231
} ) ;
242
232
} ) ;
243
233
234
+ describe ( 'global scope' , ( ) => {
235
+ beforeEach ( ( ) => {
236
+ resetGlobals ( ) ;
237
+ } ) ;
238
+
239
+ it ( 'works before calling init' , ( ) => {
240
+ const globalScope = Sentry . getGlobalScope ( ) ;
241
+ expect ( globalScope ) . toBeDefined ( ) ;
242
+ // No client attached
243
+ expect ( globalScope . getClient ( ) ) . toBeUndefined ( ) ;
244
+ // Repeatedly returns the same instance
245
+ expect ( Sentry . getGlobalScope ( ) ) . toBe ( globalScope ) ;
246
+
247
+ globalScope . setTag ( 'tag1' , 'val1' ) ;
248
+ globalScope . setTag ( 'tag2' , 'val2' ) ;
249
+
250
+ expect ( globalScope . getScopeData ( ) . tags ) . toEqual ( { tag1 : 'val1' , tag2 : 'val2' } ) ;
251
+
252
+ // Now when we call init, the global scope remains intact
253
+ Sentry . init ( { dsn : 'https://username@domain/123' , defaultIntegrations : false } ) ;
254
+
255
+ expect ( globalScope . getClient ( ) ) . toBeUndefined ( ) ;
256
+ expect ( Sentry . getGlobalScope ( ) ) . toBe ( globalScope ) ;
257
+ expect ( globalScope . getScopeData ( ) . tags ) . toEqual ( { tag1 : 'val1' , tag2 : 'val2' } ) ;
258
+ } ) ;
259
+
260
+ it ( 'is applied to events' , async ( ) => {
261
+ const beforeSend = vi . fn ( ) ;
262
+ const client = mockSdkInit ( { beforeSend } ) ;
263
+
264
+ const globalScope = Sentry . getGlobalScope ( ) ;
265
+ globalScope . setTag ( 'tag1' , 'val1' ) ;
266
+ globalScope . setTag ( 'tag2' , 'val2' ) ;
267
+
268
+ const error = new Error ( 'test error' ) ;
269
+ Sentry . captureException ( error ) ;
270
+
271
+ await client ?. flush ( ) ;
272
+
273
+ expect ( beforeSend ) . toHaveBeenCalledTimes ( 1 ) ;
274
+ expect ( beforeSend ) . toHaveBeenCalledWith (
275
+ expect . objectContaining ( {
276
+ tags : {
277
+ tag1 : 'val1' ,
278
+ tag2 : 'val2' ,
279
+ } ,
280
+ } ) ,
281
+ {
282
+ event_id : expect . any ( String ) ,
283
+ originalException : error ,
284
+ syntheticException : expect . any ( Error ) ,
285
+ } ,
286
+ ) ;
287
+ } ) ;
288
+ } ) ;
289
+
244
290
describe ( 'isolation scope' , ( ) => {
245
291
beforeEach ( ( ) => {
246
292
resetGlobals ( ) ;
247
293
} ) ;
248
294
295
+ it ( 'works before calling init' , ( ) => {
296
+ const isolationScope = Sentry . getIsolationScope ( ) ;
297
+ expect ( isolationScope ) . toBeDefined ( ) ;
298
+ // No client attached
299
+ expect ( isolationScope . getClient ( ) ) . toBeUndefined ( ) ;
300
+ // Repeatedly returns the same instance
301
+ expect ( Sentry . getIsolationScope ( ) ) . toBe ( isolationScope ) ;
302
+
303
+ isolationScope . setTag ( 'tag1' , 'val1' ) ;
304
+ isolationScope . setTag ( 'tag2' , 'val2' ) ;
305
+
306
+ expect ( isolationScope . getScopeData ( ) . tags ) . toEqual ( { tag1 : 'val1' , tag2 : 'val2' } ) ;
307
+
308
+ // Now when we call init, the isolation scope remains intact
309
+ Sentry . init ( { dsn : 'https://username@domain/123' , defaultIntegrations : false } ) ;
310
+
311
+ // client is only attached to global scope by default
312
+ expect ( isolationScope . getClient ( ) ) . toBeUndefined ( ) ;
313
+ expect ( Sentry . getIsolationScope ( ) ) . toBe ( isolationScope ) ;
314
+ expect ( isolationScope . getScopeData ( ) . tags ) . toEqual ( { tag1 : 'val1' , tag2 : 'val2' } ) ;
315
+ } ) ;
316
+
317
+ it ( 'is applied to events' , async ( ) => {
318
+ const beforeSend = vi . fn ( ) ;
319
+ mockSdkInit ( { beforeSend } ) ;
320
+ const client = Sentry . getClient ( ) ;
321
+
322
+ const isolationScope = Sentry . getIsolationScope ( ) ;
323
+ isolationScope . setTag ( 'tag1' , 'val1' ) ;
324
+ isolationScope . setTag ( 'tag2' , 'val2' ) ;
325
+
326
+ const error = new Error ( 'test error' ) ;
327
+ Sentry . captureException ( error ) ;
328
+
329
+ await client ?. flush ( ) ;
330
+
331
+ expect ( beforeSend ) . toHaveBeenCalledTimes ( 1 ) ;
332
+ expect ( beforeSend ) . toHaveBeenCalledWith (
333
+ expect . objectContaining ( {
334
+ tags : {
335
+ tag1 : 'val1' ,
336
+ tag2 : 'val2' ,
337
+ } ,
338
+ } ) ,
339
+ {
340
+ event_id : expect . any ( String ) ,
341
+ originalException : error ,
342
+ syntheticException : expect . any ( Error ) ,
343
+ } ,
344
+ ) ;
345
+ } ) ;
346
+
249
347
it ( 'withIsolationScope works' , async ( ) => {
250
348
const beforeSend = vi . fn ( ) ;
251
349
mockSdkInit ( { beforeSend } ) ;
@@ -494,8 +592,8 @@ describe('Integration | Scope', () => {
494
592
} ) ;
495
593
496
594
it ( 'automatically forks with OTEL context' , async ( ) => {
497
- const beforeSend = vi . fn ( ( ) => null ) ;
498
- mockSdkInit ( { tracesSampleRate : 1 , beforeSend } ) ;
595
+ const beforeSend = vi . fn ( ) ;
596
+ mockSdkInit ( { beforeSend } ) ;
499
597
const client = Sentry . getClient ( ) ;
500
598
501
599
const initialCurrentScope = Sentry . getCurrentScope ( ) ;
0 commit comments