In React Testing Library tests, keep assertions outside components, and rely on proper global teardown.
cleanup() is automatic when RTL is configured normally. Only call cleanup() manually if your test setup does not provide an afterEach.expect(...) inside a React component. Instead, render the value to the DOM (or expose it via an element) and assert after render using RTL queries.Example (asserting after render, without component-level expect):
function Page() {
const { canFetchMore } = useInfiniteQuery(
'items',
(key, nextId = 0) => fetchItems(nextId),
{ initialData: [/* ... */] }
)
return <div data-testid="can-fetch-more">{String(canFetchMore)}</div>
}
const { getByTestId } = render(<Page />)
expect(getByTestId('can-fetch-more').textContent).toBe('true')
Enter the URL of a public GitHub repository