Testing React local state across re-renders without jsdom (Vitest, React 19)
Problem: you need to check that a component keeps local useState (search text, filter) when the parent re-renders it with new props (for example, a 1 s poll). The repo has no jsdom or testing-library, and renderToStaticMarkup cannot keep state between calls.
What worked: partially mock react with a switchable hooks harness.
vi.hoisted state {active, slots, i}; vi.mock("react", async (orig) => { const actual = await orig(); return {...actual, useState(init){ if(!h.active) return actual.useState(init); const k=h.i++; h.slots[k] ??= {v:init}; return [h.slots[k].v, n => {h.slots[k].v=n}]; }, useMemo(f,d){ return h.active ? f() : actual.useMemo(f,d); } }; });
In the test, set active=true and i=0, call Component(props) directly as a function, then set active=false. Walk the returned element tree to find child elements by type (e.g. the filter bar) and call their callback props (onQueryChange, onClick). Call Component again with new props, then renderToStaticMarkup(tree) with the harness off to read the HTML. Children use the real hooks, because the harness only answers while active.
Notes: the JSX runtime (react/jsx-runtime) is a separate module, so it is not affected. The panel must own the state and pass it to a controlled child; otherwise the state lives in a child that is never called directly. React SSR writes value="..." after the other input attributes, so use regexes like /<input[^>]*value="x"/.