{"id":14064,"library":"streamtest","title":"Streamtest","description":"Streamtest is a JavaScript library providing a robust set of utilities specifically designed for testing Node.js stream-based modules. It simplifies the creation of test input streams and the collection of output from streams under test. Developers can easily generate readable streams from arrays of data chunks or objects using methods like `fromChunks` and `fromObjects`, and then collect results from writable streams into promises that resolve with processed text, raw chunks, or objects via `toText`, `toChunks`, and `toObjects`. The current stable version is 4.0.0, and it primarily targets modern Node.js environments (v24.14.0+). Its key differentiator is providing a concise and intuitive API for stream testing, significantly reducing boilerplate compared to manually managing stream events and states, making it an ideal choice for projects with extensive stream processing logic.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/nfroidure/streamtest","tags":["javascript","test","streams","stream1","stream2","typescript"],"install":[{"cmd":"npm install streamtest","lang":"bash","label":"npm"},{"cmd":"yarn add streamtest","lang":"bash","label":"yarn"},{"cmd":"pnpm add streamtest","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is ESM-only since v4. The default export is an object containing all utility methods.","wrong":"const StreamTest = require('streamtest');","symbol":"StreamTest","correct":"import StreamTest from 'streamtest';"},{"note":"Methods like `fromChunks` are properties of the default `StreamTest` object, not named exports.","wrong":"import { fromChunks } from 'streamtest';","symbol":"fromChunks","correct":"StreamTest.fromChunks(['hello']);"},{"note":"`toText` returns a tuple of a writable stream and a promise, it is not a constructor.","wrong":"new StreamTest.toText();","symbol":"toText","correct":"const [outputStream, resultPromise] = StreamTest.toText();"}],"quickstart":{"code":"import StreamTest from 'streamtest';\nimport { Transform } from 'stream';\nimport assert from 'assert';\n\ndescribe('My Stream Lib', () => {\n  it('should capture text output from a stream', async () => {\n    // A simple passthrough stream, representing your stream-under-test\n    const myTestedStream = new Transform({\n      transform(chunk, encoding, callback) {\n        this.push(chunk.toString().toUpperCase()); // Example transformation\n        callback();\n      }\n    });\n\n    const [outputStream, resultPromise] = StreamTest.toText();\n\n    // Create a readable stream from an array of chunks,\n    // pipe it through your tested stream, then into streamtest's collecting stream.\n    StreamTest.fromChunks(['hello ', 'world', '!'])\n      .pipe(myTestedStream)\n      .pipe(outputStream);\n\n    // Await the collected text and assert against the expected output.\n    assert.equal(await resultPromise, 'HELLO WORLD!');\n  });\n\n  it('should create a stream from objects and collect processed objects', async () => {\n    const [objectOutputStream, objectResultPromise] = StreamTest.toObjects();\n\n    const myObjectStream = new Transform({\n      objectMode: true,\n      transform(obj, encoding, callback) {\n        this.push({ ...obj, processed: true, timestamp: Date.now() });\n        callback();\n      }\n    });\n\n    StreamTest.fromObjects([{ id: 1, value: 'A' }, { id: 2, value: 'B' }])\n      .pipe(myObjectStream)\n      .pipe(objectOutputStream);\n\n    const expected = [\n      { id: 1, value: 'A', processed: true, timestamp: (await objectResultPromise)[0].timestamp },\n      { id: 2, value: 'B', processed: true, timestamp: (await objectResultPromise)[1].timestamp }\n    ];\n\n    assert.deepStrictEqual(await objectResultPromise, expected);\n  });\n});","lang":"typescript","description":"Demonstrates creating input streams with `fromChunks` and `fromObjects`, piping data through a test stream, and capturing the processed output with `toText` and `toObjects` for assertion."},"warnings":[{"fix":"Upgrade your Node.js environment to version 24.14.0 or newer. Consider using `nvm` or `volta` to manage Node.js versions.","message":"Version 4.0.0 and above of streamtest explicitly requires Node.js version 24.14.0 or higher due to its `engines` field in `package.json`. Running it on older Node.js versions will result in an error or unexpected behavior.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`) and use `import StreamTest from 'streamtest';` for all imports.","message":"Streamtest v4.0.0 is an ECMAScript Module (ESM) only package. Attempting to use `require()` for CommonJS imports will fail with `SyntaxError: Cannot use import statement outside a module` or similar errors.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Refactor asynchronous test blocks to be `async` functions and `await` the `resultPromise` returned by `toText()`, `toChunks()`, or `toObjects()`.","message":"The `done` callback pattern used in some older test frameworks for asynchronous operations is often discouraged in favor of `async/await` with modern test runners. While `streamtest` supports both, using `async/await` with its promise-based collectors (`resultPromise`) provides cleaner and more readable test code.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'4.0.0':86 'api':104 'array':46 'base':20 'boilerpl':110 'choic':123 'chunk':49,73 'collect':32,60 'compar':111 'concis':101 'creation':25 'current':82 'data':48 'design':14 'develop':39 'differenti':97 'easili':41 'environ':93 'event':116 'extens':127 'fromchunk':55 'fromobject':57 'generat':42 'ideal':122 'input':28 'intuit':103 'javascript':5,131 'key':96 'librari':6 'like':54 'logic':130 'make':119 'manag':114 'manual':113 'method':53 'modern':91 'modul':21 'node.js':17,92 'object':51,75 'output':34 'primarili':89 'process':70,129 'project':125 'promis':66 'provid':7,99 'raw':72 'readabl':43 'reduc':109 'resolv':68 'result':61 'robust':9 'set':10 'signific':108 'simplifi':23 'specif':13 'stabl':83 'state':118 'stream':19,29,36,44,64,106,115,128,133 'stream-bas':18 'stream1':134 'stream2':135 'streamtest':1,2 'target':90 'test':16,27,38,107,132 'text':71 'tochunk':78 'toobject':80 'totext':77 'typescript':136 'use':52 'util':12 'v24.14.0':94 'version':84 'via':76 'writabl':63","created_at":"2026-04-20T01:57:44.230304+00:00","updated_at":"2026-04-20T01:57:44.230304+00:00","problems":[{"fix":"Configure your project to use ESM by adding `\"type\": \"module\"` to your `package.json` or by using `.mjs` file extensions for modules that use `import`.","cause":"Attempting to use `import StreamTest from 'streamtest';` in a CommonJS module context (e.g., a `.js` file without `\"type\": \"module\"` in `package.json`, or a `.cjs` file).","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Access `streamtest` utilities directly as properties of the imported `StreamTest` object, e.g., `StreamTest.fromChunks()` or `StreamTest.toText()`.","cause":"Trying to instantiate `StreamTest` with `new StreamTest()` or similar, when it's an object containing static utility methods, not a class.","error":"TypeError: StreamTest is not a constructor"},{"fix":"Verify that your environment is configured for ESM and you are using `import StreamTest from 'streamtest';`. Also, check your Node.js version meets the `>=24.14.0` requirement.","cause":"This error often occurs when `StreamTest` is `undefined` because `require('streamtest')` was used in an ESM-only package, or the `import` failed silently.","error":"TypeError: Cannot read properties of undefined (reading 'fromChunks')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0b1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/nfroidure/streamtest","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/streamtest","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}