isError not firing in mutate tests using @testing-library/react-hooks #2300
Replies: 2 comments 1 reply
-
So I figured out one issue around this, which is that if you have multiple tests, you need to create a QueryClient for each of those tests, as per here #270 Otherwise it reuses the same QueryClient state for subsequent tests. However, the main idea is similar, it is just timing out in the error case rather than firing isError. I have seen about having to set retries to 0 but that seems to not be working for me. |
Beta Was this translation helpful? Give feedback.
-
Aha, after some tweaks, I was able to get it to work with isError. I created a testHelpers.tsx file with the following code:
And then in the test file it looks like:
This fires isError correctly. Just sharing here in case anyone else runs across the same issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When I write a test for the error path for a custom hook using @testing-library/react-hooks, it seems to never get to isError status, but rather, only isSuccess.
The idea of this custom hook use case is that it uses
fetch
to delete a data record via API call, and then, rather than have another API call to re-fetch the updated data set, it just deletes it locally throughsetQueryData
.Here is an example custom hook:
And here is an example of the test in question:
I am able to get isError to be true in the more verbose way of basically rendering a component, and then simulating a click to trigger the mutation, etc., similar to how tests are done here - https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/useMutation.test.tsx but I wanted to know if there was something I was missing about testing the error cases using @testing-library/react-hooks.
Beta Was this translation helpful? Give feedback.
All reactions