Skip to content

Can you simulate an AbortController timeout? #5

@TomHart

Description

@TomHart

I'm trying to use this to test my retry fetch util, but it doesn't seem to be working. I can see the timeout is being called, but it doesn't seem to timeout the request. Do you know if this is possible to test?

Code

const {timeout = 10000} = options;

const controller = new AbortController();
const id = setTimeout(() => {
    controller.abort();
}, timeout);

res = await fetch(url, {
    ...options,
    signal: controller.signal
});
clearTimeout(id);

Test

let called = 0;

mf.mock("GET@/api/hello/:name", async (_req: Request, params: any) => {
    if (called++ === 0) {
        await sleep(2);
        return new Response();
    }

    return new Response(`Hello, ${params["name"]} ${called}!`, {
        status: 200,
    });
});

const res = await retryFetch("https://localhost:1234/api/hello/SeparateRecords", {timeout: 1000});
assertEquals(2, called);
assertEquals('Hello, SeparateRecords 2!', await res.text());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions