Skip to content

test(react): poll rendered text instead of fixed sleeps - #38

Merged
jimmy-phantom merged 1 commit into
mainfrom
fix-flaky-component-text-assertions
Jun 8, 2026
Merged

jimmy-phantom merged 1 commit into
mainfrom
fix-flaky-component-text-assertions

Conversation

@jimmy-phantom

Copy link
Copy Markdown
Collaborator

Problem

Several component() tests in component.test.tsx waited a fixed await sleep(10) then asserted rendered DOM text in a single shot (expect(...element().textContent).toBe(x)). Under CI runner load the fetch and React re-render don't always finish within that window, so the assertion samples Loading and fails. The react vitest project (chromium browser mode) has no retry configured, so one slow render fails the run.

This is the flake reported for should deduplicate multiple components using same query (AssertionError: expected 'Loading' to be '5'), which passed on a clean re-run with no code change.

Fix

Replace the racy sites with the polling assertion already used elsewhere in the file:

await expect.element(getByTestId('a')).toHaveTextContent('5');

expect.element(...).toHaveTextContent(...) retries until the DOM matches or the test times out, instead of sampling once. This also removes the now-redundant toBeInTheDocument() + sleep scaffolding.

Notes

  • The render-count test now polls the updated text first, then asserts renderCount, so the counts are only read once the update has fully propagated.
  • Left two tests on their sleeps on purpose: the stale-data / no-rerender tests assert that data did not change, and the "keep previous value during refetch" test samples a transient in-flight state on the query object (not rendered output). Polling would mask those.
  • Left toBeInTheDocument() + hard-toBe() pairs whose testid only renders once isReady (loading renders a no-testid Loading...); those already wait for data.

Verification

All 99 react browser tests pass (21/21 in component.test.tsx).

🤖 Generated with Claude Code

Several component() tests waited a fixed `await sleep(10)` then asserted
rendered DOM text in a single shot. Under CI load the fetch and render
don't always finish in that window, so the assertion samples `Loading`
and fails (the react vitest project has no retry configured).

Replace the racy sites with the polling `expect.element(...).toHaveTextContent(...)`
assertion already used elsewhere in the file, which retries until the DOM
matches or the test times out. Left the stale-data and in-flight-state
tests on their sleeps, since those assert that something did not change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jimmy-phantom
jimmy-phantom requested a review from pzuraq June 5, 2026 18:06
@jimmy-phantom
jimmy-phantom merged commit 0b51482 into main Jun 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants