Skip to content

Commit cbba814

Browse files
committed
updates
1 parent 5993edb commit cbba814

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

test/index.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,93 @@ describe("ofetch", () => {
344344
});
345345
});
346346

347+
it("retries when retryIf (sync) returns true", async () => {
348+
let called = 0;
349+
await $fetch(getURL("408"), {
350+
retry: 2,
351+
retryIf: () => {
352+
called++;
353+
return true;
354+
},
355+
}).catch(() => "failed");
356+
expect(called).to.equal(3);
357+
});
358+
359+
it("retries when retryIf (async) returns true", async () => {
360+
let called = 0;
361+
await $fetch(getURL("408"), {
362+
retry: 2,
363+
retryIf: async () => {
364+
called++;
365+
return true;
366+
},
367+
}).catch(() => "failed");
368+
expect(called).to.equal(3);
369+
});
370+
371+
it("does not retry when retryIf returns false and status does not match", async () => {
372+
let called = 0;
373+
await $fetch(getURL("404"), {
374+
retry: 2,
375+
retryIf: () => {
376+
called++;
377+
return false;
378+
},
379+
}).catch(() => "failed");
380+
expect(called).to.equal(1);
381+
});
382+
383+
it("retries on matching status even if retryIf returns false", async () => {
384+
let called = 0;
385+
await $fetch(getURL("408"), {
386+
retry: 2,
387+
retryIf: () => {
388+
called++;
389+
return false;
390+
},
391+
}).catch(() => "failed");
392+
expect(called).to.equal(3);
393+
});
394+
395+
it("throws if retryIf throws (consumer error) propagates error to consumer", async () => {
396+
let called = 0;
397+
await expect(
398+
$fetch(getURL("408"), {
399+
retry: 2,
400+
retryIf: () => {
401+
called++;
402+
throw new Error("bad predicate");
403+
},
404+
})
405+
).rejects.toThrow("bad predicate");
406+
expect(called).to.equal(1);
407+
});
408+
409+
it("retries when both retryStatusCodes and retryIf allow", async () => {
410+
let called = 0;
411+
await $fetch(getURL("408"), {
412+
retry: 2,
413+
retryStatusCodes: [408],
414+
retryIf: () => {
415+
called++;
416+
return true;
417+
},
418+
}).catch(() => "failed");
419+
expect(called).to.equal(3);
420+
});
421+
422+
it("retries when only retryStatusCodes match and retryIf is absent", async () => {
423+
let called = 0;
424+
await $fetch(getURL("408"), {
425+
retry: 2,
426+
retryStatusCodes: [408],
427+
onResponseError: () => {
428+
called++;
429+
},
430+
}).catch(() => "failed");
431+
expect(called).to.equal(3);
432+
});
433+
347434
it("deep merges defaultOptions", async () => {
348435
const _customFetch = $fetch.create({
349436
query: {

0 commit comments

Comments
 (0)