Skip to content

Mock Server

Medium — good to knowTesting

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.