Jest test finishing before onSuccess of useMutation is executed
Hi all,
We are having trouble with our react component test that relies on data being re-fetched after a mutation.
useMutation()
Sends a post request that mutates a resource on the server.
onSuccess()
executes queryClient.invalidateQueries() which then fetches the updated data, and causes a re-render within react.
This all works in the browser. In our Jest tests though, we can see when we console.log() inside the onSuccess() that the test suite is finishing before the onSuccess function has finished executing and therefore we cannot wait until the data has been fetched to make assertions.
We have reviewed the documentation.
We are using React 17 and React Query 3.34.19
What is the proper way to setup invalidating cache and making assertions on the updated render after the invalidation in our tests?
How do we indicate to react that there are more updates to wait for and not to end the test?
1 Reply
extended-salmon•4y ago
I would just assert that some updated text is visible on the screen because that's likely what your users will do