{"id":17960,"library":"stream-mock","title":"Node.js Stream Mocking Utilities","description":"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.","status":"active","version":"2.0.5","language":"javascript","source_language":"en","source_url":"https://github.com/BastienAr/stream-mock","tags":["javascript","stream","mock","test","writable","readable","typescript"],"install":[{"cmd":"npm install stream-mock","lang":"bash","label":"npm"},{"cmd":"yarn add stream-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add stream-mock","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Named export, preferred ESM import. CommonJS `require` can lead to issues with type checking or module resolution since v2.0.2.","wrong":"const ObjectReadableMock = require('stream-mock').ObjectReadableMock","symbol":"ObjectReadableMock","correct":"import { ObjectReadableMock } from 'stream-mock'"},{"note":"Main Writable mock class. It is a named export, not a default export.","wrong":"import ObjectWritableMock from 'stream-mock'","symbol":"ObjectWritableMock","correct":"import { ObjectWritableMock } from 'stream-mock'"},{"note":"Introduced in v1.2.0 for mocking Duplex streams.","wrong":"import { MockDuplex } from 'stream-mock'","symbol":"DuplexMock","correct":"import { DuplexMock } from 'stream-mock'"}],"quickstart":{"code":"import { Transform } from 'stream';\nimport { ObjectReadableMock, ObjectWritableMock } from 'stream-mock';\nimport chai from 'chai';\n\n// Imagine this is your custom Transform stream to test\nclass Rounder extends Transform {\n  _transform(chunk, encoding, callback) {\n    this.push(Math.round(chunk));\n    callback();\n  }\n}\n\nchai.should();\n\ndescribe('Test Rounder Transform Stream', () => {\n  it('should round numbers piped through it', (done) => {\n    // Given\n    const input = [1.2, 2.6, 3.7];\n    const transform = new Rounder({ objectMode: true });\n    const reader = new ObjectReadableMock(input);\n    const writer = new ObjectWritableMock();\n\n    // When\n    reader.pipe(transform).pipe(writer);\n\n    // Then\n    writer.on('finish', () => {\n      writer.data.should.deep.equal(input.map(Math.round));\n      done();\n    });\n  });\n});","lang":"javascript","description":"Demonstrates how to test a custom Transform stream using `ObjectReadableMock` for input and `ObjectWritableMock` to capture output, asserting the transformed data."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":">=2.0.2"},{"fix":"Inspect `writer.data` for an array of arrays or chunks. If a single flat array is desired, use `writer.flatData`.","message":"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.","severity":"gotcha","affected_versions":">=1.1.0"},{"fix":"Upgrade to a maintained Node.js LTS version (e.g., Node.js 16 or newer) for optimal compatibility and performance.","message":"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.","severity":"gotcha","affected_versions":"<2.0.3 (potentially), 8.x"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Always use the `new` keyword: `const reader = new ObjectReadableMock(input);`","cause":"Attempting to call `ObjectReadableMock` or other mock classes as a function instead of instantiating them with `new`.","error":"TypeError: Class constructor ObjectReadableMock cannot be invoked without 'new'"},{"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`).","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.","error":"TypeError: stream_mock_1.ObjectWritableMock is not a constructor"},{"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.","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.","error":"Property 'flatData' does not exist on type 'WritableMock<any>'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}