I understand that I'm not supposed to add act()
around user events because the testing library does it for me, but I get warnings if I don't. Specifically, for example given a MUI component with the ripple effect, here's what I find:
import { Tab as MuiTab } from "@mui/material";
describe("Tab", () => {
it("MuiTab creates a warning ", async () => {
const user = userEvent.setup();
render(<MuiTab label="hello" />);
// yep. There's a warning here.
await user.click(screen.getByRole("tab"));
});
it("MuiTab does NOT create a warning with disableTouchRipple", async () => {
const user = userEvent.setup();
// disabling the animation fixes the warning
render(<MuiTab label="hello" disableTouchRipple />);
await user.click(screen.getByRole("tab"));
});
it("MuiTab does NOT create a warning with act", async () => {
const user = userEvent.setup();
render(<MuiTab label="hello" />);
// no warning
await act(async () => await user.click(screen.getByRole("tab")));
});
});
These warnings started when I upgraded @testing-library/react
to ^14.0.0
, which fixed other act
warnings.
But now with the upgrade, I have 1.7M lines of warnings in my test suite. It's not "wrong" in that the ripple effect hasn't ended by the end of the test and I also understand that using jest timers can help resolve this.
But it feels wrong to add act
throughout the testing suite or to litter my test code with fake timers just because MUI has some animations.
Given all that, what's the recommended way for me to fix these warnings?
Versions:
- "@mui/material": "^5.12.1"
- "@testing-library/jest-dom": "^5.16.5",
- "@testing-library/react": "^14.0.0",
- "@testing-library/user-event": "^14.4.3",
- "jest": "^29.5.0"
(Also, please don't tell me that I should be asserting on the expected behavior after the click. I know that. I'm just using the Tab
as an example and I get the warnings whether there's an expect
afterwards or not.)