Skip to content

Commit 90ac00d

Browse files
committed
test: added tests for duplex stream functionality
1 parent fd412e3 commit 90ac00d

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

test/parallel/test-stream-duplex.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
const common = require('../common');
2424
const assert = require('assert');
2525
const Duplex = require('stream').Duplex;
26+
const { Readable, Writable } = require('stream');
2627
const { ReadableStream, WritableStream } = require('stream/web');
2728

2829
const stream = new Duplex({ objectMode: true });
@@ -131,3 +132,47 @@ process.on('exit', () => {
131132
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
132133
}));
133134
}
135+
136+
const assertMessages = {
137+
isDuplex: 'Expected a Duplex stream',
138+
duplexUnchanged: 'Expected the Duplex stream to be returned unchanged',
139+
notDuplex: 'Expected a non-Duplex stream',
140+
isReadable: 'Expected a Readable stream'
141+
};
142+
143+
// readable.compose - Duplex stream unchanged
144+
{
145+
const readable = new Readable({
146+
read() {}
147+
});
148+
149+
const duplex = new Duplex({
150+
read() {},
151+
write(chunk, encoding, callback) {
152+
callback();
153+
}
154+
});
155+
156+
const composedStream = readable.compose(duplex);
157+
158+
assert(composedStream instanceof Duplex, assertMessages.isDuplex);
159+
assert.strictEqual(composedStream, duplex, assertMessages.duplexUnchanged);
160+
}
161+
162+
// readable.compose - wraps non-Duplex streams
163+
{
164+
const readable = new Readable({
165+
read() {}
166+
});
167+
168+
const writable = new Writable({
169+
write(chunk, encoding, callback) {
170+
callback();
171+
}
172+
});
173+
174+
const composedStream = readable.compose(writable);
175+
176+
assert(!(composedStream instanceof Duplex), assertMessages.notDuplex);
177+
assert(composedStream instanceof Readable, assertMessages.isReadable);
178+
}

0 commit comments

Comments
 (0)