{"id":18013441,"url":"https://github.com/milly/ts-streamtest","last_synced_at":"2025-10-09T18:36:45.293Z","repository":{"id":186455280,"uuid":"674962362","full_name":"Milly/ts-streamtest","owner":"Milly","description":"Library for testing streams like RxJS","archived":false,"fork":false,"pushed_at":"2024-09-02T08:38:56.000Z","size":209,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T19:45:36.464Z","etag":null,"topics":["rxjs","testing","typescript","webstream"],"latest_commit_sha":null,"homepage":"https://jsr.io/@milly/streamtest","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Milly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-05T10:08:49.000Z","updated_at":"2024-09-15T17:20:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"b448e32b-eb51-451e-b7ec-9b911b00516a","html_url":"https://github.com/Milly/ts-streamtest","commit_stats":null,"previous_names":["milly/deno-streamtest","milly/ts-streamtest"],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Milly%2Fts-streamtest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Milly%2Fts-streamtest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Milly%2Fts-streamtest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Milly%2Fts-streamtest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Milly","download_url":"https://codeload.github.com/Milly/ts-streamtest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247191568,"owners_count":20898954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rxjs","testing","typescript","webstream"],"created_at":"2024-10-30T03:21:33.339Z","updated_at":"2025-10-09T18:36:40.274Z","avatar_url":"https://github.com/Milly.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# streamtest\n\n[![license:MIT](https://img.shields.io/github/license/Milly/ts-streamtest)](LICENSE)\n[![jsr](https://jsr.io/badges/@milly/streamtest)](https://jsr.io/@milly/streamtest)\n[![Test](https://github.com/Milly/ts-streamtest/actions/workflows/test.yml/badge.svg)](https://github.com/Milly/ts-streamtest/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/Milly/ts-streamtest/graph/badge.svg?token=81L4DWDPIJ)](https://codecov.io/gh/Milly/ts-streamtest)\n\n**streamtest** is a library for testing streams. Provides helper functions that\nmake it easier to test streams with various scenarios and assertions.\n\nInspired by the test helpers in [RxJS](https://rxjs.dev/).\n\n## Define stream scenarios\n\nA simple string `series` can be used to define values to be enqueued into a\nstream and events such as closes and errors.\n\n### Series for ReadableStream\n\nThis can be used with the `readable` or `assertReadable` helpers.\n\n#### Series format\n\nThe following characters are available in the `series`:\n\n- `\\x20` : Space is ignored. Used to align columns.\n- `-` : Advance 1 tick.\n- `|` : Close the stream.\n- `!` : Cancel the stream.\n- `#` : Abort the stream.\n- `(...)` : Groups characters. It does not advance ticks inside. After closing\n  `)`, advance 1 tick.\n- Characters with keys in `values` will have their values enqueued to the\n  stream, and then advance 1 tick.\n- Other characters are enqueued into the stream as a single character, and then\n  advance 1 tick.\n\n#### Example\n\n- Series: `\"  ---A--B(CD)--|\"`\n- Values: `{ A: \"foo\" }`\n\n1. Waits 3 ticks.\n2. \"foo\" is enqueued and waits 1 tick.\n3. Waits 2 ticks.\n4. \"B\" is enqueued and waits 1 tick.\n5. \"C\" is enqueued, \"D\" is enqueued and waits 1 tick.\n6. Waits 2 ticks.\n7. Close the stream.\n\n### Series for WritableStream\n\nThis can be used with the `writable` helper.\n\n#### Series format\n\nThe following characters are available in the `series`:\n\n- `\\x20` : Space is ignored. Used to align columns.\n- `-` : Advance 1 tick.\n- `#` : Abort the stream.\n- `\u003c` : Apply backpressure. Then advance 1 tick.\n- `\u003e` : Release backpressure. Then advance 1 tick.\n\n#### Example\n\n- Series: `\"  --- \u003c-- \u003e-- #  \"`\n\n1. Waits 3 ticks.\n2. Apply backpressure. Flags the stream is not ready for writing.\n3. Waits 3 ticks.\n4. Release backpressure. Notify the data source that the stream is ready for\n   writing.\n5. Waits 3 ticks.\n6. Abort the stream.\n\n### Series for AbortSignal\n\nThis can be used with the `abort` helper.\n\n#### Series format\n\nThe following characters are available in the `series`:\n\n- `\\x20` : Space is ignored. Used to align columns.\n- `-` : Advance 1 tick.\n- `!` : Abort the signal.\n\n#### Example\n\n- Series: `\"  ----- !  \"`\n\n1. Waits 5 ticks.\n2. Aborts the signal.\n\n## API Reference\n\n### `testStream`\n\nDefine a block to test streams. `TestStreamHelper` is passed to the function\nspecified for `testStream`, which has helper functions available only within\nthat function.\n\n```typescript\nimport { testStream, type TestStreamHelper } from \"@milly/streamtest\";\n\nDeno.test(\"use testStream\", async () =\u003e {\n  await testStream(async (helper: TestStreamHelper) =\u003e {\n    // ... test logic using helper.assertReadable, helper.readable, and helper.run ...\n  });\n});\n```\n\n### `readable` helper\n\nCreates a `ReadableStream` with the specified `series`.\n\n```typescript\nimport { testStream } from \"@milly/streamtest\";\n\nDeno.test(\"use readable helper\", async () =\u003e {\n  await testStream(async ({ readable }) =\u003e {\n    const abortReason = new Error(\"abort\");\n    const values = {\n      A: \"foo\",\n      B: \"bar\",\n      C: \"baz\",\n    } as const;\n\n    // \"a\" ..sleep.. \"b\" ..sleep.. \"c\" ..sleep.. close\n    const characterStream = readable(\"a--b--c--|\");\n\n    // ..sleep.. \"foo\" ..sleep.. \"bar\" ..sleep.. \"baz\" and close\n    const stringStream = readable(\"   --A--B--(C|)\", values);\n\n    // \"0\" ..sleep.. \"1\" ..sleep.. \"2\" ..sleep.. abort\n    const errorStream = readable(\"    012#\", undefined, abortReason);\n\n    // Now you can use the `*Stream` in your test logic.\n  });\n});\n```\n\n### `writable` helper\n\nCreates a `WritableStream` with the specified `series`.\n\n```typescript\nimport { testStream } from \"@milly/streamtest\";\n\nDeno.test(\"use writable helper\", async () =\u003e {\n  await testStream(async ({ writable, readable, run, assertReadable }) =\u003e {\n    const abortReason = new Error(\"abort\");\n\n    const dest = writable(\"  -----\u003c------------- \u003e  --#\", abortReason);\n    //     Backpressure range ____^^^^^^^^^^^^^^      ^\n    //                    Aborts the dest stream ____/\n\n    const source = readable(\"---a---b---c---d--- -  -----|\");\n    const expected = \"       ---a---b-----------(cd)--!\";\n    //       Apply backpressure ____^            ^^\n    // Release backpressure and emits \"c\", \"d\" _/\n\n    await run([source], async (source) =\u003e {\n      await source.pipeTo(dest).catch(() =\u003e {});\n    });\n\n    await assertReadable(source, expected, {}, abortReason);\n  });\n});\n```\n\n### `assertReadable` helper\n\nAsserts that the readable stream matches the specified `series`.\n\n```typescript\nimport { testStream } from \"@milly/streamtest\";\nimport { UpperCase } from \"@milly/streamtest/examples/upper-case\";\n\nDeno.test(\"use assertReadable helper\", async () =\u003e {\n  await testStream(async ({ assertReadable, readable }) =\u003e {\n    const abortReason = new Error(\"abort\");\n    const values = {\n      A: \"foo\",\n      B: \"bar\",\n      C: \"baz\",\n    } as const;\n\n    const source = readable(\"--A--B--C--#\", values, abortReason);\n    const expectedSeries = \" --A--B--C--#\";\n    const expectedValues = {\n      A: \"FOO\",\n      B: \"BAR\",\n      C: \"BAZ\",\n    };\n\n    const actual = source.pipeThrough(new UpperCase());\n\n    await assertReadable(actual, expectedSeries, expectedValues, abortReason);\n  });\n});\n```\n\n### `abort` helper\n\nCreates a `AbortSignal` with the specified `series`.\n\n```typescript\nimport { assertEquals } from \"@std/assert/equals\";\nimport { delay } from \"@std/async/delay\";\nimport { testStream } from \"@milly/streamtest\";\n\nDeno.test(\"use abort helper\", async () =\u003e {\n  await testStream(async ({ abort, run }) =\u003e {\n    const abortReason = new Error(\"abort\");\n\n    // ..sleep 3 ticks.. abort with `abortReason`\n    const signal = abort(\"---!\", abortReason);\n\n    await run([], async () =\u003e {\n      await delay(300 - 1);\n      assertEquals(signal.aborted, false);\n\n      await delay(2);\n      assertEquals(signal.aborted, true);\n      assertEquals(signal.reason, abortReason);\n    });\n  });\n});\n```\n\n### `run` helper\n\nProcess the test streams inside the `run` block.\n\n```typescript\nimport { assertEquals } from \"@std/assert/equals\";\nimport { testStream } from \"@milly/streamtest\";\nimport { UpperCase } from \"@milly/streamtest/examples/upper-case\";\n\nDeno.test(\"use run helper\", async () =\u003e {\n  await testStream(async ({ run, readable }) =\u003e {\n    const source = readable(\"--a--b--c--|\");\n\n    const actual = source.pipeThrough(new UpperCase());\n\n    await run([actual], async (actual) =\u003e {\n      const reader = actual.getReader();\n\n      assertEquals(await reader.read(), { value: \"A\", done: false });\n      assertEquals(await reader.read(), { value: \"B\", done: false });\n      assertEquals(await reader.read(), { value: \"C\", done: false });\n      assertEquals(await reader.read(), { value: undefined, done: true });\n\n      reader.releaseLock();\n    });\n  });\n});\n```\n\n## License\n\nThis library is licensed under the MIT License. See the [LICENSE](./LICENSE)\nfile for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilly%2Fts-streamtest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmilly%2Fts-streamtest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilly%2Fts-streamtest/lists"}