11import { Scope , getCurrentScope , getGlobalScope , getIsolationScope , withIsolationScope , withScope } from '@sentry/core' ;
22import { GLOBAL_OBJ } from '@sentry/utils' ;
33import { AsyncLocalStorage } from 'async_hooks' ;
4+ import { beforeEach , describe , expect , it } from 'vitest' ;
45import { setAsyncLocalStorageAsyncContextStrategy } from '../src/async' ;
56
67describe ( 'withScope()' , ( ) => {
@@ -13,67 +14,73 @@ describe('withScope()', () => {
1314 setAsyncLocalStorageAsyncContextStrategy ( ) ;
1415 } ) ;
1516
16- it ( 'will make the passed scope the active scope within the callback' , done => {
17- withScope ( scope => {
18- expect ( getCurrentScope ( ) ) . toBe ( scope ) ;
19- done ( ) ;
20- } ) ;
21- } ) ;
22-
23- it ( 'will pass a scope that is different from the current active isolation scope' , done => {
24- withScope ( scope => {
25- expect ( getIsolationScope ( ) ) . not . toBe ( scope ) ;
26- done ( ) ;
27- } ) ;
28- } ) ;
29-
30- it ( 'will always make the inner most passed scope the current scope when nesting calls' , done => {
31- withIsolationScope ( _scope1 => {
32- withIsolationScope ( scope2 => {
33- expect ( getIsolationScope ( ) ) . toBe ( scope2 ) ;
17+ it ( 'will make the passed scope the active scope within the callback' , ( ) =>
18+ new Promise < void > ( done => {
19+ withScope ( scope => {
20+ expect ( getCurrentScope ( ) ) . toBe ( scope ) ;
3421 done ( ) ;
3522 } ) ;
36- } ) ;
37- } ) ;
23+ } ) ) ;
3824
39- it ( 'forks the scope when not passing any scope' , done => {
40- const initialScope = getCurrentScope ( ) ;
41- initialScope . setTag ( 'aa' , 'aa' ) ;
42-
43- withScope ( scope => {
44- expect ( getCurrentScope ( ) ) . toBe ( scope ) ;
45- scope . setTag ( 'bb' , 'bb' ) ;
46- expect ( scope ) . not . toBe ( initialScope ) ;
47- expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
48- done ( ) ;
49- } ) ;
50- } ) ;
51-
52- it ( 'forks the scope when passing undefined' , done => {
53- const initialScope = getCurrentScope ( ) ;
54- initialScope . setTag ( 'aa' , 'aa' ) ;
55-
56- withScope ( undefined , scope => {
57- expect ( getCurrentScope ( ) ) . toBe ( scope ) ;
58- scope . setTag ( 'bb' , 'bb' ) ;
59- expect ( scope ) . not . toBe ( initialScope ) ;
60- expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
61- done ( ) ;
62- } ) ;
63- } ) ;
25+ it ( 'will pass a scope that is different from the current active isolation scope' , ( ) =>
26+ new Promise < void > ( done => {
27+ withScope ( scope => {
28+ expect ( getIsolationScope ( ) ) . not . toBe ( scope ) ;
29+ done ( ) ;
30+ } ) ;
31+ } ) ) ;
32+
33+ it ( 'will always make the inner most passed scope the current scope when nesting calls' , ( ) =>
34+ new Promise < void > ( done => {
35+ withIsolationScope ( _scope1 => {
36+ withIsolationScope ( scope2 => {
37+ expect ( getIsolationScope ( ) ) . toBe ( scope2 ) ;
38+ done ( ) ;
39+ } ) ;
40+ } ) ;
41+ } ) ) ;
42+
43+ it ( 'forks the scope when not passing any scope' , ( ) =>
44+ new Promise < void > ( done => {
45+ const initialScope = getCurrentScope ( ) ;
46+ initialScope . setTag ( 'aa' , 'aa' ) ;
47+
48+ withScope ( scope => {
49+ expect ( getCurrentScope ( ) ) . toBe ( scope ) ;
50+ scope . setTag ( 'bb' , 'bb' ) ;
51+ expect ( scope ) . not . toBe ( initialScope ) ;
52+ expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
53+ done ( ) ;
54+ } ) ;
55+ } ) ) ;
56+
57+ it ( 'forks the scope when passing undefined' , ( ) =>
58+ new Promise < void > ( done => {
59+ const initialScope = getCurrentScope ( ) ;
60+ initialScope . setTag ( 'aa' , 'aa' ) ;
61+
62+ withScope ( undefined , scope => {
63+ expect ( getCurrentScope ( ) ) . toBe ( scope ) ;
64+ scope . setTag ( 'bb' , 'bb' ) ;
65+ expect ( scope ) . not . toBe ( initialScope ) ;
66+ expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
67+ done ( ) ;
68+ } ) ;
69+ } ) ) ;
6470
65- it ( 'sets the passed in scope as active scope' , done => {
66- const initialScope = getCurrentScope ( ) ;
67- initialScope . setTag ( 'aa' , 'aa' ) ;
71+ it ( 'sets the passed in scope as active scope' , ( ) =>
72+ new Promise < void > ( done => {
73+ const initialScope = getCurrentScope ( ) ;
74+ initialScope . setTag ( 'aa' , 'aa' ) ;
6875
69- const customScope = new Scope ( ) ;
76+ const customScope = new Scope ( ) ;
7077
71- withScope ( customScope , scope => {
72- expect ( getCurrentScope ( ) ) . toBe ( customScope ) ;
73- expect ( scope ) . toBe ( customScope ) ;
74- done ( ) ;
75- } ) ;
76- } ) ;
78+ withScope ( customScope , scope => {
79+ expect ( getCurrentScope ( ) ) . toBe ( customScope ) ;
80+ expect ( scope ) . toBe ( customScope ) ;
81+ done ( ) ;
82+ } ) ;
83+ } ) ) ;
7784} ) ;
7885
7986describe ( 'withIsolationScope()' , ( ) => {
@@ -86,65 +93,71 @@ describe('withIsolationScope()', () => {
8693 setAsyncLocalStorageAsyncContextStrategy ( ) ;
8794 } ) ;
8895
89- it ( 'will make the passed isolation scope the active isolation scope within the callback' , done => {
90- withIsolationScope ( scope => {
91- expect ( getIsolationScope ( ) ) . toBe ( scope ) ;
92- done ( ) ;
93- } ) ;
94- } ) ;
95-
96- it ( 'will pass an isolation scope that is different from the current active scope' , done => {
97- withIsolationScope ( scope => {
98- expect ( getCurrentScope ( ) ) . not . toBe ( scope ) ;
99- done ( ) ;
100- } ) ;
101- } ) ;
102-
103- it ( 'will always make the inner most passed scope the current scope when nesting calls' , done => {
104- withIsolationScope ( _scope1 => {
105- withIsolationScope ( scope2 => {
106- expect ( getIsolationScope ( ) ) . toBe ( scope2 ) ;
96+ it ( 'will make the passed isolation scope the active isolation scope within the callback' , ( ) =>
97+ new Promise < void > ( done => {
98+ withIsolationScope ( scope => {
99+ expect ( getIsolationScope ( ) ) . toBe ( scope ) ;
107100 done ( ) ;
108101 } ) ;
109- } ) ;
110- } ) ;
102+ } ) ) ;
111103
112- it ( 'forks the isolation scope when not passing any isolation scope' , done => {
113- const initialScope = getIsolationScope ( ) ;
114- initialScope . setTag ( 'aa' , 'aa' ) ;
115-
116- withIsolationScope ( scope => {
117- expect ( getIsolationScope ( ) ) . toBe ( scope ) ;
118- scope . setTag ( 'bb' , 'bb' ) ;
119- expect ( scope ) . not . toBe ( initialScope ) ;
120- expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
121- done ( ) ;
122- } ) ;
123- } ) ;
124-
125- it ( 'forks the isolation scope when passing undefined' , done => {
126- const initialScope = getIsolationScope ( ) ;
127- initialScope . setTag ( 'aa' , 'aa' ) ;
128-
129- withIsolationScope ( undefined , scope => {
130- expect ( getIsolationScope ( ) ) . toBe ( scope ) ;
131- scope . setTag ( 'bb' , 'bb' ) ;
132- expect ( scope ) . not . toBe ( initialScope ) ;
133- expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
134- done ( ) ;
135- } ) ;
136- } ) ;
104+ it ( 'will pass an isolation scope that is different from the current active scope' , ( ) =>
105+ new Promise < void > ( done => {
106+ withIsolationScope ( scope => {
107+ expect ( getCurrentScope ( ) ) . not . toBe ( scope ) ;
108+ done ( ) ;
109+ } ) ;
110+ } ) ) ;
111+
112+ it ( 'will always make the inner most passed scope the current scope when nesting calls' , ( ) =>
113+ new Promise < void > ( done => {
114+ withIsolationScope ( _scope1 => {
115+ withIsolationScope ( scope2 => {
116+ expect ( getIsolationScope ( ) ) . toBe ( scope2 ) ;
117+ done ( ) ;
118+ } ) ;
119+ } ) ;
120+ } ) ) ;
121+
122+ it ( 'forks the isolation scope when not passing any isolation scope' , ( ) =>
123+ new Promise < void > ( done => {
124+ const initialScope = getIsolationScope ( ) ;
125+ initialScope . setTag ( 'aa' , 'aa' ) ;
126+
127+ withIsolationScope ( scope => {
128+ expect ( getIsolationScope ( ) ) . toBe ( scope ) ;
129+ scope . setTag ( 'bb' , 'bb' ) ;
130+ expect ( scope ) . not . toBe ( initialScope ) ;
131+ expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
132+ done ( ) ;
133+ } ) ;
134+ } ) ) ;
135+
136+ it ( 'forks the isolation scope when passing undefined' , ( ) =>
137+ new Promise < void > ( done => {
138+ const initialScope = getIsolationScope ( ) ;
139+ initialScope . setTag ( 'aa' , 'aa' ) ;
140+
141+ withIsolationScope ( undefined , scope => {
142+ expect ( getIsolationScope ( ) ) . toBe ( scope ) ;
143+ scope . setTag ( 'bb' , 'bb' ) ;
144+ expect ( scope ) . not . toBe ( initialScope ) ;
145+ expect ( scope . getScopeData ( ) . tags ) . toEqual ( { aa : 'aa' , bb : 'bb' } ) ;
146+ done ( ) ;
147+ } ) ;
148+ } ) ) ;
137149
138- it ( 'sets the passed in isolation scope as active isolation scope' , done => {
139- const initialScope = getIsolationScope ( ) ;
140- initialScope . setTag ( 'aa' , 'aa' ) ;
150+ it ( 'sets the passed in isolation scope as active isolation scope' , ( ) =>
151+ new Promise < void > ( done => {
152+ const initialScope = getIsolationScope ( ) ;
153+ initialScope . setTag ( 'aa' , 'aa' ) ;
141154
142- const customScope = new Scope ( ) ;
155+ const customScope = new Scope ( ) ;
143156
144- withIsolationScope ( customScope , scope => {
145- expect ( getIsolationScope ( ) ) . toBe ( customScope ) ;
146- expect ( scope ) . toBe ( customScope ) ;
147- done ( ) ;
148- } ) ;
149- } ) ;
157+ withIsolationScope ( customScope , scope => {
158+ expect ( getIsolationScope ( ) ) . toBe ( customScope ) ;
159+ expect ( scope ) . toBe ( customScope ) ;
160+ done ( ) ;
161+ } ) ;
162+ } ) ) ;
150163} ) ;
0 commit comments