Skip to content

Commit bf30e3d

Browse files
committed
test(abort): Add AbortSignal.any basic tests
1 parent d4eaaf8 commit bf30e3d

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { describe, expect, test } from "bun:test";
2+
3+
/**
4+
* Comprehensive tests for AbortSignal.any()
5+
*
6+
* AbortSignal.any() creates a composite signal that aborts when any of the
7+
* provided signals abort. This test suite covers edge cases and ensures
8+
* standards-compliant behavior.
9+
*/
10+
describe("AbortSignal.any()", () => {
11+
describe("basic functionality", () => {
12+
test("should return a non-aborted signal for empty array", () => {
13+
// @ts-ignore - TypeScript may not have this typed
14+
const signal = AbortSignal.any([]);
15+
expect(signal).toBeInstanceOf(AbortSignal);
16+
expect(signal.aborted).toBe(false);
17+
});
18+
19+
test("should follow a single signal", () => {
20+
const controller = new AbortController();
21+
// @ts-ignore
22+
const composite = AbortSignal.any([controller.signal]);
23+
24+
expect(composite.aborted).toBe(false);
25+
controller.abort();
26+
expect(composite.aborted).toBe(true);
27+
});
28+
29+
test("should abort when any of multiple signals abort", () => {
30+
const controller1 = new AbortController();
31+
const controller2 = new AbortController();
32+
const controller3 = new AbortController();
33+
34+
// @ts-ignore
35+
const composite = AbortSignal.any([
36+
controller1.signal,
37+
controller2.signal,
38+
controller3.signal
39+
]);
40+
41+
expect(composite.aborted).toBe(false);
42+
controller2.abort("middle signal aborted");
43+
expect(composite.aborted).toBe(true);
44+
});
45+
});
46+
47+
describe("already-aborted signals", () => {
48+
test("should immediately abort if any input signal is already aborted", () => {
49+
const abortedController = new AbortController();
50+
abortedController.abort("pre-aborted");
51+
52+
const freshController = new AbortController();
53+
54+
// @ts-ignore
55+
const composite = AbortSignal.any([
56+
freshController.signal,
57+
abortedController.signal
58+
]);
59+
60+
expect(composite.aborted).toBe(true);
61+
});
62+
63+
test("should use AbortSignal.abort() result correctly", () => {
64+
const alreadyAborted = AbortSignal.abort("already aborted reason");
65+
const controller = new AbortController();
66+
67+
// @ts-ignore
68+
const composite = AbortSignal.any([controller.signal, alreadyAborted]);
69+
70+
expect(composite.aborted).toBe(true);
71+
expect(composite.reason).toBe("already aborted reason");
72+
});
73+
74+
test("should work with all signals already aborted", () => {
75+
const aborted1 = AbortSignal.abort("first");
76+
const aborted2 = AbortSignal.abort("second");
77+
78+
// @ts-ignore
79+
const composite = AbortSignal.any([aborted1, aborted2]);
80+
81+
expect(composite.aborted).toBe(true);
82+
// First aborted signal's reason should be used
83+
expect(composite.reason).toBe("first");
84+
});
85+
});
86+
87+
describe("reason propagation", () => {
88+
// Table-driven tests for different reason types per coderabbitai suggestion
89+
const reasonCases = [
90+
{ name: "Error", reason: new Error("custom abort reason") },
91+
{ name: "string", reason: "string reason" },
92+
{ name: "object", reason: { code: 42, message: "custom object" } },
93+
];
94+
95+
test.each(reasonCases)("should propagate $name reasons", ({ reason }) => {
96+
const controller = new AbortController();
97+
// @ts-ignore
98+
const composite = AbortSignal.any([controller.signal]);
99+
controller.abort(reason);
100+
expect(composite.reason).toBe(reason);
101+
});
102+
103+
test("should use DOMException for default abort reason", () => {
104+
const controller = new AbortController();
105+
// @ts-ignore
106+
const composite = AbortSignal.any([controller.signal]);
107+
108+
controller.abort();
109+
110+
expect(composite.reason).toBeInstanceOf(DOMException);
111+
expect(composite.reason.name).toBe("AbortError");
112+
});
113+
});
114+
115+
});

0 commit comments

Comments
 (0)