|
1 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { |
3 | 3 | createAbortableFetchMock, |
4 | 4 | createJsonResponse, |
@@ -21,6 +21,14 @@ async function expectAbortError(promise: Promise<unknown>) {
|
21 | 21 | } |
22 | 22 | |
23 | 23 | describe("createDiscordRequestClient", () => { |
| 24 | +beforeEach(() => { |
| 25 | +vi.useRealTimers(); |
| 26 | +}); |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | +vi.useRealTimers(); |
| 30 | +}); |
| 31 | + |
24 | 32 | it("preserves the REST client's abort signal for proxied fetch calls", async () => { |
25 | 33 | const fetchSpy = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => { |
26 | 34 | if (!(init?.signal instanceof AbortSignal)) { |
@@ -41,14 +49,20 @@ describe("createDiscordRequestClient", () => {
|
41 | 49 | |
42 | 50 | it("lets the REST client abort hanging proxied requests after its timeout", async () => { |
43 | 51 | const { fetch: fetchSpy } = createAbortableFetchMock(); |
| 52 | +vi.useFakeTimers(); |
44 | 53 | |
45 | 54 | const client = createDiscordRequestClient("Bot test-token", { |
46 | 55 | fetch: fetchSpy as never, |
47 | 56 | queueRequests: false, |
48 | 57 | timeout: 20, |
49 | 58 | }); |
50 | 59 | |
51 | | -await expectAbortError(client.get("/channels/123/messages")); |
| 60 | +const request = client.get("/channels/123/messages"); |
| 61 | +const abortExpectation = expectAbortError(request); |
| 62 | +await Promise.resolve(); |
| 63 | +expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 64 | +await vi.advanceTimersByTimeAsync(20); |
| 65 | +await abortExpectation; |
52 | 66 | }, 1_000); |
53 | 67 | |
54 | 68 | it("lets abortAllRequests cancel active proxied fetches", async () => { |
|