Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda