type-guards.ts
This file
Path
test/type-guards.ts
110 lines · 3.1 KiB · TypeScript
Last changed by
Stop surfacing progress callback errorsSindre Sorhusbefore adoption13c8da5
History of this fileTake it
Avoid misidentifying unrelated errors as Ky errorsSindre Sorhusbefore adoptiond636438
lines 1–2why, one step back1import {setTimeout as delay} from 'node:timers/promises';2import test from 'ava';Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93
lines 3–7why, one step back3import ky, {4 HTTPError,5 NetworkError,6 ForceRetryError,7 SchemaValidationError,Avoid misidentifying unrelated errors as Ky errorsSindre Sorhusbefore adoptiond636438
lines 8–14why, one step back8 TimeoutError,9 isKyError,10 isHTTPError,11 isNetworkError,12 isTimeoutError,13 isForceRetryError,14} from '../source/index.js';Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93
lines 15–42why, one step back1516test('error names can be customized without changing instance type guards', async t => {17 const request = new Request('https://example.com');18 const httpError = await t.throwsAsync(ky(request, {19 retry: 0,20 fetch: async () => new Response('Forbidden', {status: 403}),21 }), {instanceOf: HTTPError});2223 for (const [error, guard] of [24 [httpError, isHTTPError],25 [new NetworkError(request), isNetworkError],26 [new TimeoutError(request), isTimeoutError],27 [new ForceRetryError(), isForceRetryError],28 ] as const) {29 t.is(error.name, error.constructor.name);30 error.name = 'CustomError';31 t.true(error.toString().startsWith('CustomError: '));32 t.true(guard(error));33 t.true(isKyError(error));34 }3536 const validationError = new SchemaValidationError([{message: 'Invalid response'}]);37 t.is(validationError.name, 'SchemaValidationError');38 validationError.name = 'InvalidResponseError';39 t.true(validationError.toString().startsWith('InvalidResponseError: '));40 t.true(validationError instanceof SchemaValidationError);41 t.false(isKyError(validationError));42});Avoid misidentifying unrelated errors as Ky errorsSindre Sorhusbefore adoptiond636438
lines 43–85why, one step back4344test('AbortSignal.timeout errors are not Ky errors', async t => {45 const signal = AbortSignal.timeout(0);46 await delay(10);4748 t.true(signal.aborted);49 t.true(signal.reason instanceof DOMException);50 t.is(signal.reason.name, 'TimeoutError');51 t.false(isTimeoutError(signal.reason));52 t.false(isKyError(signal.reason));53});5455test('Ky timeout errors are recognized', t => {56 const error = new TimeoutError(new Request('https://example.com'));5758 t.true(isTimeoutError(error));59 t.true(isKyError(error));60});6162for (const [name, guard] of [63 ['HTTPError', isHTTPError],64 ['NetworkError', isNetworkError],65 ['TimeoutError', isTimeoutError],66 ['ForceRetryError', isForceRetryError],67] as const) {68 test(`${name} guard rejects unrelated errors with the same name`, t => {69 const error = new Error('Not from Ky');70 error.name = name;7172 t.false(guard(error));73 t.false(isKyError(error));74 });7576 test(`${name} guard recognizes branded errors from another Ky constructor`, t => {77 const error = Object.assign(new Error('From another Ky constructor'), {78 name,79 isKyError: true,80 });8182 t.true(guard(error));83 t.true(isKyError(error));84 });85}Skip upload progress wrapping for Request-like results and use the cross-realm guardsSindre Sorhusbefore adoptionf3b1f7d
lines 86–110why, one step back8687// Ky ships `isForceRetryError()` for cross-realm use, but its own retry logic used `instanceof`, so a `ForceRetryError` from a second copy of Ky was silently never retried.88test('a branded ForceRetryError from another Ky copy still forces a retry', async t => {89 let attempts = 0;90 const branded = Object.assign(new Error('From another Ky constructor'), {91 name: 'ForceRetryError',92 isKyError: true,93 });9495 const text = await ky('https://example.com', {96 async fetch() {97 attempts++;98 if (attempts === 1) {99 throw branded;100 }101102 return new Response('retried');103 },104 retry: {limit: 1, delay: () => 0},105 }).text();106107 t.is(attempts, 2);108 t.is(text, 'retried');109});110