[react]: mutation has no loading status in test environment (vitest) #4379
Send to my agentDescribe the bug#
I'm new to react query, but faced issues while trying to test simple logic.
I've want to write test for src/components/Header/Header.ts that checks if loading spinner is on the page while doing mutation, the spinner relies on logoutMutation.isLoading. While everything works as expected in dev env, running tests always skips state: 'loading' and switched to 'error' or 'success' from 'idle'. I've added some logging (console.log(logoutMutation.isLoading, logoutMutation.status);):
In browser logout logging shows:
false 'idle'
true 'loading'
true 'loading'
false 'success'
But in test environment:
false idle
false idle
false success
Your minimal, reproducible example#
https://github.com/wight554/blog-template/tree/react-query-repro
Steps to reproduce#
- Run
yarn test test/src/components/Header/Header.test.ts - Face following error:
Error: expect(element).toHaveAttribute("aria-busy", "true") // element.getAttribute("aria-busy") === "true"
Expected the element to have attribute:
aria-busy="true"
Received:
aria-busy="false"
- Check the logging:
stdout | test/src/components/Header/Header.test.ts > Header > user is authenticated > user menu is opened > logout click > should render loading spinner
false idle
false idle
false success
Expected behavior#
Test should see state: 'loading'
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
OS: MacOS
react-query version#
^4.13.0
TypeScript version#
No response
Additional context#
vitest version: 0.23.4
wight554(GitHub profile, opens in a new tab) changed the title
Upd 2. It also works fine if I add
await sleep(10)to msw handlers, e.g:rest.post('*/api/v1/auth/logout', async (_req, res, ctx) => { await sleep(10); return res(ctx.status(200)); }),or add spy that adds sleep to original action like this:
const spy = vi.spyOn(userService, 'logoutUser'); spy.mockImplementationOnce(async () => { await sleep(10); return userService.logoutUser(); });But that's only needed for mutation, works fine as is for queries
could it be that we're just batching things so efficiently that if you return "too fast", the loading state is just batched away with the result state?
I've seen this in our tests as well but it's usually not a problem, but a good wanted thing. I'm adding at least a
sleep(10)to all query functions in tests...TkDodo added labelstests
could it be that we're just batching things so efficiently that if you return "too fast", the loading state is just batched away with the result state?
I've seen this in our tests as well but it's usually not a problem, but a good wanted thing. I'm adding at least a
sleep(10)to all query functions in tests...Delaying async actions works but I believe this should be at least documented somewhere, behavior is very unpredictable, took me a long time to investigate
it depends on what you are testing really. Sometimes, it's important that the intermediate loading state is shown. For queries, it is often not. You just want to assert that the data is there. If you want to capture loading states (like, a spinner visible on the screen), it makes a lot of sense to me that you have to make the network request a bit slower.
But yes, please document it:
https://tanstack.com/query/v4/docs/guides/testing
I'll close this as I don't think it's a bug. It's actually pretty cool that
useQuery(['foo'], () => 'bar')can skip the loading state.TkDodo closed this
Sign in to join this conversation.
Upd. It works fine if I replace
logoutUserwithasync () => { await sleep(10); }Might be something wrong with node-fetch interaction