@@ -21,6 +21,15 @@ async function throwingFunction(_params: Record<string, unknown>): Promise<{ ok:
2121 throw new Error ( 'boom' ) ;
2222}
2323
24+ class GraphQLError extends Error {
25+ name = 'GraphQLError'
26+ }
27+
28+ async function throwingCustomError ( _params : Record < string , unknown > ) : Promise < { ok : boolean ; } > {
29+ silenceConsoleError ( ) ;
30+ throw new GraphQLError ( 'validation error' ) ;
31+ }
32+
2433async function successfulFunction ( params : Record < string , unknown > ) : Promise < { ok : boolean ; } > {
2534 return { ok : true , ...params } ;
2635}
@@ -40,6 +49,7 @@ class TestClass {
4049describe ( 'Try' , ( ) => {
4150 afterEach ( ( ) => {
4251 vi . restoreAllMocks ( ) ;
52+ Try . throwThroughErrorTypes ( [ ] ) ;
4353 } ) ;
4454
4555 it ( 'should return default value' , async ( ) => {
@@ -148,30 +158,34 @@ describe('Try', () => {
148158
149159 it ( 'should return the function result' , async ( ) => {
150160 const params = { parameterKey : 'alpha' } ;
161+
151162 const result = await new Try ( successfulFunction , params ) . unwrap ( ) ;
163+
152164 expect ( result ) . toEqual ( { ok : true , ...params } ) ;
153- // captureException should not be called on success
154165 expect ( Sentry . captureException ) . not . toHaveBeenCalled ( ) ;
155166 } ) ;
156167
157168 it ( 'should return a class method result' , async ( ) => {
158169 const greeting = 'Hi!' ;
159170 const newTest = new TestClass ( 'newTest' ) ;
171+
160172 const result = await new Try ( newTest . greet . bind ( newTest ) , { greeting } ) . unwrap ( ) ;
173+
161174 expect ( result ) . toEqual ( 'Hi!, I\'m newTest' ) ;
162- // captureException should not be called on success
163175 expect ( Sentry . captureException ) . not . toHaveBeenCalled ( ) ;
164176 } ) ;
165177
166178 it ( 'should add tags' , async ( ) => {
167179 const params = { parameterKey : 'alpha' } ;
180+
168181 const exec = new Try ( throwingFunction , params )
169182 . report ( 'failed' )
170183 . tag ( 'name' , 'value' )
171184 . tag ( 'test' , 'true' )
172185 . unwrap ( ) ;
186+
173187 await expect ( exec ) . rejects . toThrow ( 'failed' ) ;
174- // captureException should not be called on success
188+
175189 expect ( Sentry . captureException ) . toBeCalledWith ( new Error ( 'failed' , {
176190 cause : new Error ( 'boom' )
177191 } ) , {
@@ -183,10 +197,23 @@ describe('Try', () => {
183197 } ) ;
184198 } ) ;
185199
186- it ( 'should returns the actual error' , async ( ) => {
200+ it ( 'should return the actual error' , async ( ) => {
187201 const params = { parameterKey : 'alpha' } ;
202+
188203 const result = await new Try ( throwingFunction , params )
189204 . error ( ) ;
205+
190206 expect ( result ) . toEqual ( new Error ( 'boom' ) ) ;
191207 } ) ;
208+
209+ it ( 'should return the actual error' , async ( ) => {
210+ Try . throwThroughErrorTypes ( [ 'GraphQLError' ] ) ;
211+ const params = { parameterKey : 'alpha' } ;
212+
213+ const exec = new Try ( throwingCustomError , params )
214+ . report ( 'failed' )
215+ . unwrap ( ) ;
216+
217+ await expect ( exec ) . rejects . toThrow ( 'validation error' ) ;
218+ } ) ;
192219} ) ;
0 commit comments