Mock Server
ELI5 — The Vibe Check
A Mock Server pretends to be a real API. It returns predefined responses so your tests don't depend on external services. The payment API is down? Your tests still pass because the mock server responds exactly how you told it to. No flakes from third-party outages.
Real Talk
Mock servers simulate external service APIs for testing in isolation. Tools like MSW (Mock Service Worker), WireMock, and Prism intercept HTTP requests and return configurable responses. MSW intercepts at the network level, while others run as standalone servers. Supports response delays, errors, and stateful scenarios.
Show Me The Code
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer(
http.get('/api/users', () => {
return HttpResponse.json([
{ id: 1, name: 'Alice' }
]);
})
);
beforeAll(() => server.listen());
afterAll(() => server.close());
When You'll Hear This
"MSW mocks our API at the network level — components don't know they're hitting a fake." / "The mock server returns a 500 error for our error handling tests."
Related Terms
API Testing Patterns
API Testing Patterns are strategies for testing your APIs thoroughly: happy path, error responses, validation, auth, pagination, rate limiting.
MSW
MSW (Mock Service Worker) intercepts your API calls at the network level and returns fake data, like a polite man-in-the-middle attack on yourself.
Stub
A stub is like a cardboard cutout of a function. It stands in for the real thing and always gives you the same canned response.
Test Isolation
Test Isolation means each test is completely independent — it sets up its own data, runs in its own world, and cleans up after itself.