-
Notifications
You must be signed in to change notification settings - Fork 0
/
promiseSettledAggregate.test.ts
60 lines (55 loc) · 1.86 KB
/
promiseSettledAggregate.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import promiseSettledAggregate from "./promiseSettledAggregate";
it("rejects immediately on invalid arg", async () => {
// @ts-expect-error (omitting required param for test purposes)
await expect(() => promiseSettledAggregate()).rejects.toThrow();
});
it("returns a promise", async () => {
const p = promiseSettledAggregate([]);
expect(p).toBeInstanceOf(Promise);
await p;
});
it("returns resolved values if all promises resolve", async () => {
const result: [number, boolean] = await promiseSettledAggregate([
Promise.resolve(5),
Promise.resolve(true),
]);
expect(result).toEqual([5, true]);
});
it("rejects with AggregateError if one of the promises rejected", async () => {
const err = new Error("Boom!");
let aggregateErr: AggregateError | undefined;
await expect(async () => {
try {
const result: [number, never] = await promiseSettledAggregate([
Promise.resolve(5),
Promise.reject(err),
]);
return result;
} catch (e) {
aggregateErr = e as AggregateError;
throw e;
}
}).rejects.toThrow("Some promises were rejected");
expect(aggregateErr?.name).toBe("AggregateError");
expect(aggregateErr?.errors).toEqual([err]);
});
it("rejects with AggregateError if multiple of the promises rejected", async () => {
const err = new Error("Boom!");
const err2 = new Error("Woopsie!");
let aggregateErr: AggregateError | undefined;
await expect(async () => {
try {
const result: [number, never, never] = await promiseSettledAggregate([
Promise.resolve(5),
Promise.reject(err),
Promise.reject(err2),
]);
return result;
} catch (e) {
aggregateErr = e as AggregateError;
throw e;
}
}).rejects.toThrow("Some promises were rejected");
expect(aggregateErr?.name).toBe("AggregateError");
expect(aggregateErr?.errors).toEqual([err, err2]);
});