Blog

Where Should Your Mock Actually Live?

September 6, 2026Xerat024 min read
Share
Where Should Your Mock Actually Live?

"Mocking an API" can mean a few genuinely different things depending on where the interception happens, and the differences aren't just implementation detail — they change what you can and can't test, and who else can use the mock besides you.

In-process mocking

The simplest form: your test framework replaces the function or module that would make the network call with a fake one that returns canned data directly, without a real HTTP request happening at all. Jest's jest.mock(), Python's unittest.mock, and similar tools across most languages work this way.

This is fast and requires no network stack at all, which makes it a good fit for unit tests where you specifically want to isolate a small piece of logic from everything around it — testing that a function correctly transforms a response shape doesn't need a real request to prove it, and a mocked function call is the most direct way to remove that variable entirely.

The tradeoff: because no real request ever happens, you're not testing your actual HTTP client configuration, your actual serialization, your actual error handling around a real failed connection, or anything at the network layer. A bug in how you construct headers, or a typo in a URL path, can hide behind a mock that never actually sent anything anywhere.

Network-level interception, in-browser

A layer up: tools like Mock Service Worker (MSW) intercept requests at the network layer inside the browser (using the Service Worker API) or in Node (via lower-level request interception), so your actual fetch or axios call really executes — it just gets intercepted before it leaves the process and answered by the mock instead of hitting the real network. This tests meaningfully more of your real code path than an in-process mock does, while still not requiring any actual server to exist, anywhere.

This is a strong fit for frontend integration and component tests, and for local development against an API that doesn't exist yet, where you want realistic request/response behavior without standing up any real infrastructure. Its limit is scope: because interception happens inside the running browser/Node process, it generally only mocks requests made by that specific app instance. A teammate on a different machine, a CI job, a QA tester using a deployed build, or a mobile app hitting the same backend don't share that mock — each needs its own instance of the interception running locally, if it's even available in that context.

Mocking at the actual network destination

A third approach: instead of intercepting the request before it leaves your process, let the request travel over a real network to a real, separately-running server that's configured to return mock responses — the same way MockBase or a tool like json-server works. Your app doesn't know or care that the "backend" is a mock; it made a completely normal HTTP request to a completely normal URL and got a completely normal response back.

This is the only approach of the three that naturally works across machines and environments without extra setup on each one: a shared mock URL works identically whether it's called from your laptop, a teammate's laptop, a CI pipeline, a staging deployment of the frontend, or a manual QA pass on a device that isn't running your dev tooling at all. It also tests the most of your actual stack — real DNS resolution, a real TLS handshake if you're using HTTPS, real request/response serialization over an actual socket, not a simulated one inside your own process.

The tradeoff is the flip side of that portability: it requires an actual reachable server to exist, which is more setup than "replace this one function" for a single, narrowly-scoped unit test.

Picking one, or several

These aren't mutually exclusive, and most teams end up using more than one depending on the test's scope:

  • Testing that a specific function parses a response correctly, in isolation → in-process mocking is the right amount of tool.
  • Building and testing a frontend component's states (loading, success, error) without a real backend → in-browser interception is a strong, fast default.
  • Anything that needs to be shared across people or environments — a demo, a QA pass, a CI integration test, a mobile client, or frontend development happening genuinely in parallel with backend development on someone else's schedule → a real, separately-hosted mock server, like MockBase, is the one that actually reaches all of those without each person or environment needing its own local interception set up.

The mistake isn't picking any one of these — it's reaching for whichever one you're already familiar with regardless of what the test actually needs, and then working around its limits instead of switching tools when the limits start to matter.

X

Xerat02

Building MockBase.