Cypress tests failing after upgrading from react-query v3 to v5
I'm trying to upgrade to v5 of react-query, but I'm experiencing a weird issue where some of my e2e Cypress tests are no longer working. In these tests I am mocking data returned from an API. Running the tests with breakpoints, I can see in the network tab that the mocked data is correctly appearing in the response. However once the
useQuery completes, the component doesn't re-render so the data variable is undefined
Here's vaguely what my code looks like:
When I run the Cypress test the console looks like this:
If I run my app outside of Cypress using Vite, everything works fine. The console looks like this:
When I was on v3 on react-query my code looked like:
And running when Cypress the console looked like:
So to me it looks like my component isn't re-rendering when it should once it has received the data from the useQuery
I am using :
- "react": "^19.1.0"
- "@tanstack/react-query": "^5.76.1"
- "cypress": "^14.3.2"5 Replies
like-gold•7mo ago
do you have a stable queryClient ?
flat-fuchsiaOP•7mo ago
It was not stable, I've changed things so that it is now created outside the component it was previously inside. But my tests are still failing for some reason.
I've found the culprit. In my Cypress test I had the following:
For some reason removing that made things start working again. Not sure why setting the Cypress clock broke things to begin with though?
Is
useQuery sensitive to the date with regards to it's cache invalidation calculations?like-gold•7mo ago
because we use
setTimeout somewhere and cypress clock overwrites thatflat-fuchsiaOP•7mo ago
I'm assuming that the use of
setTimeout was added after v3 then? If I want to mock dates for my Cypress tests is there any alternative available to me? Or is this best to ask in the Cypress Discord?like-gold•7mo ago
Try this:
notifyManager.setScheduler(cb => cb())
In tests only!