@@ -42,12 +42,86 @@ describe('withScope', () => {
4242 expect ( res ) . toBe ( 'foo' ) ;
4343 } ) ;
4444
45- it ( 'works with an async function' , async ( ) => {
45+ it ( 'works with an async function return value ' , async ( ) => {
4646 const res = withScope ( async scope => {
4747 return 'foo' ;
4848 } ) ;
4949
5050 expect ( res ) . toBeInstanceOf ( Promise ) ;
5151 expect ( await res ) . toBe ( 'foo' ) ;
5252 } ) ;
53+
54+ it ( 'correctly sets & resets the current scope' , ( ) => {
55+ const scope1 = getCurrentScope ( ) ;
56+
57+ withScope ( scope2 => {
58+ expect ( scope2 ) . not . toBe ( scope1 ) ;
59+ expect ( getCurrentScope ( ) ) . toBe ( scope2 ) ;
60+ } ) ;
61+
62+ withScope ( scope3 => {
63+ expect ( scope3 ) . not . toBe ( scope1 ) ;
64+ expect ( getCurrentScope ( ) ) . toBe ( scope3 ) ;
65+ } ) ;
66+
67+ expect ( getCurrentScope ( ) ) . toBe ( scope1 ) ;
68+ } ) ;
69+
70+ it ( 'correctly sets & resets the current scope with async functions' , async ( ) => {
71+ const scope1 = getCurrentScope ( ) ;
72+
73+ await withScope ( async scope2 => {
74+ expect ( scope2 ) . not . toBe ( scope1 ) ;
75+ expect ( getCurrentScope ( ) ) . toBe ( scope2 ) ;
76+
77+ await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
78+
79+ expect ( getCurrentScope ( ) ) . toBe ( scope2 ) ;
80+ } ) ;
81+
82+ await withScope ( async scope3 => {
83+ expect ( scope3 ) . not . toBe ( scope1 ) ;
84+ expect ( getCurrentScope ( ) ) . toBe ( scope3 ) ;
85+
86+ await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
87+
88+ expect ( getCurrentScope ( ) ) . toBe ( scope3 ) ;
89+ } ) ;
90+
91+ expect ( getCurrentScope ( ) ) . toBe ( scope1 ) ;
92+ } ) ;
93+
94+ it ( 'correctly sets & resets the current scope when an error happens' , ( ) => {
95+ const scope1 = getCurrentScope ( ) ;
96+
97+ const error = new Error ( 'foo' ) ;
98+
99+ expect ( ( ) =>
100+ withScope ( scope2 => {
101+ expect ( scope2 ) . not . toBe ( scope1 ) ;
102+ expect ( getCurrentScope ( ) ) . toBe ( scope2 ) ;
103+
104+ throw error ;
105+ } ) ,
106+ ) . toThrow ( error ) ;
107+
108+ expect ( getCurrentScope ( ) ) . toBe ( scope1 ) ;
109+ } ) ;
110+
111+ it ( 'correctly sets & resets the current scope with async functions & errors' , async ( ) => {
112+ const scope1 = getCurrentScope ( ) ;
113+
114+ const error = new Error ( 'foo' ) ;
115+
116+ await expect (
117+ withScope ( async scope2 => {
118+ expect ( scope2 ) . not . toBe ( scope1 ) ;
119+ expect ( getCurrentScope ( ) ) . toBe ( scope2 ) ;
120+
121+ throw error ;
122+ } ) ,
123+ ) . rejects . toBe ( error ) ;
124+
125+ expect ( getCurrentScope ( ) ) . toBe ( scope1 ) ;
126+ } ) ;
53127} ) ;
0 commit comments