Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you simulate an AbortController timeout? #5

Open
TomHart opened this issue Nov 3, 2023 · 0 comments
Open

Can you simulate an AbortController timeout? #5

TomHart opened this issue Nov 3, 2023 · 0 comments

Comments

@TomHart
Copy link

TomHart commented Nov 3, 2023

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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant