-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformMapSync.test.ts
96 lines (80 loc) · 2.06 KB
/
transformMapSync.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Readable } from 'node:stream'
import { AppError, ErrorMode, _range, pTry } from '@naturalcycles/js-lib'
import { writableVoid, _pipeline, TransformMapStats } from '../..'
import { transformMapSync } from './transformMapSync'
interface Item {
id: string
}
test('transformMapSync simple', async () => {
const data: Item[] = _range(1, 4).map(n => ({ id: String(n) }))
const readable = Readable.from(data)
const data2: Item[] = []
await _pipeline([readable, transformMapSync<Item, void>(r => void data2.push(r)), writableVoid()])
expect(data2).toEqual(data)
// expect(readable.destroyed).toBe(true)
})
test('transformMapSync error', async () => {
let stats: TransformMapStats
const data = _range(100).map(String)
const data2: string[] = []
const [err] = await pTry(
_pipeline([
Readable.from(data),
transformMapSync<string, void>(
(r, i) => {
if (i === 50) {
throw new AppError('error on 50th')
}
data2.push(r)
},
{
onDone: s => (stats = s),
},
),
writableVoid(),
]),
)
expect(err).toBeInstanceOf(AppError)
expect(err).toMatchInlineSnapshot(`[AppError: error on 50th]`)
expect(data2).toEqual(data.slice(0, 50))
expect(stats!).toMatchInlineSnapshot(`
{
"collectedErrors": [],
"countErrors": 1,
"countIn": 51,
"countOut": 50,
"ok": false,
}
`)
})
test('transformMapSync suppressed error', async () => {
let stats: TransformMapStats
const data = _range(100).map(String)
const data2: string[] = []
await _pipeline([
Readable.from(data),
transformMapSync<string, void>(
(r, i) => {
if (i === 50) {
throw new AppError('error on 50th')
}
data2.push(r)
},
{
errorMode: ErrorMode.SUPPRESS,
onDone: s => (stats = s),
},
),
writableVoid(),
])
expect(data2).toEqual(data.filter(r => r !== '50'))
expect(stats!).toMatchInlineSnapshot(`
{
"collectedErrors": [],
"countErrors": 1,
"countIn": 100,
"countOut": 99,
"ok": true,
}
`)
})