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

124
Views
Next JS Unit test page with multiple components inside using jest

I'm trying to do unit testing for the first time. I want to test my index.tsx page with multiple components inside. My index.tsx page structure is like this

index.tsx

return (
  <div>
    <DefaultLayout
      meta={
        <Meta
          title=""
          description=""
          canonical="https://www.com"
        />
      }
    >
      {!props.acceptCookie && <CookieConsent />}
      <Header promo={props.promo} />
      <HomeBody
        totalPostsToday={props.totalPostsToday}
        promo={props.promo}
        postData={props}
        partners={props.partners}
      />
      <Footer />
    </DefaultLayout>
  </div>
)

If I run my test code I got an error says

Uncaught [Error: Objects are not valid as a React child (found: object with keys {store, .... If you meant to render a collection of children, use an array instead.

index.spec.tsx

import { act, render } from '@testing-library/react'
import { RouterContext } from 'next/dist/shared/lib/router-context'

import Index from '@/pages'
import { createMockRouter } from '@/test-utils/createMockRouter'
import { renderWithProviders } from '@/test-utils/reduxMockProvider'

describe('Index page', () => {
  test('should proper render components', async () => {
    await act(() => {
      render(
        <RouterContext.Provider value={createMockRouter({})}>
          {renderWithProviders(<Index />)}
        </RouterContext.Provider>
      )
    })
  })
})

Aside of my error, does my approach fine or is it better if I do isolation test per components?

about 4 years ago · Santiago Trujillo
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!