question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useQuery not catching/handling the error when throwing error from mocked API in tests

See original GitHub issue

Describe the bug I am using useQuery to fetch data from API. When I run the application in the browser, if the backend throws error, the error is properly handled by useQuery and displayed on the screen.

But when I am trying to do the same thing via testing, once I throw an error from the test itself, even tho my function is throwing the error properly, useQuery is not handling the error and it’s constantly in loading state.

Codesandbox example

https://codesandbox.io/s/cool-glitter-0vvki?file=/src/App.test.js

Expected behavior Expected isError to be true when throwing error from the tests.

Desktop (please complete the following information):

  • OS: [MacOS Catalina]
  • Browser: [Chrome]
  • Version: [e.g. 22]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

18reactions
methercommented, Aug 18, 2020

I think I had the same issue with constant loading states and I believe this was due to the fact that by default react-query retries 3 times on error, leaving status === 'loading' and error === null until it fails on the third attempt

Try setting retry: false in your useQuery options e.g. const { ... } = useQuery(["toDo"], getToDo, { ..., retry: false });

If that addresses the problem in your tests, you can create a custom renderer for your tests and use the ReactQueryConfigProvider to set retry: false for consistent tests without having to disable it for your end users

Hope that helps!

P.S. I tried to fork your example but there seems to be some errors in there and I didn’t have time to address them.

13reactions
tannerlinsleycommented, Aug 20, 2020

Yep. Make sure you are accounting for retry or setting it to 0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useQuery not catching/handling the error when throwing error ...
I am using useQuery to fetch data from API. When I run the application in the browser, if the backend throws error, the...
Read more >
Handling operation errors - Apollo GraphQL Docs
graphQLErrors are ignored ( error.graphQLErrors is not populated), and any returned data is cached and rendered as if no errors occurred. all, Both ......
Read more >
Testing | TanStack Query Docs
Testing Network Calls​​ The primary use for React Query is to cache network requests, so it's important that we can test our code...
Read more >
How to imperatively handle an error, with react-query?
Have you tried using the onError callback? It looks like this : const useTodos = () => useQuery(['todos'], fetchTodos, { // ⚠️ looks...
Read more >
Async data made simple with React Query - Hey! I'm Tyler
As mentioned, you'll want to make the cache key unique, however if you wanted to declare a separate useQuery hook somewhere else with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found