Skip to content

Commit 4851448

Browse files
committed
test: add stream map tests
1 parent 5badf46 commit 4851448

File tree

1 file changed

+98
-8
lines changed

1 file changed

+98
-8
lines changed

test/parallel/test-stream-map.js

+98-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ const { setTimeout } = require('timers/promises');
1010
{
1111
// Map works on synchronous streams with a synchronous mapper
1212
const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => x + x);
13-
const result = [2, 4, 6, 8, 10];
1413
(async () => {
15-
for await (const item of stream) {
16-
assert.strictEqual(item, result.shift());
17-
}
14+
assert.deepStrictEqual(await stream.toArray(), [2, 4, 6, 8, 10]);
1815
})().then(common.mustCall());
1916
}
2017

@@ -24,7 +21,49 @@ const { setTimeout } = require('timers/promises');
2421
await Promise.resolve();
2522
return x + x;
2623
});
27-
const result = [2, 4, 6, 8, 10];
24+
(async () => {
25+
assert.deepStrictEqual(await stream.toArray(), [2, 4, 6, 8, 10]);
26+
})().then(common.mustCall());
27+
}
28+
29+
{
30+
// Map works on asynchronous streams with a asynchronous mapper
31+
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => {
32+
return x + x;
33+
}).map((x) => x + x);
34+
(async () => {
35+
assert.deepStrictEqual(await stream.toArray(), [4, 8, 12, 16, 20]);
36+
})().then(common.mustCall());
37+
}
38+
39+
{
40+
// Map works on an infinite stream
41+
const stream = Readable.from(async function* () {
42+
while (true) yield 1;
43+
}()).map(common.mustCall(async (x) => {
44+
return x + x;
45+
}, 5));
46+
(async () => {
47+
let i = 1;
48+
for await (const item of stream) {
49+
assert.strictEqual(item, 2);
50+
if (++i === 5) break;
51+
}
52+
})().then(common.mustCall());
53+
}
54+
55+
{
56+
// Map works on non-objectMode streams
57+
const stream = new Readable({
58+
read() {
59+
this.push(Uint8Array.from([1]));
60+
this.push(Uint8Array.from([2]));
61+
this.push(null);
62+
}
63+
}).map(async ([x]) => {
64+
return x + x;
65+
}).map((x) => x + x);
66+
const result = [4, 8];
2867
(async () => {
2968
for await (const item of stream) {
3069
assert.strictEqual(item, result.shift());
@@ -33,18 +72,69 @@ const { setTimeout } = require('timers/promises');
3372
}
3473

3574
{
36-
// Map works on asynchronous streams with a asynchronous mapper
37-
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => {
75+
// Does not care about data events
76+
const source = new Readable({
77+
read() {
78+
this.push(Uint8Array.from([1]));
79+
this.push(Uint8Array.from([2]));
80+
this.push(null);
81+
}
82+
});
83+
setImmediate(() => stream.emit('data', Uint8Array.from([1])));
84+
const stream = source.map(async ([x]) => {
3885
return x + x;
3986
}).map((x) => x + x);
40-
const result = [4, 8, 12, 16, 20];
87+
const result = [4, 8];
4188
(async () => {
4289
for await (const item of stream) {
4390
assert.strictEqual(item, result.shift());
4491
}
4592
})().then(common.mustCall());
4693
}
4794

95+
{
96+
// Emitting an error during `map`
97+
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => {
98+
if (x === 3) {
99+
stream.emit('error', new Error('boom'));
100+
}
101+
return x + x;
102+
});
103+
assert.rejects(
104+
stream.map((x) => x + x).toArray(),
105+
/boom/,
106+
).then(common.mustCall());
107+
}
108+
109+
{
110+
// Throwing an error during `map` (sync)
111+
const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => {
112+
if (x === 3) {
113+
throw new Error('boom');
114+
}
115+
return x + x;
116+
});
117+
assert.rejects(
118+
stream.map((x) => x + x).toArray(),
119+
/boom/,
120+
).then(common.mustCall());
121+
}
122+
123+
124+
{
125+
// Throwing an error during `map` (async)
126+
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => {
127+
if (x === 3) {
128+
throw new Error('boom');
129+
}
130+
return x + x;
131+
});
132+
assert.rejects(
133+
stream.map((x) => x + x).toArray(),
134+
/boom/,
135+
).then(common.mustCall());
136+
}
137+
48138
{
49139
// Concurrency + AbortSignal
50140
const ac = new AbortController();

0 commit comments

Comments
 (0)