@@ -12,7 +12,7 @@ describe('retryRequestHandler', () => {
12
12
const requestConfig : InternalAxiosRequestConfig = { headers : { } as AxiosHeaders } ;
13
13
const updatedConfig = retryRequestHandler ( requestConfig ) ;
14
14
15
- expect ( updatedConfig . retryCount ) . toBe ( 0 ) ;
15
+ expect ( updatedConfig . retryCount ) . toBe ( 1 ) ;
16
16
} ) ;
17
17
} ) ;
18
18
@@ -40,40 +40,65 @@ describe('retryResponseErrorHandler', () => {
40
40
it ( 'should reject the promise if retryOnError is false' , async ( ) => {
41
41
const error = { config : { retryOnError : false } , code : 'ECONNABORTED' } ;
42
42
const config = { retryLimit : 5 } ;
43
-
44
- await expect ( retryResponseErrorHandler ( error , config ) ) . rejects . toBe ( error ) ;
43
+ const client = axios . create ( ) ;
44
+
45
+ try {
46
+ await retryResponseErrorHandler ( error , config , client ) ;
47
+ fail ( 'Expected retryResponseErrorHandler to throw an error' ) ;
48
+ } catch ( err ) {
49
+ expect ( err ) . toEqual ( expect . objectContaining ( {
50
+ code : 'ECONNABORTED' ,
51
+ config : expect . objectContaining ( { retryOnError : false } ) ,
52
+ } ) ) ;
53
+ }
45
54
} ) ;
46
55
it ( 'should reject the promise if retryOnError is true' , async ( ) => {
47
56
const error = { config : { retryOnError : true } } ;
48
57
const config = { retryLimit : 5 } ;
49
-
50
- await expect ( retryResponseErrorHandler ( error , config ) ) . rejects . toBe ( error ) ;
58
+ const client = axios . create ( ) ;
59
+
60
+ try {
61
+ await retryResponseErrorHandler ( error , config , client ) ;
62
+ fail ( 'Expected retryResponseErrorHandler to throw an error' ) ;
63
+ } catch ( err : any ) {
64
+ expect ( err . config ) . toEqual ( expect . objectContaining ( { retryOnError : true } ) ) ;
65
+ expect ( err ) . toEqual ( error ) ;
66
+ }
51
67
} ) ;
52
68
it ( 'should resolve the promise to 408 error if retryOnError is true and error code is ECONNABORTED' , async ( ) => {
53
69
const error = { config : { retryOnError : true , retryCount : 1 } , code : 'ECONNABORTED' } ;
54
70
const config = { retryLimit : 5 , timeout : 1000 } ;
55
-
56
- const errorResponse = { status : 408 , statusText : 'timeout of 1000ms exceeded' } ;
57
-
58
- await expect ( retryResponseErrorHandler ( error , config ) ) . resolves . toEqual ( errorResponse ) ;
71
+ const client = axios . create ( ) ;
72
+ try {
73
+ await retryResponseErrorHandler ( error , config , client ) ;
74
+ fail ( 'Expected retryResponseErrorHandler to throw an error' ) ;
75
+ } catch ( err ) {
76
+ expect ( err ) . toEqual ( expect . objectContaining ( {
77
+ error_code : 408 ,
78
+ error_message : `Timeout of ${ config . timeout } ms exceeded` ,
79
+ errors : null
80
+ } ) ) ;
81
+ }
59
82
} ) ;
60
83
it ( 'should reject the promise if response status is 429 and retryCount exceeds retryLimit' , async ( ) => {
61
84
const error = {
62
85
config : { retryOnError : true , retryCount : 5 } ,
63
86
response : { status : 429 , statusText : 'timeout of 1000ms exceeded' } ,
64
87
} ;
65
88
const config = { retryLimit : 5 , timeout : 1000 } ;
89
+ const client = axios . create ( ) ;
66
90
67
- await expect ( retryResponseErrorHandler ( error , config ) ) . rejects . toBe ( error ) ;
91
+ await expect ( retryResponseErrorHandler ( error , config , client ) ) . rejects . toBe ( error ) ;
68
92
} ) ;
69
93
it ( 'should reject the promise if response status is 401 and retryCount exceeds retryLimit' , async ( ) => {
70
94
const error = {
71
95
config : { retryOnError : true , retryCount : 5 } ,
72
96
response : { status : 401 , statusText : 'timeout of 1000ms exceeded' } ,
73
97
} ;
74
98
const config = { retryLimit : 5 , timeout : 1000 } ;
99
+ const client = axios . create ( ) ;
75
100
76
- await expect ( retryResponseErrorHandler ( error , config ) ) . rejects . toBe ( error ) ;
101
+ await expect ( retryResponseErrorHandler ( error , config , client ) ) . rejects . toBe ( error ) ;
77
102
} ) ;
78
103
it ( 'should reject the promise if response status is 429 or 401 and retryCount is within limit' , async ( ) => {
79
104
const error = {
@@ -87,17 +112,24 @@ describe('retryResponseErrorHandler', () => {
87
112
} ,
88
113
} ;
89
114
const config = { retryLimit : 5 , timeout : 1000 } ;
115
+ const client = axios . create ( ) ;
90
116
91
117
const finalResponseObj = {
92
- config : { retryOnError : true , retryCount : 5 } ,
118
+ config : { retryOnError : true , retryCount : 4 } ,
93
119
response : { status : 429 , statusText : 'timeout of 1000ms exceeded' } ,
94
120
} ;
95
121
96
122
mock . onPost ( '/retryURL' ) . reply ( 200 , finalResponseObj ) ;
97
123
98
- const finalResponse = await retryResponseErrorHandler ( error , config ) ;
124
+ try {
125
+ await retryResponseErrorHandler ( error , config , client ) ;
126
+ throw new Error ( 'Expected retryResponseErrorHandler to throw an error' ) ;
127
+ } catch ( err : any ) {
128
+ expect ( err . response . status ) . toBe ( 429 ) ;
129
+ expect ( err . response . statusText ) . toBe ( error . response . statusText ) ;
130
+ expect ( err . config . retryCount ) . toBe ( error . config . retryCount ) ;
131
+ }
99
132
100
- expect ( finalResponse . data ) . toEqual ( finalResponseObj ) ;
101
133
} ) ;
102
134
it ( 'should call the retry function if retryCondition is passed' , async ( ) => {
103
135
const error = {
@@ -113,6 +145,7 @@ describe('retryResponseErrorHandler', () => {
113
145
// eslint-disable-next-line @typescript-eslint/no-shadow
114
146
const retryCondition = ( error : any ) => true ;
115
147
const config = { retryLimit : 5 , timeout : 1000 , retryCondition : retryCondition } ;
148
+ const client = axios . create ( ) ;
116
149
117
150
const finalResponseObj = {
118
151
config : { retryOnError : true , retryCount : 5 } ,
@@ -121,7 +154,7 @@ describe('retryResponseErrorHandler', () => {
121
154
122
155
mock . onPost ( '/retryURL' ) . reply ( 200 , finalResponseObj ) ;
123
156
124
- const finalResponse = await retryResponseErrorHandler ( error , config ) ;
157
+ const finalResponse : any = await retryResponseErrorHandler ( error , config , client ) ;
125
158
126
159
expect ( finalResponse . data ) . toEqual ( finalResponseObj ) ;
127
160
} ) ;
@@ -139,6 +172,7 @@ describe('retryResponseErrorHandler', () => {
139
172
// eslint-disable-next-line @typescript-eslint/no-shadow
140
173
const retryCondition = ( error : any ) => true ;
141
174
const config = { retryLimit : 5 , timeout : 1000 , retryCondition : retryCondition } ;
175
+ const client = axios . create ( ) ;
142
176
143
177
const finalResponseObj = {
144
178
config : { retryOnError : true , retryCount : 5 } ,
@@ -147,6 +181,6 @@ describe('retryResponseErrorHandler', () => {
147
181
148
182
mock . onPost ( '/retryURL' ) . reply ( 200 , finalResponseObj ) ;
149
183
150
- await expect ( retryResponseErrorHandler ( error , config ) ) . rejects . toBe ( error ) ;
184
+ await expect ( retryResponseErrorHandler ( error , config , client ) ) . rejects . toBe ( error ) ;
151
185
} ) ;
152
186
} ) ;
0 commit comments