-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathall.test.ts
52 lines (45 loc) · 1.68 KB
/
all.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
import test from 'ava';
import * as promiseUtils from '../src/index';
test('throws errors in order with multiple errors', async (t) => {
const testPromises: Promise<any>[] = [
promiseUtils.invert(promiseUtils.delay(500, null), 'delayed error'),
promiseUtils.delay(500, { success: true }),
Promise.resolve(3),
Promise.reject(new Error('failed')),
Promise.resolve('success'),
Promise.reject('also failed'),
];
const err = await promiseUtils.invert(promiseUtils.all(testPromises));
t.is(err.message, 'delayed error... and 2 other errors');
t.deepEqual(err.otherErrors, [new Error('failed'), 'also failed']);
});
test('returns values in order of array not execution speed', async (t) => {
const testPromises: Promise<any>[] = [
promiseUtils.delay(500, { success: true }),
Promise.resolve(3),
Promise.resolve('success'),
];
const res = await promiseUtils.all(testPromises);
t.deepEqual(res, [{ success: true }, 3, 'success']);
});
test('throws single error', async (t) => {
const testPromises: Promise<any>[] = [
Promise.reject(new Error('failed')),
Promise.resolve('success'),
];
const err = await promiseUtils.invert(promiseUtils.all(testPromises));
t.deepEqual(err, new Error('failed'));
});
test('settles null value', async (t) => {
const res = await promiseUtils.all(null as any);
t.deepEqual(res, []);
});
test('works for results', async (t) => {
const testPromises: Promise<any>[] = [Promise.resolve('a'), Promise.resolve('b')];
const res = await promiseUtils.all(testPromises);
t.deepEqual(res, ['a', 'b']);
});
test('handles empty promise array', async (t) => {
const res = await promiseUtils.all([]);
t.deepEqual(res, []);
});