rsc-test-helper is a utility designed to enable unit testing of React components that consume async/await React Server Components (RSCs) within client-side testing environments like `@testing-library/react` or `react-test-renderer`. As of version 0.1.4, this package provides a `patch` function that transforms an async React component tree into a synchronous one by awaiting promises, thus making them compatible with standard React test renderers which do not natively support async component types. This is particularly useful for projects utilizing Next.js App Directory beta features (introduced in Next.js 13 in October 2022 and stabilized in 13.4 in June 2023), where RSCs return promises, causing errors in testing setups. The package currently has an early-stage development status with an undefined release cadence, focusing on solving an immediate testing pain point before official React/Next.js testing support for RSCs. A key differentiator is its specific focus on resolving the "Objects are not valid as a React child (found: [object Promise])" error encountered when rendering async RSCs in tests, especially in environments like JSDOM.
npm install rsc-test-helperVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `rsc-test-helper`'s `patch` function to enable unit testing of a React component tree that includes an asynchronous React Server Component (RSC) within a standard testing environment like `@testing-library/react`. It shows how to resolve the common 'Objects are not valid as a React child' error by awaiting and transforming the component before rendering.
For testing fallback states, alternative strategies (e.g., mocking the async component to return a pending promise, or using a different testing approach that doesn't resolve promises) would be necessary, but are outside the scope of this helper.
Users should exercise caution and thoroughly test their applications when updating, and ideally, pin to exact versions or ranges only after careful consideration in production environments.
Components containing server-side logic might need to be mocked or abstracted for unit tests using this helper, or a Node.js-based test environment might be required for true integration testing of server-side code.
Apply the `patch` function from `rsc-test-helper` to your root component before rendering. For example: `const Component = await patch(<YourRootComponent />); render(<Component />);`. This transforms the async component tree into a synchronous one by resolving all promises.
No dependency data recorded yet.