testing-library / testing-library/dom-testing-library

findBy* no longer waiting when used with jest fake timers

Open
#988 39 comments 18 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
3.3k
Forks
474
PR merge metrics
No merged PRs in 30d

Description

  • @testing-library/dom version: 8.0.0
  • Testing Framework and version: @testing-library/react v12
  • DOM Environment: jest 27
Relevant code or config:
"jest": {
  "testEnvironment": "jsdom",
  "setupFilesAfterEnv": ["<rootDir>/jest-setup.js"]
}
// jest-setup.js
import '@testing-library/jest-dom'
What you did:

I was trying to use fake timers and findBy*

What happened:

findBy* doesn't wait when using fake timers as it used to.

Reproduction:

Created a repo with two branches, one using v11 of RTL and another one for v12.

v11 branch works fine (dom v7.31.2)
v12 branch is experiencing issues (dom v8)

https://github.com/deini/rtl-find/tree/v11
https://github.com/deini/rtl-find/tree/v12

Problem description:

When doing something like:

jest.useFakeTimers();

render(<Button />)

await screen.findByRole('dialog', {}, { timeout: 5000 })

I would expect it to wait for 5 seconds (unless I'm totally wrong 😅), however, seems like fake timers is now messing up with findBy* and it doesn't wait anymore. This happens both in jest 26 and 27 with both legacy and modern fake timers.

Suggested solution:

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 linked v11 and v12 reproduction branches and the screen.findByRole call using fake timers; compare the behavior across the DOM Testing Library versions. Review jest-setup.js and the async waiting entry point, then add a regression test covering Jest fake timers and confirm that findBy* waits according to its timeout.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.