feat: add renderAsync for testing async React Server Components#1444
Draft
aurorascharff wants to merge 4 commits intotesting-library:mainfrom
Draft
feat: add renderAsync for testing async React Server Components#1444aurorascharff wants to merge 4 commits intotesting-library:mainfrom
aurorascharff wants to merge 4 commits intotesting-library:mainfrom
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 707a6a9:
|
Made-with: Cursor
- Refactor resolveElementProps to use Promise.all instead of await-in-loop
(fixes no-await-in-loop lint error)
- Wrap resolved values in {value, changed} objects so Promise.all does
not inadvertently flatten Promise-valued props (e.g. dataPromise for use())
- Remove unreachable props == null guard (React elements always have props)
- Add test for non-element objects in children to cover resolveElement
safety branch (96.15% branch coverage, above 95% threshold)
Made-with: Cursor
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.
What:
Add
renderAsync— a first-class API for testing async React Server Components and components using React 19'suse()API.Why:
React 19's client-side renderer (
react-dom/client) does not supportasync functioncomponents — they only work with the server renderer. This has been a major pain point for the community (#1209), forcing projects to either pin to a specific RC version of React (19.0.0-rc-380f5d67-20241113), write custom render helpers, or abandon unit testing RSCs in favor of E2E tests.That RC version worked because its fiber reconciler had implicit
use()behavior for async components — when it encountered a Promise return value from a function component, it suspended at the nearest Suspense boundary. This was removed before the stable React 19 release.renderAsyncreplicates that behavior in userland.How:
Two-phase approach:
Pre-resolution (
resolveElement): Recursively walks the JSX element tree, callsasync functioncomponents withawait, and replaces them with their resolved output before React ever sees them. Walks all props — not justchildren— so async components passed as e.g.sidebar,header, orfallbackprops are also resolved. Handles nested async components, siblings, fragments, and any server/client component tree.Suspense +
act(): Wraps the resolved tree in a<Suspense>boundary and renders insideawait act(async () => ...), so components usinguse(promise)suspend and resolve correctly.The returned result matches
render()exceptrerenderis async (it also pre-resolves and flushes suspensions).Note: This PR was authored with AI assistance (Cursor + Claude). I'm not deeply experienced with RTL internals, so I'd especially appreciate feedback on whether the approach and conventions are right. Happy to iterate.
Checklist:
docs site
24 tests covering: simple/nested/deeply-nested async components, sibling async components, fragments, mixed sync+async trees, async components in non-children props,
use()with promises,use()with context, rerender, error propagation, wrapper/container options, and standard render result properties. All existing tests continue to pass. TypeScript compiles clean.A demo project using
renderAsyncwith a Next.js App Router app and Vitest is available at aurorascharff/testing-rscs.Resources: