next-with-apollo icon indicating copy to clipboard operation
next-with-apollo copied to clipboard

Testing getInitialProps when using ctx.apolloClient

Open alexjfno1 opened this issue 4 years ago • 0 comments

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?

alexjfno1 avatar Jul 17 '20 12:07 alexjfno1