mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:48:26 +00:00
test: dedupe fetch cleanup-throw signal harness
This commit is contained in:
@@ -31,6 +31,18 @@ function createForeignSignalHarness() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createThrowingCleanupSignalHarness(cleanupError: Error) {
|
||||||
|
const removeEventListener = vi.fn(() => {
|
||||||
|
throw cleanupError;
|
||||||
|
});
|
||||||
|
const fakeSignal = {
|
||||||
|
aborted: false,
|
||||||
|
addEventListener: (_event: string, _handler: () => void) => {},
|
||||||
|
removeEventListener,
|
||||||
|
} as unknown as AbortSignal;
|
||||||
|
return { fakeSignal, removeEventListener };
|
||||||
|
}
|
||||||
|
|
||||||
describe("wrapFetchWithAbortSignal", () => {
|
describe("wrapFetchWithAbortSignal", () => {
|
||||||
it("adds duplex for requests with a body", async () => {
|
it("adds duplex for requests with a body", async () => {
|
||||||
let seenInit: RequestInit | undefined;
|
let seenInit: RequestInit | undefined;
|
||||||
@@ -122,15 +134,7 @@ describe("wrapFetchWithAbortSignal", () => {
|
|||||||
);
|
);
|
||||||
const wrapped = wrapFetchWithAbortSignal(fetchImpl);
|
const wrapped = wrapFetchWithAbortSignal(fetchImpl);
|
||||||
|
|
||||||
const removeEventListener = vi.fn(() => {
|
const { fakeSignal, removeEventListener } = createThrowingCleanupSignalHarness(cleanupError);
|
||||||
throw cleanupError;
|
|
||||||
});
|
|
||||||
|
|
||||||
const fakeSignal = {
|
|
||||||
aborted: false,
|
|
||||||
addEventListener: (_event: string, _handler: () => void) => {},
|
|
||||||
removeEventListener,
|
|
||||||
} as unknown as AbortSignal;
|
|
||||||
|
|
||||||
await expect(wrapped("https://example.com", { signal: fakeSignal })).rejects.toBe(fetchError);
|
await expect(wrapped("https://example.com", { signal: fakeSignal })).rejects.toBe(fetchError);
|
||||||
expect(removeEventListener).toHaveBeenCalledOnce();
|
expect(removeEventListener).toHaveBeenCalledOnce();
|
||||||
@@ -146,15 +150,7 @@ describe("wrapFetchWithAbortSignal", () => {
|
|||||||
);
|
);
|
||||||
const wrapped = wrapFetchWithAbortSignal(fetchImpl);
|
const wrapped = wrapFetchWithAbortSignal(fetchImpl);
|
||||||
|
|
||||||
const removeEventListener = vi.fn(() => {
|
const { fakeSignal, removeEventListener } = createThrowingCleanupSignalHarness(cleanupError);
|
||||||
throw cleanupError;
|
|
||||||
});
|
|
||||||
|
|
||||||
const fakeSignal = {
|
|
||||||
aborted: false,
|
|
||||||
addEventListener: (_event: string, _handler: () => void) => {},
|
|
||||||
removeEventListener,
|
|
||||||
} as unknown as AbortSignal;
|
|
||||||
|
|
||||||
expect(() => wrapped("https://example.com", { signal: fakeSignal })).toThrow(syncError);
|
expect(() => wrapped("https://example.com", { signal: fakeSignal })).toThrow(syncError);
|
||||||
expect(removeEventListener).toHaveBeenCalledOnce();
|
expect(removeEventListener).toHaveBeenCalledOnce();
|
||||||
|
|||||||
Reference in New Issue
Block a user