@@ -35,6 +35,7 @@ interface SetupArgs {
3535 retryCount ?: number ;
3636 sendError ?: boolean ;
3737 failFetch ?: boolean ;
38+ retryFailureStatus ?: number ;
3839}
3940
4041describe ( "fetchWithRetry" , ( ) => {
@@ -50,6 +51,7 @@ describe("fetchWithRetry", () => {
5051 sendError = false ,
5152 failFetch = false ,
5253 retryCount = 0 ,
54+ retryFailureStatus = 503 ,
5355 } : SetupArgs ) {
5456 consoleSpy = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => null ) ;
5557
@@ -64,7 +66,9 @@ describe("fetchWithRetry", () => {
6466 }
6567
6668 retryCount -= 1 ;
67- return new HttpResponse ( "not found" , { status : 404 } ) ;
69+ return new HttpResponse ( "service unavailable" , {
70+ status : retryFailureStatus ,
71+ } ) ;
6872 } ) ,
6973 ) ;
7074 }
@@ -92,6 +96,7 @@ describe("fetchWithRetry", () => {
9296 setup ( {
9397 data : { url : "http://example.com" } ,
9498 retryCount : 2 ,
99+ retryFailureStatus : 503 ,
95100 } ) ;
96101
97102 const urlPromise = await fetchWithRetry ( {
@@ -105,12 +110,43 @@ describe("fetchWithRetry", () => {
105110 } ) ;
106111 } ) ;
107112
113+ describe ( "when the initial response is a 4xx error" , ( ) => {
114+ it ( "returns the response without retrying" , async ( ) => {
115+ let requestCount = 0 ;
116+ consoleSpy = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => null ) ;
117+
118+ server . use (
119+ http . all ( "http://localhost" , ( ) => {
120+ requestCount += 1 ;
121+ return HttpResponse . json (
122+ { message : "Bad Request" , detail : "example error body" } ,
123+ { status : 400 } ,
124+ ) ;
125+ } ) ,
126+ ) ;
127+
128+ const response = await fetchWithRetry ( {
129+ url : "http://localhost" ,
130+ requestData : { } ,
131+ retryCount : 5 ,
132+ } ) ;
133+
134+ expect ( requestCount ) . toBe ( 1 ) ;
135+ expect ( response . status ) . toBe ( 400 ) ;
136+ await expect ( response . json ( ) ) . resolves . toEqual ( {
137+ message : "Bad Request" ,
138+ detail : "example error body" ,
139+ } ) ;
140+ } ) ;
141+ } ) ;
142+
108143 describe ( "retry count exceeds limit" , ( ) => {
109144 it ( "returns the response" , async ( ) => {
110145 setup ( {
111146 data : { url : "http://example.com" } ,
112147 retryCount : 2 ,
113148 failFetch : true ,
149+ retryFailureStatus : 503 ,
114150 } ) ;
115151
116152 const response = await fetchWithRetry ( {
@@ -119,7 +155,7 @@ describe("fetchWithRetry", () => {
119155 retryCount : 1 ,
120156 } ) ;
121157
122- expect ( response . status ) . toBe ( 404 ) ;
158+ expect ( response . status ) . toBe ( 503 ) ;
123159 } ) ;
124160 } ) ;
125161
0 commit comments