@@ -423,10 +423,7 @@ test('onError middleware is called when onResponse is not defined and error exis
423423 const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
424424 let errorMiddlewareCalled = false
425425
426- // onError is only called when onResponse is not defined and error exists
427- // The condition is: if (response && middleware.onResponse) - if onResponse is not defined, the block doesn't execute
428- // So onError is never called in the current implementation
429- // This test verifies the middleware structure exists
426+ // onError is called when there's an error and no onResponse handler returned a result
430427 api . use ( {
431428 onError : async ( { error } ) => {
432429 errorMiddlewareCalled = true
@@ -437,9 +434,8 @@ test('onError middleware is called when onResponse is not defined and error exis
437434
438435 await api . get ( '/test' as never )
439436
440- // Note: onError is not called because the condition requires response && middleware.onResponse
441- // If onResponse is not defined, the entire block is skipped
442- expect ( errorMiddlewareCalled ) . toBe ( false )
437+ // onError should be called when there's an error response (404)
438+ expect ( errorMiddlewareCalled ) . toBe ( true )
443439} )
444440
445441test ( 'onError middleware can return Error' , async ( ) => {
@@ -455,7 +451,7 @@ test('onError middleware can return Error', async () => {
455451 const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
456452 const customError = new Error ( 'Custom error from middleware' )
457453
458- // onError is registered but won't be called due to the condition check
454+ // onError returns a custom Error to replace the original error
459455 api . use ( {
460456 onError : async ( ) => customError ,
461457 } )
@@ -466,9 +462,8 @@ test('onError middleware can return Error', async () => {
466462 response : Response
467463 }
468464
469- // Since onError is not called, error comes from convertResponse
470- expect ( result . error ) . toBeDefined ( )
471- expect ( result . error ) . not . toBe ( customError )
465+ // onError is called and returns the custom error
466+ expect ( result . error ) . toBe ( customError )
472467} )
473468
474469test ( 'onError middleware can return Response' , async ( ) => {
@@ -487,7 +482,7 @@ test('onError middleware can return Response', async () => {
487482 headers : { 'Content-Type' : 'application/json' } ,
488483 } )
489484
490- // onError is registered but won't be called due to the condition check
485+ // onError returns a recovery Response to replace the error
491486 api . use ( {
492487 onError : async ( ) => recoveryResponse ,
493488 } )
@@ -498,9 +493,8 @@ test('onError middleware can return Response', async () => {
498493 response : Response
499494 }
500495
501- // Since onError is not called, response comes from convertResponse
502- expect ( result . response ) . toBeDefined ( )
503- expect ( result . response ) . not . toBe ( recoveryResponse )
496+ // onError is called and returns the recovery response
497+ expect ( result . response ) . toBe ( recoveryResponse )
504498} )
505499
506500test ( 'middleware can be passed in request options' , async ( ) => {
0 commit comments