testing-library / testing-library/react-testing-library

Test based on `findByText` assertion fails after 551ms, despite default timeout being 1000ms

Open
#1,414 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

  • @testing-library/react version: 16.3.0
  • Testing Framework and version: Vitest 3.2.4
  • DOM Environment: jsdom 26.1.0
Relevant code or config:
expect(
  await screen.findByText(
    "text which is supposed to be found after loading state is passed",
  ),
).toBeInTheDocument();
What you did:

Ran a test which failed on this assertion. Only sometimes. Typically in CI (Github Actions).

What happened:

× path/to/my.test.tsx > x > shows expected content 551ms
→ expect(element).toBeInTheDocument()
element could not be found in the document
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
FAIL path/to/my.test.tsx > x > shows expected content
Error: expect(element).toBeInTheDocument()
element could not be found in the document

Reproduction:

Does not reproduce easily. Is flaky. Trying to understand if there is some misunderstanding in how things are supposed to work.

Problem description:

When running a test based on findBy, we sometimes find them to be flaky. It is sometimes resolved by using waitForElementToBeRemoved on the previous state, but that just adds another possible race condition, and also tends to be flaky.

I expect the test to run for at least 1000ms (as per findBy logic documented here), but sometimes find that the test fails much sooner (after 551 ms in the example above).

Suggested solution:

I'm trying to deduce a cause here for further investigation.

  • Is findBy resolving sooner than expected? If yes, it seems like a problem internal to the library, possibly https://github.com/testing-library/react-testing-library/issues/865?
  • Is the (vitest) reported time unreliable, and we may have hit the 1000ms timeout despite 551ms being reported? If so, maybe extending the default timeout could be beneficial, even though hundreds of similar tests tend to pass every time, so it seems odd.

Overall, this seems like a strange problem, and I'm not expecting a straight-forward fix, but would like you to know about it, and would be grateful for any pointers on how to analyze further.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied screen.findByText assertion and the listed @testing-library/react 16.3.0, Vitest 3.2.4, and jsdom 26.1.0 versions. Investigate whether the failure reaches the documented 1000ms timeout or whether Vitest reports the duration inaccurately, using the linked findBy documentation and prior issue for context. Done means identifying the source of the flaky early failure or documenting that it is external.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react, typescript
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.