-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
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: 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 |
I think react-testing-library has some tricks to silence act warning for Jest when using their (globalThis as any).IS_REACT_ACT_ENVIRONMENT = false;
try {
await vi.runAllTimersAsync();
} finally {
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
} |
See Hiroshi's reply: #7196 (comment) I agree this is not related to Vitest, but to how |
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 withvi.runAllTimersAsync()
.This works but triggers
An update to ... inside a test was not wrapped in act(...)
warnings. Wrapping the async timer inact
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
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: