React Query Test with MSW - useMutation test returns `undefined` for current.data property
https://codesandbox.io/s/great-wood-whq7z5?file=/src/hooks.spec.tsx
I am basically copying the implementation from this repo (https://github.com/TkDodo/testing-react-query) and writing a test for useMutation.
Taking inspiration from this tutorial (https://www.js-howto.com/react-query-usemutation-with-jest-testing/)
When I run the test,
current.data is undefined. Why?
hooks.spec.tsx
msw handler
mutation
rostgoat
CodeSandbox
great-wood-whq7z5 - CodeSandbox
great-wood-whq7z5 by rostgoat using @tanstack/react-query, @testing-library/jest-dom, @testing-library/react, @types/jest, axios, express, jest, jest-environment-jsdom, loader-utils
GitHub
GitHub - TkDodo/testing-react-query
Contribute to TkDodo/testing-react-query development by creating an account on GitHub.
user
JS-HowTo
React-Query useMutation with Jest Testing | JS-HowTo
React-Query useMutation hook with Jest and React-testing-library for testing the UI components and the hooks. Complete full-stack example.
2 Replies
optimistic-goldOP•3y ago
I am able to get the tests to pass if I use a dummy component that calls the mutation but not
act. Why?
Here is what I used to get the tests to pass:
spec
ExampleMutation
foreign-sapphire•3y ago
I'm also having trouble trying to create tests for
useMutation. I'm doing almost the same as you, but isSuccess always returns false
cc: @Maintainer - Query