All jobs / Frontend / web developer / Testing
Frontend / web developer interview question · 1–3 years
How do you assert a login button in React Testing Library?
The strongest answer
Find by role and name — getByRole('button', { name: /log in/i }) — not test-id-first or snapshot-everything
Why it works
RTL's point is the user tree: roles and names catch a11y holes and survive refactors.
What a good answer covers
- Query the way a user finds it.
- Prefer role + accessible name.
- test-id only when there is no name.
- Avoid class-name and snapshot-only tests.
Answers that fall short
- Close, but not the strongest: Default to getByTestId on every control
test-id is a fallback when the control has no accessible name — not the default. - Weak: Query by CSS class or enzyme-style internals
Class names are implementation details. - Red flag: Skip the test and say I click it in staging
Staging clicks don't catch regressions on the PR.
Who gets asked this
More Testing questions
- How do you test a search box that debounces input by 300 ms, in Jest or Vitest, without slowing the suite…
- A test passes when run alone and fails when the whole file runs, and the failing test changes when you…
- A Playwright or Cypress suite fails about one run in five on CI, in different places. The tests are full of…
- After a bad release, management asks you to enforce 90% code coverage on the frontend repository as a merge…
- How do you catch layout breakage before you translate into German and Hebrew?
- What is the difference between a unit test, a component test and an end-to-end test on the frontend, and…