80%
Testing Test pyramid, unit tests, mocking, React Testing Library, E2E0/23 questions known
1. What to test
DefinitionTests give confidence to change code.
Unit: one function/module in isolation · Vitest, Jest
Integration: pieces together: API + DB, component + hooks · Supertest, Testing Library, test DB
End-to-end: the real app in a browser · Playwright, Cypress
TipTest behaviour, not implementation details — tests should survive a refactor that keeps behaviour the same.
2. Unit tests (Arrange–Act–Assert)
Mock: replaces a dependency and records calls.
Mock the boundaries (network, time, randomness), not your own logic.
Fake timers (vi.useFakeTimers()) for debounce/timeout code.
3. Testing React components
Query priority: getByRole → getByLabelText → getByText → getByTestId (last resort).
getBy throws if missing, queryBy returns null (use it to assert absence), findBy waits (async).
Mock the network with MSW so components use their real data-fetching code.
4. API and end-to-end tests
Use a separate test database; reset or seed it per test file.
E2E: test the critical paths (sign-up, checkout, payment), not every edge case.
Coverage is a guide, not a goal — 100% coverage with weak assertions proves little.
TDD: write a failing test (red), make it pass (green), clean up (refactor).
ReviseMany unit, some integration, few E2E.
Most asked (8)
QWhat is the difference between unit, integration and end-to-end tests?- Unit: tests one small piece (a function, a hook, a component) in isolation, with its dependencies mocked.
QJest vs Vitest: what are the basics of writing a test?Both are test runners with the same API: describe groups tests, it/test defines one, expect with matchers asserts (toBe, toEqual…
QWhat is the difference between mocks, stubs, spies and fakes?They are all test doubles, stand-ins for real dependencies: - Stub: returns canned answers (getUser always returns Asha).
QWhat is the philosophy of React Testing Library?"The more your tests resemble the way your software is used, the more confidence they can give you."
QWhich React Testing Library queries should you use, and what is the difference between getBy, queryBy and findBy?Priority order: getByRole (with { name }) first, then getByLabelText, getByPlaceholderText, getByText, and getByTestId only as a last…
QWhat would you test in a React component?Test behaviour from the user's point of view, focusing on what could realistically break: - it renders the right content for given props…
QHow do you test asynchronous code?Make the test wait for the async work, otherwise it finishes before the assertion runs and passes falsely.
QWhat is TDD? What are its pros and cons?Test-driven development follows a short loop: red, green, refactor.