Turning off query retries does not work during tests
I am trying to set the query retries to false for my tests using Jest / RTL, I was reading https://tkdodo.eu/blog/testing-react-query
It says specifying the options to:
But this does not work as it still retries the query, however if I set the actual query to retry false, then the test passes, however I do not want to turn off retries to the actual query, just for the test only.
Any Idea's, what could be going wrong? I would have thought as per the article that setting it in the test would effect the component tree?
I have reset the handler also for MSW, to get a 500 error response.
Do I need to change anything anywhere else?
Testing React Query
Let's take a look at how to efficiently test custom useQuery hooks and components using them.
12 Replies
metropolitan-bronze•4y ago
Hi for testing, in addition to
retry: false we also set cacheTime to Infinity so that queries are never removed from the cache.flat-fuchsia•4y ago
If your query has
retry defined on the useQuery itself, the defaultOptions will be overwritten by that.plain-purpleOP•4y ago
So the query itself does not have on it, I do have the following:
Is that on the test query?
flat-fuchsia•4y ago
then it should work, as you can see in the repo attached to the blog post where it does work
metropolitan-bronze•4y ago
On the query or as default options in the queryclient used in tests so that it can apply on all queries
plain-purpleOP•4y ago
hmmmm, it's not working is there a best practice for tests with react-query?
flat-fuchsia•4y ago
yes, it's in my blog post from above 😅
what you can do is: fork my repo and try to make it look like your code and note when the tests start to break
it's probably a tiny thing that's different in your impl
plain-purpleOP•4y ago
I think, the default query must be overwriting the test queries somehow, in your article you state it is best to have create a new query client per individual test, does your repo show this? Where is the link to the repo?
flat-fuchsia•4y ago
yes. at the end of the article
plain-purpleOP•4y ago
Does the same code and concepts apply with react-query V3?
flat-fuchsia•4y ago
yes
plain-purpleOP•4y ago
thanks will give it a try, if my tests as in different files have got different query clients set for each test / test (Suite) could it be possible that the test files could be picking up query clients from that?