Testing Interview Questions
What's the difference between unit, integration, and E2E tests? Unit: isolated functions/components with mocked deps. Integration: multiple units/layers together (API + DB). E2E: full user flows through real browser
What is test coverage and what % should you aim for? % of code exercised by tests. Aim for 80%+ for critical paths, not 100% everywhere. Coverage ≠ quality — tests should assert correct behaviour, not just execute lines
Mock vs stub vs spy? Stub: hard-coded return value. Mock: verifiable expectations on calls. Spy: wraps real implementation, records calls. jest.fn() is a mock/stub; jest.spyOn() is a spy
What is snapshot testing? Serializes component output to file, fails if it changes. Useful for catching unintended UI changes. Update with --updateSnapshot. Don't rely on it alone — it tests structure not behaviour
How do you test async code? Return promise or use async/await. Wrap assertions in waitFor for React Testing Library. Use fake timers (jest.useFakeTimers) for setTimeout/setInterval
What makes tests brittle? Testing implementation details (internal state, CSS classes, exact DOM structure). Prefer: user-visible behaviour, accessible roles/labels, semantic queries (getByRole, getByLabelText)
When would you skip TDD? Exploratory/prototyping code, UI layout, rapidly changing requirements. Write tests after stabilisation. TDD shines for business logic, algorithms, APIs
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free