Skip to content

feat: warn about unmounted elements with eventDiagnostics - #1970

Open
mdjastrzebski wants to merge 1 commit into
mainfrom
feat/unmounted-element-diagnostics
Open

mdjastrzebski wants to merge 1 commit into
mainfrom
feat/unmounted-element-diagnostics

Conversation

@mdjastrzebski

Copy link
Copy Markdown
Member

Summary

When a test keeps a reference to an element and a re-render removes it, fireEvent and userEvent skip events on that element on purpose. With eventDiagnostics on, they didn't report this clearly:

  • fireEvent skipped the event without any warning.
  • userEvent.press() warned about missing responderGrant and responderRelease handlers, which points in the wrong direction.
  • userEvent.type() didn't warn, because it still updated the detached TextInput's native state.

With eventDiagnostics on, both now warn that the element is unmounted and suggest querying it again:

Cannot fire the "press" event on an unmounted element. Query the element again after the update that removed it.
press() interaction did not call any event handlers. The element is unmounted. Query the element again after the update that removed it.
  • Interaction records whether its target was mounted when the interaction started. A handler that unmounts the element during press() therefore still doesn't warn.
  • The warning is opt-in like the other diagnostics, so existing suites see no change. Throwing an error would be a breaking change in v14, and DOM Testing Library doesn't throw for detached nodes either.

Test plan

  • fireEvent warns about an unmounted element when eventDiagnostics is on (inline snapshot).
  • userEvent.press() and userEvent.type() warn about an unmounted element (inline snapshots).
  • No warning when a press handler unmounts the element.
  • yarn test (961 passed), yarn typecheck, yarn lint, yarn format:check, yarn docs:check.

Known gap: after the whole tree is unmounted, fireEvent and userEvent still throw test-renderer's "Cannot access .container on unmounted test renderer" error. This PR doesn't change that.

🤖 Generated with Claude Code

`fireEvent` skipped events on unmounted elements silently, and `userEvent`
either warned about missing responder handlers or not at all. With
`eventDiagnostics` on, both now say the element is unmounted and suggest
querying it again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.57%. Comparing base (32b3d1e) to head (a7e70e7).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1970   +/-   ##
=======================================
  Coverage   98.57%   98.57%           
=======================================
  Files          87       87           
  Lines        1750     1760   +10     
  Branches      460      483   +23     
=======================================
+ Hits         1725     1735   +10     
  Misses         24       24           
  Partials        1        1           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

1 participant