Skip to content
Source
Sign inSign up
TanStack/query

[react]: mutation has no loading status in test environment (vitest) #4379

Send to my agent
Closedwight554(GitHub profile, opens in a new tab) opened this issue 5 comments

Describe 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

  1. Run yarn test test/src/components/Header/Header.test.ts
  2. 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"
  1. 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

  1. Upd. It works fine if I replace logoutUser with async () => { await sleep(10); } Might be something wrong with node-fetch interaction

  2. 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

  3. 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...

  4. TkDodo added labelstests

  5. 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

  6. 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.

  7. TkDodo closed this

Sign in to join this conversation.