I'm trying to test a React Server Component, which returns a Promise. The standard render
method in Jest doesn't like that.
I checked the similar questions, but all are > 4 years old and deal with componentDidMount
etc.
/// Home.tsx
async function Home(props: Props) : Promise<JSX.Element> {
// call async functions to setup component
}
/// home.test.ts
describe('Home', () => {
it('renders a heading', () => {
render(<Home />);
}
}
Error:
'Home' can not be used as a JSX element
Any suggestions on how to work around this? An obvious solution is to make the component not return a Promise, but than I'm writing code to satisfy the test framework which is not ideal.
render
method? – AmigogetServerSideProps
then pass that to your component. – Amigorender
is not part of Jest. You must be using some other library for that. Are you using react-testing-library? – Nashrender
is most likely from react-testing-library, but this is not very important to the question at hand. There is no documentation on how to test React Server Components that are available in Next.JS 13.4. If anyone has achieved this, an answer would be awesome! – Inquiringimport { version } from "react"
. If you're using a meta framework like Next, don't usepackage.json
. – Danieladaniele