next-with-apollo
next-with-apollo copied to clipboard
Testing getInitialProps when using ctx.apolloClient
I have a page component that is using getInitialProps
to fetch data using Apollo via the ctx.apolloClient
.
Page.getInitialProps = async (ctx: ApolloPageContext) => {
const response = await ctx.apolloClient.query({ query: QUERY, variables: { productId: '123' } });
return { product: response.data.product };
};
When testing a normal component I can use the MockedProvider
from @apollo/react-testing
to stub about the queries. However I'm a bit lost to how I would test the getInitialProps
function.
I could just mock out the ctx.apolloClient.query
function to return the expected data but this won't provide much value. Is there a way to mock out the apollo client in the NextJS context similar to the way MockedProvider
works?