how do you test async server components?
Unanswered
Pacific sand lance posted this in #help-forum
Pacific sand lanceOP
I'm using next 14 with the app router, JEST and React Testing Library, and I am unable to have any of my tests pass for my async client components. If I remove the async keyword, my tests pass just fine, but adding it gives me weird errors as described here:
https://github.com/vercel/next.js/issues/47131
https://github.com/testing-library/react-testing-library/issues/1209
I need async because I need to use await, and I need await for fetching data from my db
there has to be someone testing with Jest and RTL right? here is some sample code that shows the problem, do you have any advice for me?
https://github.com/vercel/next.js/issues/47131
https://github.com/testing-library/react-testing-library/issues/1209
I need async because I need to use await, and I need await for fetching data from my db
there has to be someone testing with Jest and RTL right? here is some sample code that shows the problem, do you have any advice for me?
//page.tsx
export default async function Page() {
return (
<main>
<h1>App router</h1>
</main>
);
}//page.test.tsx
/**
* u/jest-environment jsdom
*/
import { render, screen } from '@testing-library/react';
import Page from '@/app/page';
it('App Router: Works with Server Components', async () => {
await render(<Page />);
expect(screen.getByRole('heading')).toHaveTextContent('App Router');
});Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
6 |
7 | it('App Router: Works with Server Components', async () => {
> 8 | await render(<Page />);
| ^
9 | expect(screen.getByRole('heading')).toHaveTextContent('App Router');
10 | });
11 |6 Replies
Pacific sand lanceOP
bump
Pacific sand lanceOP
bump
Pacific sand lanceOP
bump
Giant Angora
@Pacific sand lance Did you find a solution to this, also running into this problem
EDIT: If anyone else runs into this issue I found a solution here https://nextjs-forum.com/post/1193729368753713192#message-1193748265003393083
EDIT: If anyone else runs into this issue I found a solution here https://nextjs-forum.com/post/1193729368753713192#message-1193748265003393083
@Giant Angora <@326800686845263872> Did you find a solution to this, also running into this problem
EDIT: If anyone else runs into this issue I found a solution here https://discord.com/channels/752553802359505017/1193729368753713192/1193748265003393083
Pacific sand lanceOP
hey nice that you were abel to resolve it. i ended up just using cypress for e2e testing
but for my next project ill try out jest and stuff again since you got it working and see if i like it