act() does not flush updates scheduled before it, so a following fireEvent's update is deferred too #99
Version: @tanstack/redact 0.2.2
If a state update is scheduled outside act() and then an event runs inside act(), React's act flushes both before it returns. Redact flushes neither: both updates land one microtask later. A click on its own is flushed synchronously, so this only shows up when an update is already pending.
The common case: a test calls element.focus() (whose onFocus sets state, e.g. a tooltip trigger opening on focus) and then fireEvent.click(element). Assertions right after the click see the old state under Redact.
Repro (Vitest + jsdom + Testing Library)#
import { render, fireEvent } from "@testing-library/react";
import * as React from "react";
function App() {
const [focused, setFocused] = React.useState(false);
const [open, setOpen] = React.useState(true);
return (
<button
data-focused={String(focused)}
onFocus={() => setFocused(true)}
onClick={() => setOpen((o) => !o)}
>
{open ? "open" : "closed"}
</button>
);
}
it("act flushes an update that was scheduled before it", async () => {
const { getByRole } = render(<App />);
const button = getByRole("button");
button.focus(); // onFocus -> setState, outside act
fireEvent.click(button); // wrapped in act by Testing Library
expect(button.textContent).toBe("closed");
expect(button.dataset.focused).toBe("true");
});
| state read | right after fireEvent.click | after await Promise.resolve() |
|---|---|---|
| React 19 | closed / focused true | closed / true |
| Redact 0.2.2 | open / focused false | closed / true |
| Redact, click without prior focus | closed | closed |
Expected#
Like React: leaving the outermost act() scope flushes all pending work, including updates queued before the scope started.
Found in a TanStack Start app: two shell tests (sidebar toggle and detail panel with base-ui tooltip triggers) focus the trigger, click it and assert the new state and the focus hand-off synchronously. They pass under React and fail under Redact; with an extra await they pass.
tannerlinsley closed this
Sign in to join this conversation.