-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpromises.unit.test.ts
More file actions
52 lines (45 loc) · 1.57 KB
/
promises.unit.test.ts
File metadata and controls
52 lines (45 loc) · 1.57 KB
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
import { describe } from 'vitest';
import { asyncSequential, groupByStatus } from './promises.js';
describe('groupByStatus', () => {
it('should group results by status', () => {
const results = [
{ status: 'fulfilled', value: 'first' },
{ status: 'rejected', reason: 'second' },
{ status: 'fulfilled', value: 'third' },
] as PromiseSettledResult<string>[];
const grouped = groupByStatus(results);
expect(grouped).toEqual({
fulfilled: [
{ status: 'fulfilled', value: 'first' },
{ status: 'fulfilled', value: 'third' },
],
rejected: [{ status: 'rejected', reason: 'second' }],
});
});
});
describe('asyncSequential', () => {
it('should map async function to array', async () => {
await expect(
asyncSequential(['a', 'b', 'c'], x => Promise.resolve(x.toUpperCase())),
).resolves.toEqual(['A', 'B', 'C']);
});
it('should wait for previous item to resolve before processing next item', async () => {
let counter = 0;
const work = vi.fn().mockImplementation(
() =>
new Promise(resolve => {
counter++;
setTimeout(() => {
resolve(counter);
}, 10);
}),
);
const items = Array.from({ length: 4 });
await expect(asyncSequential(items, work)).resolves.toEqual([1, 2, 3, 4]);
counter = 0;
const sequentialResult = await asyncSequential(items, work); // [1, 2, 3, 4]
counter = 0;
const parallelResult = await Promise.all(items.map(work)); // [4, 4, 4, 4]
expect(sequentialResult).not.toEqual(parallelResult);
});
});