|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | 2 | import { computeBackoff, sleepWithAbort, type BackoffPolicy } from "./backoff.js"; |
3 | 3 | |
| 4 | +async function expectAbortedSleep(promise: Promise<void>): Promise<Error> { |
| 5 | +try { |
| 6 | +await promise; |
| 7 | +} catch (error) { |
| 8 | +expect(error).toBeInstanceOf(Error); |
| 9 | +return error as Error; |
| 10 | +} |
| 11 | +throw new Error("expected aborted sleep"); |
| 12 | +} |
| 13 | + |
4 | 14 | describe("backoff helpers", () => { |
5 | 15 | const policy: BackoffPolicy = { |
6 | 16 | initialMs: 100, |
@@ -38,10 +48,9 @@ describe("backoff helpers", () => {
|
38 | 48 | const controller = new AbortController(); |
39 | 49 | controller.abort(); |
40 | 50 | |
41 | | -await expect(sleepWithAbort(5, controller.signal)).rejects.toMatchObject({ |
42 | | -message: "aborted", |
43 | | -cause: expect.anything(), |
44 | | -}); |
| 51 | +const error = await expectAbortedSleep(sleepWithAbort(5, controller.signal)); |
| 52 | +expect(error.message).toBe("aborted"); |
| 53 | +expect(error.cause).toBe(controller.signal.reason); |
45 | 54 | }); |
46 | 55 | |
47 | 56 | it("advances with fake timers", async () => { |
@@ -74,8 +83,8 @@ describe("backoff helpers", () => {
|
74 | 83 | removeEventListener() {}, |
75 | 84 | } as unknown as AbortSignal; |
76 | 85 | |
77 | | -await expect(sleepWithAbort(50, signal)).rejects.toMatchObject({ |
78 | | - message: "aborted", |
79 | | -}); |
| 86 | +const error = await expectAbortedSleep(sleepWithAbort(50, signal)); |
| 87 | +expect(error.message).toBe("aborted"); |
| 88 | +expect(error.cause).toStrictEqual(new Error("listener-registration-race")); |
80 | 89 | }); |
81 | 90 | }); |