Implementation-Agnostic Assertions
Any test that asserts on rendered UI should assert what the user perceives. Prefer visibility, enabled, editable and visible-text checks over DOM count, attached-state and attribute checks.
Why
A test fails only when what the user perceives changes.
How To Apply
- For presence:
toBeVisible(andnot.toBeVisiblefor absence). - For interaction state:
toBeEnabled/toBeDisabled,toBeEditable,toBeChecked,toBeFocused. - For content:
toHaveText/toContainText,toHaveValue. - Reach for
toHaveCountonly when the assertion is about cardinality ("the list shows exactly three results"), never as a stand-in for "this element is gone". - Reach for
toHaveAttributeonly when the attribute is part of the user-facing contract (aria-label,href,for), never for internal data attributes. - A change in how visibility is achieved leaves these assertions passing: a conditional render swapped for
display: none, a lazy render, an exit animation, a portal.