Node.js Stream Mocking Utilities
raw JSON →stream-mock is a Node.js library designed for creating mock Readable, Writable, and Duplex streams, primarily for testing purposes. The current stable version is 2.0.5, with a release cadence that shows active maintenance and feature additions, such as the introduction of Duplex stream support in v1.2.0 and a complete refactoring to TypeScript in v2.0.2. This refactoring enhances type safety and developer experience for TypeScript users. Key differentiators include its ability to create readable streams from any iterable, provide direct access to data written to a mock writable stream through properties like `data` or `flatData`, and support for both object and buffer modes. It simplifies unit testing of custom stream transformations by providing controllable input and verifiable output mechanisms.
Common errors
error TypeError: Class constructor ObjectReadableMock cannot be invoked without 'new' ↓
new keyword: const reader = new ObjectReadableMock(input); error TypeError: stream_mock_1.ObjectWritableMock is not a constructor ↓
const { ObjectWritableMock } = require('stream-mock');. If using TypeScript, ensure your tsconfig.json module option is appropriate for your target environment (e.g., ESNext with moduleResolution: NodeNext or CommonJS). error Property 'flatData' does not exist on type 'WritableMock<any>' ↓
stream-mock to at least v1.1.0 (npm install stream-mock@latest) or ensure your TypeScript configuration correctly picks up the latest type definitions. Warnings
breaking The package was refactored to TypeScript in v2.0.2. While efforts were made to maintain compatibility, users relying on specific CommonJS module structures or internal paths might experience breaking changes, especially when upgrading from v1.x. ↓
gotcha When using `WritableMock`, data is accessible via the `data` property, which returns an array of chunks. For cases where you expect a flat array of values (e.g., in `objectMode`), the `flatData` property (added in v1.1.0) might be more convenient and accurate. ↓
gotcha While the `engines` field in `package.json` suggests support for Node.js 8.x, the library actively targets and tests against newer Node.js versions (e.g., Node.js 12+ since v2.0.3). Using `stream-mock` with very old Node.js 8.x environments might lead to unforeseen compatibility issues, especially with modern JavaScript features or stream API changes. ↓
Install
npm install stream-mock yarn add stream-mock pnpm add stream-mock Imports
- ObjectReadableMock wrong
const ObjectReadableMock = require('stream-mock').ObjectReadableMockcorrectimport { ObjectReadableMock } from 'stream-mock' - ObjectWritableMock wrong
import ObjectWritableMock from 'stream-mock'correctimport { ObjectWritableMock } from 'stream-mock' - DuplexMock wrong
import { MockDuplex } from 'stream-mock'correctimport { DuplexMock } from 'stream-mock'
Quickstart
import { Transform } from 'stream';
import { ObjectReadableMock, ObjectWritableMock } from 'stream-mock';
import chai from 'chai';
// Imagine this is your custom Transform stream to test
class Rounder extends Transform {
_transform(chunk, encoding, callback) {
this.push(Math.round(chunk));
callback();
}
}
chai.should();
describe('Test Rounder Transform Stream', () => {
it('should round numbers piped through it', (done) => {
// Given
const input = [1.2, 2.6, 3.7];
const transform = new Rounder({ objectMode: true });
const reader = new ObjectReadableMock(input);
const writer = new ObjectWritableMock();
// When
reader.pipe(transform).pipe(writer);
// Then
writer.on('finish', () => {
writer.data.should.deep.equal(input.map(Math.round));
done();
});
});
});