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
muslnode 18–226 runs
build_error
glibcnode 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.fixEnsure 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.fixInspect `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.fixUpgrade 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`.
fixAlways 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.
fixFor 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.
fixUpdate `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.
Audit
Dependencies
No dependency data recorded yet.