Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/create-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@ describe("create-client", () => {

nockScope.done();
});

it("does not hang when the API is unreachable and the fetch times out", async () => {
const originalFetch = global.fetch;
const timeoutError = new DOMException(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of think it would feel better if we wrapped this error (instead of passing the DOMException straight through), but i don't feel too strongly about it if you disagree.

"The operation timed out.",
"TimeoutError",
);
const mockFetch = jest.fn().mockRejectedValue(timeoutError);
global.fetch = mockFetch;

try {
client = await createClient("client_123abc", {
devMode: false,
redirectUri: "https://example.com/",
});

// initialize() has a catch-all around #refreshSession, so
// createClient should resolve even though the fetch failed
// (rather than hanging forever, as it would with no timeout).
expect(client).toBeDefined();
expect(client.getUser()).toBeNull();
expect(mockFetch).toHaveBeenCalledTimes(1);

// verify the request had an AbortSignal attached for the timeout
const fetchOptions = mockFetch.mock.calls[0][1];
expect(fetchOptions.signal).toBeInstanceOf(AbortSignal);
} finally {
global.fetch = originalFetch;
}
});
});

describe("when devMode is true", () => {
Expand Down
1 change: 1 addition & 0 deletions src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class HttpClient {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(30_000),
});
}

Expand Down
Loading