mediumConceptCodingDesignMidSenior
How do you test a service that calls a third-party API, without calling it?
4 min readUpdated 2026-08-15
Answer
Short answer
Substitute at the network layer or behind your own client interface, not at the HTTP library's function. Stubbing the library couples the test to how you make the call rather than to what you send, so the test keeps passing through refactors that change the request and breaks on refactors that change nothing.
Was this answer helpful?
Likely follow-ups
- Your fake returns 200 and the real API started returning 202. What catches that?
- How do you test the timeout path without waiting for the timeout?
- When would you record real responses instead of hand-writing them?
- Where do contract tests fit if the provider is a third party you cannot influence?
Related questions
- A test passes on its own and fails when the whole suite runs. How do you track that down?mediumAlso on testing and nodejs5 min
- Your consumer-driven contract test passes in CI, but production rejects a request because a supposedly optional field is missing. What did the contract testing actually miss?hardAlso on contract-testing4 min
- A promise rejects and nothing is awaiting it. What does Node do?hardAlso on nodejs4 min
- Your logs are full of Cannot set headers after they are sent. Walk me through how a request gets into that state.mediumAlso on nodejs4 min