React Query Test with MSW - renderHook `current` result always returns null
I am trying to test my react query hook using msw but
renderHook's result's current value is always null. Why?
Note: the reason fetch does not include a URL is because this is a Rails Propshaft app and React and Rails apps share the same port.
In the real app, everything works and the hook returns data just fine but not in the test.
[tutorial I followed][1]
[reproducible sandbox][2]
spec example
spec error:
[1]: https://tkdodo.eu/blog/testing-react-query
[2]: https://codesandbox.io/s/blissful-dewdney-r6k4mm?file=/src/App.spec.tsxTesting React Query
Let's take a look at how to efficiently test custom useQuery hooks and components using them.
7 Replies
ratty-blushOP•3y ago
tried using
nock instead of msw and I face the same issue.
the following is always what I see when I log isLoading and data in from the hook in the spec
more debug logs:
tried the following react-query versions:
the strange thing is that I can see formattedAccounts being returned in the hook but data is undefined in DummyComponent 🤔ratty-blushOP•3y ago
The example repo (https://github.com/TkDodo/testing-react-query) has the following helper, which made the tests pass
vs the helper I was trying to use
Why does the original have to return a function that returns a component?
GitHub
GitHub - TkDodo/testing-react-query
Contribute to TkDodo/testing-react-query development by creating an account on GitHub.
xenophobic-harlequin•3y ago
because
wrapper passed to render from testing-libary is a function and not a component 🤷♂️xenophobic-harlequin•3y ago
oh, I stand corrected: https://testing-library.com/docs/react-testing-library/api/#wrapper
wrapper is a component 🤔
API | Testing Library
React Testing Library re-exports everything from DOM Testing Library as well
xenophobic-harlequin•3y ago
possibly, the wrapper gets re-rendered, so you need to make the QueryClient stable ?
https://tkdodo.eu/blog/react-query-fa-qs#2-the-queryclient-is-not-stable
React Query FAQs
Answering the most frequently asked React Query questions
xenophobic-harlequin•3y ago
my wrapper kinda side-steps this by having the queryClient in the closure
ratty-blushOP•3y ago
wild..thanks for the refs!
really easy thing to miss I think tho. Until I cloned your repo and ran it locally, I was stuck on this, probably for a good 24hr