reactjs – Node.js mock context.req.headers.referer

I implemented a redirect back to the previously visited page in a project and now have to adjust some tests which are failing because:

Cannot read property 'headers' of undefined
TypeError: Cannot read property 'headers' of undefine

I searched for some time but could not find a good solution or transfer the given answer to my current problem.


export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {

  const referer = ctx.req.headers.referer
  let backlink = referer

  if (!referer) {

    backlink = ...

  return {
    props: {


it('getServerSideProps should return product', async () => {
    const ctx = {
      query: { productKey: mockedProducts[0].key } as ParsedUrlQuery,

    const result = await getServerSideProps(ctx as GetServerSidePropsContext)
    expect((result as any).props.product).toEqual(mockedProducts[0])

Is there a way to mock the context.req.headers.referer? I would also need this for my own yet-to-be-written test for the redirect back.

Read more here: Source link