Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async fake timers generate "not wrapped in act()" React warnings #7196

Open
6 tasks done
CharlineCouchot opened this issue Jan 8, 2025 · 2 comments
Open
6 tasks done

Comments

@CharlineCouchot
Copy link

Describe the bug

I am attempting to test a polling component. Essentially the component re-fetches a status every 5s until the status becomes ready.
To that effect, I use msw polling mocking in conjunction with vi.runAllTimersAsync().
This works but triggers An update to ... inside a test was not wrapped in act(...) warnings. Wrapping the async timer in act essentially cancels it out, and the test times out.

I managed to do a minimal repro that doesn't involve msw or RTK to confirm the warning is indeed coming from the fake timer.

So far my only fix is to silence the warning 😅
Thanks for any help!

Reproduction

https://stackblitz.com/edit/vitest-fake-timers-act-warnings

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700K
    Memory: 23.64 GB / 31.22 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh

Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.22 - ~/.yarn/bin/yarn
    npm: 10.2.5 - ~/.nvm/versions/node/v20.9.0/bin/npm
    pnpm: 9.15.3 - ~/.nvm/versions/node/v20.9.0/bin/pnpm

Used Package Manager

yarn

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 9, 2025

I'm not sure "fake timer" is the cause of warning since if I genuinely wait for the interval, that can cause the same warning like this:
https://stackblitz.com/edit/vitest-fake-timers-act-warnings-ngkkxrrs?file=src%2FButton.test.tsx

import { describe, expect, it } from 'vitest';
import { Button } from './Button';
import { render, screen } from './utils/test-utils';

describe('Simple working test', () => {
  it('should refetch status when deploying until ready', async () => {
    render(<Button />);

    console.log('[before sleep 2sec]');
    await new Promise((r) => setTimeout(r, 2000));
    console.log('[after sleep 2sec]');

    expect(await screen.findByText('Stop')).toBeInTheDocument();
  });
});

I don't know if there's any gotcha for this, but maybe wrapping vi.runAllTimersAsync with act is supposed to work? Probably we can check relevant issues on https://github.com/testing-library/react-testing-library (like this one? testing-library/react-testing-library#1198 (comment)) and also compare with jest.

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jan 9, 2025

I think react-testing-library has some tricks to silence act warning for Jest when using their waitFor API https://github.com/testing-library/react-testing-library/blob/85ac2534a59abd38880011e77da4bb8c716eba84/src/pure.js#L39, so probably it's legitimate to manually silence the warning like you might have already done:
https://stackblitz.com/edit/vitest-fake-timers-act-warnings-kmaxc5u8?file=src%2FButton.test.tsx

  (globalThis as any).IS_REACT_ACT_ENVIRONMENT = false;
  try {
    await vi.runAllTimersAsync();
  } finally {
    (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants