All jobs / Full-stack developer / Testing
Full-stack developer interview question · 1–3 years
How do you keep the real API out of a component test without the test drifting from the real thing?
The strongest answer
Intercept at the network boundary — a request mock such as MSW returns realistic responses, the component runs its own fetching code, and the same handlers cover empty and error responses
Why it works
Right — mocking the network keeps the component’s real data layer under test, and one set of handlers covers success, empty and failure.
What a good answer covers
- Mock the network, not your own code.
- Keep the real fetching path under test.
- Share handlers across tests.
- Cover empty and error responses.
Answers that fall short
- Close, but not the strongest: Stub the fetch function in each test file and return the JSON that component expects
It works, and a per-file fetch stub drifts from the real response and quietly replaces the code you meant to test. - Weak: Pass the data in as props and leave the loading and error paths out of the tests
Props-only tests never exercise loading, error or retry — the states users actually hit. - Red flag: Let the tests call the real staging API, so we know it is truly working end to end
A unit test that needs the network is a flaky test that fails while staging redeploys, and it can write real data.
Who gets asked this
More Testing questions
- Do you test this against a real database or against a mocked repository?
- How do you test the parts that are not a request — an incoming webhook and a nightly job?
- Someone deleted a required field and merged because the unit tests still passed. What should CI do?
- The end-to-end suite passes alone and fails when it runs in parallel. What is wrong with the test data, and…
- How do you stop the API from breaking the UI in CI?
- Where should the end-to-end suite run in CI — against shared staging, or against a stack built for the run?
- What would you run against production itself, and what must such a check never do?
- You inherit a slow, flaky suite the team has learned to ignore. What is your strategy for the next quarter?