Async timeout when testing custom hook with jest #1197
Replies: 7 comments
-
Not an expert on tests, but aren't Anyways, I wouldn't rely on test execution order, and a "global" thing like the queryCache, where each test writes to / reads from, is certainly a problem. I see nothing wrong with wrapping each test in a custom |
Beta Was this translation helpful? Give feedback.
-
Unless I'm not understanding this doc page, everything in a single test file/module is run sequentially (that is,
100% agree with this, which is why I tried clearing the queryCache between each test as suggested here, but for some reason it still didn't work. That said, while working on another testing issue with a colleague I learned that the default queryCache should be frozen when running tests, so I'm gonna have to dig a little deeper into this as this should rule out any query sharing from the equation.
I also mostly agree on this. This is the solution I ended up going with, but it feels unnecessary and I was hoping to get a better understanding of what was going on in my tests. Again, I'm gonna dig deeper into the whole "default frozen cache" thing next week. |
Beta Was this translation helpful? Give feedback.
-
ah yes, you are right. I confused the evaluation of describe blocks with evaluation of tests. |
Beta Was this translation helpful? Give feedback.
-
I would argue that resetting the cache after each test feels unnecessary :) You have a shared mutable resource, and you have to do something when testing this. If you don’t eliminate the “shared” part, your tests could never run in parallel, which jest would allow via |
Beta Was this translation helpful? Give feedback.
-
Actually you're right 😛 I guess I'm mostly confused about why it didn't work. Whether I should be doing it or not is a different story. |
Beta Was this translation helpful? Give feedback.
-
agreed. would be cool if you'd find it out regardless of that 👍 |
Beta Was this translation helpful? Give feedback.
-
So I haven't had much more time to spend on this, but the one thing I've found is that clearing the query cache in The only thing I can think of right now is that some weird asynchronicity issue prevents the cache from being cleared properly in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
I sometimes get a timeout error when I try to run multiple tests on a custom hook which uses React Query and waiting for it to return mocked data.
To Reproduce
I've created a sample repository which reproduces the issue: https://github.com/Blond11516/react-query-test-issue.
The custom hook is https://github.com/Blond11516/react-query-test-issue/blob/main/src/hooks/useUsers.ts and its test is https://github.com/Blond11516/react-query-test-issue/blob/main/src/hooks/__tests__/useUsers.test.ts.
The test setup is as follows:
waitForNextUpdate
(or any equivalent variation ofwaitFor
)Note that all tests pass if I run them one by one, but the one where data is returned fails as soon as another test is run on the same hook.
Expected behavior
The query should return the data and trigger a hook update.
Desktop
Additional context
I've also applied the suggestions I found in other issues related to testing, namely running
queryCache.clear()
after each test and and awaiting for the query cache not to be fetching to avoid theact
warnings, but they didn't fix my issue.I've actually found three ways of making the tests pass, but they all feel wrong in some way:
ReactQueryCacheProvider
with a new QueryCache every time. example herewaitForNextUpdate
in every test). example hereThese solutions (especially 1 and 2) and the fact that all tests pass independently lead me to believe that the queryCache isn't cleared properly between tests, causing the query to stay cached and not return despite the mock being updated to return a data.
Any help/explanation on this would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions