Most people are searching for how to get rid of react act() errors in testing. I'm wondering the opposite. I have code that induces a state change. That state change should trigger a re-render in react (and it does). However, I did not act() wrap that state change, and an act() error was not emitted. Why is this?
Consider the following goofy component:
export function SweetTextboxBro() {
const [text, setText] = React.useState("");
return (
<>
<input
type="text"
role="input"
name="sweetbox"
onChange={(evt) => {
setText(evt.currentTarget.value);
}}
value={text}
/>
<p>{text}</p>
</>
);
}
Now, consider this minimal test:
import * as React from "react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { SweetTextboxBro } from "../SweetTextboxBro";
import { sleep } from "../timing";
test("it emits act errors", async () => {
render(<SweetTextboxBro />); // should internally call act()
const textbox = screen.getByRole("input");
const input = "weeee";
// begin state change!
userEvent.type(textbox, input);
await sleep(200);
// state updated! the DOM has changed! but no act() error... is emitted
expect(await screen.findByText(input)).toBeInTheDocument();
});
Act errors occur when react re-renders content outside of an act() call. I'm unclear why this case does not apply.