Registry / testing / stream-mock

stream-mock

JSON →
library2.0.5jsnpmunverified

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.

npm install stream-mock
INSTALL
IMPORT
SIG · STREAM-MOCK
S
stream-mock
testingjavascriptv2.0.5
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ObjectReadableMock
✓ import { ObjectReadableMock } from 'stream-mock'
✗ const ObjectReadableMock = require('stream-mock').ObjectReadableMock
Named export, preferred ESM import. CommonJS `require` can lead to issues with type checking or module resolution since v2.0.2.
ObjectWritableMock
✓ import { ObjectWritableMock } from 'stream-mock'
✗ import ObjectWritableMock from 'stream-mock'
Main Writable mock class. It is a named export, not a default export.
DuplexMock
✓ import { DuplexMock } from 'stream-mock'
✗ import { MockDuplex } from 'stream-mock'
Introduced in v1.2.0 for mocking Duplex streams.

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(); }); }); });
Debug
Known issues
breakingThe 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.
affects: >=2.0.2
gotchaWhen 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`.
affects: >=1.1.0
gotchaWhile 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.
affects: <2.0.3 (potentially), 8.x
Errors
Common errors & fixes
TypeError: Class constructor ObjectReadableMock cannot be invoked without 'new'
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);`
TypeError: stream_mock_1.ObjectWritableMock is not a constructor
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`).
Property 'flatData' does not exist on type 'WritableMock<any>'
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.
Upgrade
Version history
2.0.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
12
Amazon
1
Resources
stream-mock — npm install stream-mock · libregistry