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

297
Visualizações
How can I test React custom hook with RTL which uses react-query?

I've a custom React hook which uses react-query useQuery() in it-self:

const useFetchSomething = () => {
  const { data, isLoading } = useQuery('key', ....);
  return .......
}

I had to create a custom hook then use the useQuery() hook inside it, because it has some features and gets data from react-query!

I tried to test this hook with react-hooks-testing-library:

const { result } = renderHook(() => useFetchSomething());

but I get this error:

Error: Uncaught [Error: No QueryClient set, use QueryClientProvider to set one]

for this, how can I wrap this part: renderHook(() => useFetchSomething()) with Provider?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

renderHook accepts options, and one of them is the wrapper component, where you can specify a react component to wrap the test component in when rendering. This is a good place to put providers.

const createWrapper = () => {
  const queryClient = new QueryClient()
  return ({ children }) => (
    <QueryClientProvider client={queryClient}>
      {children}
    </QueryClientProvider>
  )
}

const { result } = renderHook(() => useFetchSomething(), {
  wrapper: createWrapper()
});

I have an in-depth guide to testing react query in my blog.

about 4 years ago · Santiago Gelvez Relatório
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