I have written a Nextjs 13 page component which is an async function. It calls an API inside it and passes that response to the child component.
export default async function Home() {
const participantsData: ParticipantInfo[] = await getParticipantsInfo();
return (
<div data-testid="home-page">
<ParticipantsTable participantsInfo={participantsData} />
</div>
);
}
While unit testing it with jest,
const getRender = (): RenderResult => {
return render(<Home />);
};
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve(participantsMockData),
}),
) as jest.Mock;
describe("Home Page Tests", () => {
it("renders Home Page Component", async () => {
getRender();
expect(screen.getByTestId("home-page")).toBeDefined();
});
});
I am not able to render it because functional component is made async. I am getting the following error
console.error
Error: Uncaught [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.]
at reportException (C:\git\axo-auth-ui\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:66:24)
at innerInvokeEventListeners (C:\git\axo-auth-ui\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:353:9)
at invokeEventListeners (C:\git\axo-auth-ui\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:286:3)
at HTMLUnknownElementImpl._dispatch (C:\git\axo-auth-ui\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:233:9)
at HTMLUnknownElementImpl.dispatchEvent (C:\git\axo-auth-ui\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:104:17)
at HTMLUnknownElement.dispatchEvent (C:\git\axo-auth-ui\node_modules\jsdom\lib\jsdom\living\generated\EventTarget.js:241:34)
at Object.invokeGuardedCallbackDev (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:4213:16)
at invokeGuardedCallback (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:4277:31)
at beginWork$1 (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:27451:7)
at performUnitOfWork (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:26560:12)
at workLoopSync (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:26466:5)
at renderRootSync (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:26434:7)
at performConcurrentWorkOnRoot (C:\git\axo-auth-ui\node_modules\react-dom\cjs\react-dom.development.js:25738:74)
I tried to make the render async, but it did not work. I found the only solution is to not make the function async, and make an api call inside useEffect, but this makes the component client component, which I do not want. How do i render a functional component which is async in jest?