Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
How to test custom `Document` in Next.js?

I have a custom Document which is copied from the document:

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }

  render() {
    return (
      <Html lang="en">
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

I tried to write test for it with the following below:

import { render } from '@testing-library/react';
import MyDocument from '../_document.page';

describe('<MyDocument />', () => {
  it('should render without errors', async () => {
    const documentProps = {} as any;

    render(<MyDocument {...documentProps} />);
  });
});

But I get an error:

    TypeError: Cannot destructure property 'inAmpMode' of '(0 , _react).useContext(...)' as it is null.

       6 |     const documentProps = {} as any;
       7 |
    >  8 |     render(<MyDocument {...documentProps} />);
         |     ^
       9 |   });
      10 | });

What is the recommended way to test the custom document or how do I resolve this? Thank you.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!