@@ -20,10 +20,10 @@ declare global {
20
20
}
21
21
}
22
22
23
- describe ( 'jsonFetch' , function ( ) {
23
+ describe ( 'jsonFetch' , ( ) => {
24
24
const { sandbox} = useSinonSandbox ( ) ;
25
- describe ( 'single request with no retry' , function ( ) {
26
- it ( 'resolves with json body for 200 status codes' , async function ( ) {
25
+ describe ( 'single request with no retry' , ( ) => {
26
+ it ( 'resolves with json body for 200 status codes' , async ( ) => {
27
27
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 , {
28
28
name : 'apple' ,
29
29
} ) ;
@@ -36,7 +36,7 @@ describe('jsonFetch', function () {
36
36
expect ( response . headers ) . to . be . ok ( ) ;
37
37
} ) ;
38
38
39
- it ( 'resolves with JSON body for 500 status codes' , async function ( ) {
39
+ it ( 'resolves with JSON body for 500 status codes' , async ( ) => {
40
40
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 500 , '"Something went wrong"' , {
41
41
'Content-Type' : 'application/json' ,
42
42
} ) ;
@@ -46,23 +46,23 @@ describe('jsonFetch', function () {
46
46
expect ( response . statusText ) . to . equal ( 'Internal Server Error' ) ;
47
47
expect ( response . headers ) . to . be . ok ( ) ;
48
48
} ) ;
49
- it ( 'resolves with JSON body when content-type contains other values but includes application/json' , async function ( ) {
49
+ it ( 'resolves with JSON body when content-type contains other values but includes application/json' , async ( ) => {
50
50
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 204 , '[{}]' , {
51
51
'Content-Type' : 'application/json; charset=utf-8' ,
52
52
} ) ;
53
53
const response = await jsonFetch ( 'http://www.test.com/products/1234' ) ;
54
54
expect ( response . body ) . to . deep . equal ( [ { } ] ) ;
55
55
} ) ;
56
56
57
- it ( 'resolves with non-JSON body' , async function ( ) {
57
+ it ( 'resolves with non-JSON body' , async ( ) => {
58
58
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 , 'This is not JSON' , {
59
59
'Content-Type' : 'text/plain' ,
60
60
} ) ;
61
61
const response = await jsonFetch ( 'http://www.test.com/products/1234' ) ;
62
62
expect ( response . body ) . to . equal ( undefined ) ;
63
63
} ) ;
64
- it ( 'rejects when there is a connection error' , async function ( ) {
65
- sandbox . stub ( global , 'fetch' ) . callsFake ( async function ( ) {
64
+ it ( 'rejects when there is a connection error' , async ( ) => {
65
+ sandbox . stub ( global , 'fetch' ) . callsFake ( async ( ) => {
66
66
throw new Error ( 'Something is broken!' ) ;
67
67
} ) ;
68
68
let errorThrown = false ;
@@ -79,7 +79,7 @@ describe('jsonFetch', function () {
79
79
expect ( errorThrown ) . to . be . true ( ) ;
80
80
} ) ;
81
81
82
- it ( 'rejects with responseText when there is a json parse error' , async function ( ) {
82
+ it ( 'rejects with responseText when there is a json parse error' , async ( ) => {
83
83
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 , 'foo' , {
84
84
'Content-Type' : 'application/json; charset=utf-8' ,
85
85
} ) ;
@@ -99,7 +99,7 @@ describe('jsonFetch', function () {
99
99
expect ( errorThrown ) . to . be . true ( ) ;
100
100
} ) ;
101
101
102
- it ( 'sends json request body' , async function ( ) {
102
+ it ( 'sends json request body' , async ( ) => {
103
103
nock ( 'http://www.test.com' )
104
104
. post ( '/products/1234' , {
105
105
name : 'apple' ,
@@ -123,7 +123,7 @@ describe('jsonFetch', function () {
123
123
expect ( response . headers ) . to . be . ok ( ) ;
124
124
} ) ;
125
125
126
- it ( 'calls onRequestStart when is passed to jsonFetch in JsonFetchOptions' , async function ( ) {
126
+ it ( 'calls onRequestStart when is passed to jsonFetch in JsonFetchOptions' , async ( ) => {
127
127
const onRequestStart = sandbox . stub ( ) ;
128
128
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 ) ;
129
129
await jsonFetch ( 'http://www.test.com/products/1234' , {
@@ -138,7 +138,7 @@ describe('jsonFetch', function () {
138
138
} ) ;
139
139
} ) ;
140
140
141
- it ( 'calls onRequestEnd when is passed to jsonFetch in JsonFetchOptions' , async function ( ) {
141
+ it ( 'calls onRequestEnd when is passed to jsonFetch in JsonFetchOptions' , async ( ) => {
142
142
const onRequestEnd = sandbox . stub ( ) ;
143
143
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 ) ;
144
144
await jsonFetch ( 'http://www.test.com/products/1234' , {
@@ -153,8 +153,8 @@ describe('jsonFetch', function () {
153
153
} ) ;
154
154
} ) ;
155
155
156
- it ( 'onRequestEnd returns error object when request fails' , async function ( ) {
157
- sandbox . stub ( global , 'fetch' ) . callsFake ( async function ( ) {
156
+ it ( 'onRequestEnd returns error object when request fails' , async ( ) => {
157
+ sandbox . stub ( global , 'fetch' ) . callsFake ( async ( ) => {
158
158
throw new Error ( 'Something is broken!' ) ;
159
159
} ) ;
160
160
const onRequestEnd = sandbox . stub ( ) ;
@@ -175,8 +175,8 @@ describe('jsonFetch', function () {
175
175
} ) ;
176
176
} ) ;
177
177
178
- describe ( 'expected statuses' , function ( ) {
179
- it ( 'errors with FetchUnexpectedStatus if the response has an unexpected status code' , async function ( ) {
178
+ describe ( 'expected statuses' , ( ) => {
179
+ it ( 'errors with FetchUnexpectedStatus if the response has an unexpected status code' , async ( ) => {
180
180
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 400 , 'not found' ) ;
181
181
182
182
try {
@@ -195,21 +195,21 @@ describe('jsonFetch', function () {
195
195
throw new Error ( 'expected to throw' ) ;
196
196
} ) ;
197
197
198
- it ( 'returns a response with an expected status code' , async function ( ) {
198
+ it ( 'returns a response with an expected status code' , async ( ) => {
199
199
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 201 , 'not found' ) ;
200
200
const response = await jsonFetch ( 'http://www.test.com/products/1234' , {
201
201
expectedStatuses : [ 201 ] ,
202
202
} ) ;
203
203
expect ( response ) . to . have . property ( 'status' , 201 ) ;
204
204
} ) ;
205
- it ( 'returns a response without an expected status code' , async function ( ) {
205
+ it ( 'returns a response without an expected status code' , async ( ) => {
206
206
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 404 , 'not found' ) ;
207
207
const response = await jsonFetch ( 'http://www.test.com/products/1234' ) ;
208
208
expect ( response ) . to . have . property ( 'status' , 404 ) ;
209
209
} ) ;
210
210
} ) ;
211
211
212
- describe ( 'retry' , function ( ) {
212
+ describe ( 'retry' , ( ) => {
213
213
let fetchSpy : sinon . SinonSpy <
214
214
[ input : RequestInfo , init ?: RequestInit | undefined ] ,
215
215
Promise < Response >
@@ -223,13 +223,13 @@ describe('jsonFetch', function () {
223
223
fetchSpy . restore ( ) ;
224
224
} ) ;
225
225
226
- it ( 'does not retry by default' , async function ( ) {
226
+ it ( 'does not retry by default' , async ( ) => {
227
227
nock ( 'http://www.test.com' ) . get ( '/' ) . reply ( 200 , { } ) ;
228
228
await jsonFetch ( 'http://www.test.com/' ) ;
229
229
expect ( fetchSpy . callCount ) . to . equal ( 1 ) ;
230
230
} ) ;
231
231
232
- it ( 'does not retry and calls OnRequest callbacks one single time each by default' , async function ( ) {
232
+ it ( 'does not retry and calls OnRequest callbacks one single time each by default' , async ( ) => {
233
233
const onRequestStart = sandbox . stub ( ) ;
234
234
const onRequestEnd = sandbox . stub ( ) ;
235
235
nock ( 'http://www.test.com' ) . get ( '/' ) . reply ( 200 , { } ) ;
@@ -241,7 +241,7 @@ describe('jsonFetch', function () {
241
241
expect ( onRequestEnd ) . to . have . been . calledOnce ( ) ;
242
242
} ) ;
243
243
244
- it ( 'does specified number of retries' , async function ( ) {
244
+ it ( 'does specified number of retries' , async ( ) => {
245
245
nock ( 'http://www.test.com' ) . get ( '/' ) . reply ( 200 , { } ) ;
246
246
247
247
try {
@@ -263,7 +263,7 @@ describe('jsonFetch', function () {
263
263
throw new Error ( 'Should have failed' ) ;
264
264
} ) ;
265
265
266
- it ( 'respects the shouldRetry() function' , async function ( ) {
266
+ it ( 'respects the shouldRetry() function' , async ( ) => {
267
267
nock ( 'http://www.test.com' ) . get ( '/' ) . times ( 6 ) . reply ( 200 , { } ) ;
268
268
269
269
try {
@@ -282,7 +282,7 @@ describe('jsonFetch', function () {
282
282
} ) ;
283
283
} ) ;
284
284
285
- describe ( 'retry network errors' , function ( ) {
285
+ describe ( 'retry network errors' , ( ) => {
286
286
let fetchStub : sinon . SinonStub <
287
287
[ input : RequestInfo , init ?: RequestInit | undefined ] ,
288
288
Promise < Response >
@@ -296,7 +296,7 @@ describe('jsonFetch', function () {
296
296
fetchStub . restore ( ) ;
297
297
} ) ;
298
298
299
- it ( 'respects the should retry function for a network error' , async function ( ) {
299
+ it ( 'respects the should retry function for a network error' , async ( ) => {
300
300
fetchStub . rejects ( new Error ( 'ECONRST' ) ) ;
301
301
302
302
try {
@@ -316,7 +316,7 @@ describe('jsonFetch', function () {
316
316
throw new Error ( 'Should have failed' ) ;
317
317
} ) ;
318
318
319
- it ( 'adds the retryCount to the error' , async function ( ) {
319
+ it ( 'adds the retryCount to the error' , async ( ) => {
320
320
fetchStub . rejects ( new Error ( 'ECONRST' ) ) ;
321
321
322
322
try {
@@ -337,7 +337,7 @@ describe('jsonFetch', function () {
337
337
throw new Error ( 'Should have failed' ) ;
338
338
} ) ;
339
339
340
- it ( 'calls the onRequestStart and onRequestEnd functions in each retry' , async function ( ) {
340
+ it ( 'calls the onRequestStart and onRequestEnd functions in each retry' , async ( ) => {
341
341
const onRequestStart = sandbox . stub ( ) ;
342
342
const onRequestEnd = sandbox . stub ( ) ;
343
343
@@ -368,7 +368,7 @@ describe('jsonFetch', function () {
368
368
throw new Error ( 'Should have failed' ) ;
369
369
} ) ;
370
370
371
- it ( 'call the onRequestStart and onRequestEnd functions when non-retryable setup is passed' , async function ( ) {
371
+ it ( 'call the onRequestStart and onRequestEnd functions when non-retryable setup is passed' , async ( ) => {
372
372
const onRequestStart = sandbox . stub ( ) ;
373
373
const onRequestEnd = sandbox . stub ( ) ;
374
374
@@ -388,9 +388,9 @@ describe('jsonFetch', function () {
388
388
} ) ;
389
389
} ) ;
390
390
391
- describe ( 'retriers' , function ( ) {
392
- describe ( '.is5xx' , function ( ) {
393
- it ( 'accepts a 503 and 504 status codes' , async function ( ) {
391
+ describe ( 'retriers' , ( ) => {
392
+ describe ( '.is5xx' , ( ) => {
393
+ it ( 'accepts a 503 and 504 status codes' , async ( ) => {
394
394
expect (
395
395
retriers . is5xx (
396
396
new Response ( '' , {
@@ -407,7 +407,7 @@ describe('jsonFetch', function () {
407
407
) . to . equal ( true ) ;
408
408
} ) ;
409
409
410
- it ( 'rejects all other inputs' , async function ( ) {
410
+ it ( 'rejects all other inputs' , async ( ) => {
411
411
expect ( retriers . is5xx ( new Error ( fake . sentence ( ) ) ) ) . to . equal ( false ) ;
412
412
expect (
413
413
retriers . is5xx (
@@ -460,7 +460,7 @@ describe('jsonFetch', function () {
460
460
) . to . equal ( false ) ;
461
461
} ) ;
462
462
463
- describe ( 'used within jsonFetch' , function ( ) {
463
+ describe ( 'used within jsonFetch' , ( ) => {
464
464
let fetchStub : sinon . SinonStub <
465
465
[ input : RequestInfo , init ?: RequestInit | undefined ] ,
466
466
Promise < { status : number } >
@@ -474,7 +474,7 @@ describe('jsonFetch', function () {
474
474
fetchStub . restore ( ) ;
475
475
} ) ;
476
476
477
- it ( 'attempts to retry on a 5xx error code' , async function ( ) {
477
+ it ( 'attempts to retry on a 5xx error code' , async ( ) => {
478
478
const is5xxSpy = sandbox . spy ( retriers , 'is5xx' ) ;
479
479
480
480
fetchStub . resolves ( { status : 503 } ) ;
@@ -498,12 +498,12 @@ describe('jsonFetch', function () {
498
498
} ) ;
499
499
} ) ;
500
500
501
- describe ( '.isNetworkError' , function ( ) {
502
- it ( 'accepts any errors' , async function ( ) {
501
+ describe ( '.isNetworkError' , ( ) => {
502
+ it ( 'accepts any errors' , async ( ) => {
503
503
expect ( retriers . isNetworkError ( new Error ( fake . sentence ( ) ) ) ) . to . equal ( true ) ;
504
504
} ) ;
505
505
506
- it ( 'rejects any non errors' , async function ( ) {
506
+ it ( 'rejects any non errors' , async ( ) => {
507
507
expect ( retriers . isNetworkError ( new Response ( 'foo' ) ) ) . to . equal ( false ) ;
508
508
expect ( retriers . isNetworkError ( new Response ( '' ) ) ) . to . equal ( false ) ;
509
509
expect (
@@ -522,7 +522,7 @@ describe('jsonFetch', function () {
522
522
) . to . equal ( false ) ;
523
523
} ) ;
524
524
525
- describe ( 'used within jsonFetch' , function ( ) {
525
+ describe ( 'used within jsonFetch' , ( ) => {
526
526
let fetchStub : sinon . SinonStub <
527
527
[ input : RequestInfo , init ?: RequestInit | undefined ] ,
528
528
Promise < { status : number } >
@@ -536,7 +536,7 @@ describe('jsonFetch', function () {
536
536
fetchStub . restore ( ) ;
537
537
} ) ;
538
538
539
- it ( 'attempts to retry on a network error' , async function ( ) {
539
+ it ( 'attempts to retry on a network error' , async ( ) => {
540
540
const isNetworkErrorSpy = sandbox . spy ( retriers , 'isNetworkError' ) ;
541
541
fetchStub . rejects ( new Error ( 'ECONRST' ) ) ;
542
542
@@ -560,8 +560,8 @@ describe('jsonFetch', function () {
560
560
} ) ;
561
561
} ) ;
562
562
} ) ;
563
- describe ( 'malformed json' , function ( ) {
564
- it ( 'throws error with malformed text' , async function ( ) {
563
+ describe ( 'malformed json' , ( ) => {
564
+ it ( 'throws error with malformed text' , async ( ) => {
565
565
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 , '{"name": "apple""}' , {
566
566
'Content-Type' : 'application/json' ,
567
567
} ) ;
@@ -576,15 +576,15 @@ describe('jsonFetch', function () {
576
576
throw new Error ( 'expected to throw' ) ;
577
577
} ) ;
578
578
} ) ;
579
- describe ( 'missing content type' , function ( ) {
580
- it ( 'handles it gracefully' , async function ( ) {
579
+ describe ( 'missing content type' , ( ) => {
580
+ it ( 'handles it gracefully' , async ( ) => {
581
581
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 , 'test' , { } ) ;
582
582
const response = await jsonFetch ( 'http://www.test.com/products/1234' ) ;
583
583
expect ( response . body ) . to . equal ( undefined ) ;
584
584
} ) ;
585
585
} ) ;
586
- describe ( 'thrown errors' , function ( ) {
587
- it ( 'does not include request headers' , async function ( ) {
586
+ describe ( 'thrown errors' , ( ) => {
587
+ it ( 'does not include request headers' , async ( ) => {
588
588
nock ( 'http://www.test.com' ) . get ( '/products/1234' ) . reply ( 200 , '{""}' , {
589
589
'Content-Type' : 'application/json' ,
590
590
} ) ;
0 commit comments