/
pipeAsync.spec.js
57 lines (45 loc) · 994 Bytes
/
pipeAsync.spec.js
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
import { delay } from './delay.js'
import { pipeAsync } from './pipeAsync.js'
async function identity(x){
await delay(100)
return x
}
test('happy', async () => {
const fn1 = async x => {
await delay(100)
return x.map(xx => xx + 1)
}
const fn2 = async x => {
await delay(100)
return x.map(xx => xx * 2)
}
const result = await pipeAsync(fn1,
fn2)(await Promise.all([ identity(1), identity(2), identity(3) ]))
expect(result).toEqual([ 4, 6, 8 ])
})
const delayFn = ms =>
new Promise(resolve => {
resolve(ms + 1)
})
test.only('with function returning promise', async () => {
const result = await pipeAsync(
x => x,
x => x + 1,
delayFn,
x => x
)(1)
expect(result).toBe(3)
})
test('throw error', async () => {
const fn = async () => {
await delay(1)
JSON.parse('{foo')
}
let didThrow = false
try {
await pipeAsync(x => x, fn)(20)
} catch (e){
didThrow = true
}
expect(didThrow).toBeTrue()
})