Replies: 3 comments 3 replies
-
Probably that's because Not sure about the 2nd one 🤔 |
Beta Was this translation helpful? Give feedback.
-
This looks very odd since act warning should come from it("tests the app", async () => {
const user = userEvent.setup();
render(<App />);
expect(screen.getByRole("paragraph")).toHaveTextContent("status: waiting");
await user.click(screen.getByRole("button"));
await new Promise(r => setTimeout(r, 500))
}) |
Beta Was this translation helpful? Give feedback.
-
You can patch import { beforeEach, vi } from "vitest";
beforeEach(() => {
const warnings: any[] = [];
const consoleError = console.error;
const spy = vi.spyOn(console, "error").mockImplementation((...args) => {
consoleError(...args);
const message = args[0];
if (typeof message === 'string' && message.includes('When testing, code that causes React state updates should be wrapped into act')) {
warnings.push(args);
}
});
return () => {
spy.mockRestore();
if (warnings.length > 0) {
throw new Error("Found act warnings");
}
}
}); |
Beta Was this translation helpful? Give feedback.
-
Apologies if this was previously asked or answered, I could not find existing discussions/issues on the topic. I created a reproduction repo at https://github.com/astorije/vitest-act-warning-repro, the following snippets are extracted from said repo.
I recently ran into a dreaded React
act()
warning in one of our tests and, while the warning was easy to fix, I cannot figure out 2 things about resolving it.Consider the following (somewhat minimal repro) React code:
as well as the following test:
The
act()
warning started to appear when addingglobals: true
to our Vitest configuration (which, separate issue, but necessary for proper automatic cleanup between each test, prior to that I was manually callingafterEach(() => cleanup())
, see testing-library/vue-testing-library#296).1️⃣ Why would the
act()
warning only start to appear withglobals
set totrue
?The warning itself was pretty easy to resolve: instead of calling
vi.waitFor()
coming from Vitest, I now usewaitFor
imported from@testing-library/react
.2️⃣ Why is the
act()
warning correctly handled by RTL'swaitFor
but not Vitest'swaitFor
?3️⃣ (After the fact and somewhat unrelated) How would you recommend making the
vitest
command (or another command I could tie tonpm test
) fail when anact()
warning is triggered? I find that often these get overlooked at review time and then it's a pain to pinpoint the commit that actually caused this (thank you git bisect!)Thank you!
Beta Was this translation helpful? Give feedback.
All reactions