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

150
Views
Next router.useRouter is not a function even mocking RouterContext

I'm trying to test a component that use useRouter from next/router hook to get the route, but I'm getting the following error even mock RouterContext:

Error: Uncaught [TypeError: (0 , _router.useRouter) is not a function]

my application use redux and it's also being mocked with react-redux Provider and mockStore.

This is my util code to render my component:

const getCustomWrapper =
  (router, initialState) => {
    const Wrapper = ({ children }) => {
      const store = mockStore({
        ...initialStateModel,
        ...initialState
      })

      return (
        <Provider store={store}>
          <RouterContext.Provider value={{ ...mockRouter, ...router }}>
            {children}
          </RouterContext.Provider>
        </Provider>
      )
    }

    Wrapper.propTypes = {
      children: PropTypes.node
    }

    return Wrapper
  }

const customRender = async (ui, { options, router, initialState } = {}) => {
  const Wrapper = getCustomWrapper(router, initialState)

  return render(<Wrapper>{ui}</Wrapper>, options)
}

And my tests are smth like that:

const createComponent = (props = defaultProp, initialState = defaultInitialState) => {
    customRender(<MyComponet {...props} />, { initialState })
}


it('should render my text', async () => {
    createComponent()

    const myText = screen.getByText('my text')

    expect(myText).toBeInTheDocument()
})

Versions

next: 10.1.2,
react-redux: 7.2.5,
react: 16.13.1

updates it's not a option right now =/

thx in advance

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!