In NextJs 13+ using the experimental App folder, async server components can be written, as described by the documentation:
export default async function Page({ params: { username } }) {
// Initiate both requests in parallel
const artistData = getArtist(username);
const albumsData = getArtistAlbums(username);
// Wait for the promises to resolve
const [artist, albums] = await Promise.all([artistData, albumsData]);
return (
<>
<h1>{artist.name}</h1>
<Albums list={albums}></Albums>
</>
);
}
This is a very useful technique that I have implemented in many pages of my app. However, when testing with jest, I find that I am unable to write any tests that are able to render this default export:
it('should render without crashing', async () => {
...(setup mocks)
const { container } = await waitFor(() => render(<Page params={{ username: 'Bob Dylan' }} />));
});
Any attempt to render the component or call it manually results in the following error:
Uncaught [Error: Objects are not valid as a React child (found: [object Promise])
Has anyone been able to correctly implement a Jest test using an async server component?
enzyme
or something else? – Clothorender
andwaitFor
are in your test code. If not, feel free to remove the tags I added. – Sarcastic