There is an easier way: stream.PassThrough
I just found that Node is very easy to skip the stream.PassThrough class, which I believe is what you are looking for.
From the node documents:
The stream.PassThrough class is a trivial implementation of a Transform stream that simply passes input bytes to the output. Its purpose is mainly for examples and testing ...
link to documents
The code from the question, modified:
const { PassThrough } = require('stream'); const mockedStream = new PassThrough(); // <---- mockedStream.on('data', (d) => { console.dir(d); }); mockedStream.on('end', function() { console.dir('goodbye'); }); mockedStream.emit('data', 'hello world'); mockedStream.end(); // <-- end. not close. mockedStream.destroy();
mockedStream.push() also works, but like Buffer , so you might want to do: console.dir(d.toString());
Taitu-lism
source share