feat: add withStreamingNetwork for HTTP streaming test support#371
Open
PabloFuentesSanz wants to merge 9 commits into
Open
feat: add withStreamingNetwork for HTTP streaming test support#371PabloFuentesSanz wants to merge 9 commits into
PabloFuentesSanz wants to merge 9 commits into
Conversation
Adds a first-class withStreamingNetwork method to the wrap() API, enabling realistic testing of HTTP streaming (SSE, NDJSON, chunked responses) without resorting to spies or manual fetch mocks. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The Extension type change was introduced as a side effect when withStreamingNetwork was briefly implemented as a user extension. Since it is now a built-in method, there is no reason to alter the existing extension type contract. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
alfupe
approved these changes
Jun 18, 2026
alfupe
reviewed
Jun 18, 2026
Using the inherited DOM body field as an implicit signal for streaming was confusing. streamBody makes the intent unambiguous both at the WrapResponse definition and at the createResponse dispatch point. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
arturosdg
approved these changes
Jun 18, 2026
frexposi
approved these changes
Jun 18, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
arturosdg
approved these changes
Jun 18, 2026
frexposi
approved these changes
Jun 18, 2026
Contributor
Author
|
PR probando la |
desclapez
reviewed
Jun 22, 2026
deftone42
approved these changes
Jun 29, 2026
frexposi
approved these changes
Jun 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
As HTTP streaming becomes increasingly common — particularly in chat and LLM-powered applications that use SSE or chunked transfer encoding — testing streaming responses with wrapito previously required workarounds that diverged significantly from production behavior.
The typical approach was to use a
vi.spyOn(global, 'fetch')and return a Promise that never resolves (to simulate a pending stream) or to mockresponse.json()on a fake response object. This approach has two problems:response.body.getReader()and loop over chunks — notresponse.json(). A spy-based mock cannot exercise this code path at all.fetchat the spy level means your test needs to know how the component callsfetchinternally, rather than testing the observable behavior (text appearing chunk by chunk, a loading indicator while the stream is open, etc.).withStreamingNetworksolves both problems by providing aReadableStream-backed response through wrapito's existing mock infrastructure, so tests can verify real streaming behavior without changing howfetchis intercepted.What's new:
withStreamingNetworkA new built-in method on the
wrap()chain — no registration required, works exactly likewithNetwork.Parameters
pathstringhoststringwithNetworkmethodHttpMethod'GET'chunksStreamChunk[]{ text, delay? }objectsdelayBetweenChunksnumber0keepOpenbooleanfalsetrue, the stream never closes — useful for testing in-progress statesStreamChunkis either a plainstring(no delay) or{ text: string; delay?: number }(per-chunk delay that overridesdelayBetweenChunks).Usage in a real project
Register nothing — just use it:
Test cases you can now cover
withNetworkcalls for endpoints that mix streaming and JSON responsesImplementation details
Core change:
createResponsenow supportsbody: ReadableStreamWrapResponsealready extendedPartial<Response>, which includesbody?: ReadableStream<Uint8Array> | nullfrom the DOM type. The only change tomockNetwork.tsis thatcreateResponsenow checks for abodyproperty and, when present, returns a response-like object with.bodyset to the stream andContent-Type: text/event-stream— instead of the default JSON shape with.json().No breaking changes
withNetwork,withInteraction,atPath, and all other built-in methods are unaffected.configure({ extend: ... })) is unchanged.response.json()continue to work exactly as before.Also fixed
Extensiontype was previously<T>(api, args: T) => Wrap— a generic that made it impossible to write typed user-defined extensions without TypeScript errors. Changed to(api, args: any) => unknown, which is what the runtime already assumed.tsconfig.test.jsonnow includes@testing-library/jest-domintypes, sotoBeInTheDocumentand other jest-dom matchers are correctly typed in test files.Tests
New test file:
tests/lib/streamingNetwork.test.tscovering:keepOpen: true— streaming indicator stays visible after chunks are receiveddelay— chunks arrive in order after their individual delaysdelayBetweenChunks— uniform delay between all chunks🤖 Generated with Claude Code
Update: multiple sequential responses & controllable streams
Two follow-up additions, driven by migrating a real chat test suite (sequential assistant messages + step-by-step loading/progress assertions without timers).
Array form — sequential responses to the same endpoint
withStreamingNetworknow also accepts an array (likewithNetwork). Each entry is a separate response served in order to successive requests to the same endpoint (consume-once):Note the two axes:
chunksare progressive pieces of one response (concatenated into a single message), while the array registers separate responses for separate requests. They compose:[{ chunks: ['Hel', 'lo'] }, { chunks: ['Bye'] }].createStreamController— drive a stream from the testFor step-by-step assertions (loading → partial → closed) without relying on timers, pass a controllable
streaminstead ofchunks:StreamingNetworkConfignow acceptsstream?: ReadableStreamas an alternative tochunks.createStreamController()returns{ stream, sendChunk, close }.sendChunk/closeare synchronous; in React tests flush withawait waitFor(...)/findBy*after each emission.Added tests
createStreamController)