Prompt
What should I use for stable tests on changing DOMs
Latest observation
Nov 11, 2026
OpenAI APIWeb search: off
For stable tests on changing DOMs, the best approach is to test behavior and user-visible outcomes, not exact DOM structure. In practice:
Prefer
- Accessible selectors:
getByRole,getByLabelText,getByText - Stable test IDs only when needed:
data-testid - Assertions on visible state: text, attributes, enabled/disabled, presence/absence
Avoid
- Deep CSS selectors
- Reliance on element order or full DOM snapshots
- Selecting by fragile classes or auto-generated IDs
Good rule of thumb
Use the same thing a user would use to find the element:
- Buttons by role/name
- Inputs by label
- Links by text
Example
screen.getByRole('button', { name: /save/i })
screen.getByLabelText(/email/i)
screen.getByText(/welcome back/i)
If the DOM changes often
- Keep tests focused on one behavior per test
- Mock only unstable external dependencies
- Use
waitFor/ async queries for dynamic updates - Add
data-testidfor elements that have no good accessible selector
If you want, I can give you a recommended selector hierarchy for React Testing Library or Cypress.