Node.js Stream Mocking Utilities

raw JSON →
2.0.5 verified Thu Apr 23 auth: no javascript

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.

error TypeError: Class constructor ObjectReadableMock cannot be invoked without 'new'
cause Attempting to call `ObjectReadableMock` or other mock classes as a function instead of instantiating them with `new`.
fix
Always use the new keyword: const reader = new ObjectReadableMock(input);
error TypeError: stream_mock_1.ObjectWritableMock is not a constructor
cause This error typically occurs in CommonJS environments when attempting to `require` a named export directly from an ESM-first or TypeScript-compiled module without correct handling of `exports` object structure.
fix
For CommonJS, use 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>'
cause Attempting to access `flatData` on a `WritableMock` instance when using an older version of `stream-mock` (before v1.1.0) or when TypeScript's type declarations are outdated.
fix
Update 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.
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.
fix Ensure you are using standard named ESM imports (e.g., `import { FooMock } from 'stream-mock'`). For CommonJS, use `const { FooMock } = require('stream-mock');` but be aware of potential differences.
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.
fix Inspect `writer.data` for an array of arrays or chunks. If a single flat array is desired, use `writer.flatData`.
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.
fix Upgrade to a maintained Node.js LTS version (e.g., Node.js 16 or newer) for optimal compatibility and performance.
npm install stream-mock
yarn add stream-mock
pnpm add stream-mock

Demonstrates how to test a custom Transform stream using `ObjectReadableMock` for input and `ObjectWritableMock` to capture output, asserting the transformed data.

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();
    });
  });
});