Skip to content

Commit c13ec06

Browse files
committed
Minor test tweaks
1 parent c8566af commit c13ec06

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

src/FetchClient.test.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -317,51 +317,54 @@ Deno.test("can deleteJSON with client middleware", async () => {
317317
assertEquals(r.data!.completed, false);
318318
});
319319

320-
Deno.test("can abort getJSON", () => {
320+
Deno.test("can abort getJSON", async () => {
321321
const provider = new FetchClientProvider();
322-
const controller = new AbortController();
323-
let responseTimeout: ReturnType<typeof setTimeout>;
324-
const fakeFetch = (r: unknown): Promise<Response> =>
325-
new Promise((resolve) => {
326-
const request = r as Request;
327-
request.signal.addEventListener("abort", () => {
328-
clearTimeout(responseTimeout);
329-
resolve(
330-
new Response(null, {
331-
status: 299,
332-
statusText: "The user aborted a request.",
333-
}),
334-
);
335-
});
336-
responseTimeout = setTimeout(function () {
337-
resolve(new Response());
338-
}, 1000);
339-
});
340-
341-
provider.fetch = fakeFetch;
342322
const client = provider.getFetchClient();
343-
client
344-
.getJSON("https://jsonplaceholder.typicode.com/todos/1", {
323+
let gotError = false;
324+
325+
try {
326+
const controller = new AbortController();
327+
setTimeout(() => {
328+
controller.abort("Signal was aborted");
329+
}, 100);
330+
331+
await client.getJSON("https://dummyjson.com/products/1?delay=2000", {
332+
timeout: 500,
345333
signal: controller.signal,
346-
})
347-
.then((r) => {
348-
assert(r.ok);
349-
assertEquals(r.status, 299);
350-
assertEquals(r.statusText, "The user aborted a request.");
351334
});
352-
controller.abort();
335+
} catch (error) {
336+
assertEquals(error, "Signal was aborted");
337+
gotError = true;
338+
}
339+
340+
assert(gotError);
341+
342+
// can use expectedStatusCodes to not throw an error
343+
const response = await client.getJSON(
344+
"https://dummyjson.com/products/1?delay=2000",
345+
{
346+
timeout: 500,
347+
signal: AbortSignal.timeout(100),
348+
expectedStatusCodes: [408],
349+
},
350+
);
351+
352+
assertEquals(response.status, 408);
353+
assertEquals(response.statusText, "Request Timeout");
354+
assertEquals(response.problem?.status, 408);
353355
});
354356

355357
Deno.test("can getJSON with timeout", async () => {
356358
const provider = new FetchClientProvider();
357-
const controller = new AbortController();
358359

359360
const client = provider.getFetchClient();
360361
let gotError = false;
361362

362363
try {
364+
// timeout is set to 100ms, but the request takes 2000ms
365+
// so it should throw a timeout error
363366
await client.getJSON("https://dummyjson.com/products/1?delay=2000", {
364-
timeout: 500,
367+
timeout: 100,
365368
});
366369
} catch (error) {
367370
assertEquals((error as Response).status, 408);
@@ -371,10 +374,11 @@ Deno.test("can getJSON with timeout", async () => {
371374
assert(gotError);
372375
gotError = false;
373376

377+
// can use expectedStatusCodes to not throw an error
374378
const response = await client.getJSON(
375379
"https://dummyjson.com/products/1?delay=2000",
376380
{
377-
timeout: 500,
381+
timeout: 100,
378382
expectedStatusCodes: [408],
379383
},
380384
);
@@ -383,13 +387,18 @@ Deno.test("can getJSON with timeout", async () => {
383387
assertEquals(response.statusText, "Request Timeout");
384388

385389
try {
386-
controller.abort("TestAbort");
390+
const controller = new AbortController();
391+
setTimeout(() => {
392+
controller.abort("Signal was aborted");
393+
}, 100);
394+
395+
// both timeout and signal are set
387396
await client.getJSON("https://dummyjson.com/products/1?delay=2000", {
388397
timeout: 500,
389398
signal: controller.signal,
390399
});
391400
} catch (error) {
392-
assertEquals(error, "TestAbort");
401+
assertEquals(error, "Signal was aborted");
393402
gotError = true;
394403
}
395404

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy