useQuery not catching/handling the error when throwing error from mocked API in tests
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:8
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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'
anderror === null
until it fails on the third attemptTry setting
retry: false
in youruseQuery
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 usersHope 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.
Yep. Make sure you are accounting for
retry
or setting it to0
.