← Blog/Mocking

Build the frontend before the backend exists

Build a frontend against a mock API that behaves like the real one: path parameters, realistic fake data, empty and error states on demand, and slow responses.

The new orders page is designed. The API it needs is agreed on paper: GET /orders, GET /orders/:id, a POST to cancel. The backend team will build it, two sprints from now. Until then the frontend team has three options, and two of them are bad.

They can wait, which wastes two sprints. They can hard-code JSON into the components, which works until the day they have to tear it all out, and never shows what happens on an error. Or they can build against an API that behaves like the real one will: a real URL, real HTTP status codes, realistic data, slow responses and failures on demand. That is what a mock server is for.

This post walks through building the frontend for that orders page against a Mercur mock server, before a line of the backend exists.

1. Agree on the contract, then mock it

Start from what both teams agreed: the routes, the fields, the error shapes. In the Mercur console, open Mock Servers and create a server. It gets its own public address right away:

Your mock server
https://mk-xxxxxxxx-xxxxxx.mercur.sh

Add a route for each endpoint of the contract. Paths take parameters, so one route covers every order:

  • GET /orders answers the list.
  • GET /orders/:id answers one order; :id captures whatever the client sent.
  • POST /orders/:id/cancel answers the cancellation.

Point the frontend's API base URL at the mock server and the app is talking HTTP to something real from day one. Turn on Allow cross-origin requests (CORS) in the server's settings (it is on by default), and the browser calls it directly.

2. Make the data look real

A list of three identical orders hides layout bugs. A response body can be a template that generates data on every request:

{
  "id": "{{request.params.id}}",
  "customer": "{{faker.person.fullName}}",
  "email": "{{faker.internet.email}}",
  "total": "{{faker.number.int(10, 900)}}",
  "createdAt": "{{faker.date.recent}}"
}

The order you ask for comes back with the id you asked for, a different customer each time, and a total that is sometimes two digits and sometimes three. Long names, short names, odd totals: the page meets them in development instead of in production. The $repeat helper turns one item into a list of random length, so the list page also sees one order, five orders and every count in between.

3. Every state, on demand

The states that break a frontend are the ones nobody sees during development: the empty list, the validation error, the expired session. A route can hold up to 20 responses, and conditions decide which one answers, based on what the request carried:

  • Empty list: when the query string has status=empty, answer 200 with [].
  • Not signed in: when the authorization header does not exist, answer 401 with the error shape the contract defines.
  • Not found: when the path parameter id equals missing, answer 404.
  • Default: a response with no conditions, kept last, answers everything else.

Now every state of the page is one URL away:

The empty state
curl 'https://mk-xxxxxxxx-xxxxxx.mercur.sh/orders?status=empty'

A designer can review the empty state, a QA engineer can test the 401 redirect, and a storybook story can point at a specific case, all without touching the code.

4. Slow and flaky, on purpose

Loading spinners and skeleton screens are easy to forget because local APIs answer right away. Give a response a delay, up to 10 seconds, and the page has to show what it does while it waits. Add a condition on a header such as x-scenario: slow and only the requests that ask for it are slow, so the rest of the app stays fast.

Do the same for failures: a 503 response behind x-scenario: down shows whether the page retries, shows an error, or quietly renders nothing.

5. Share it

Because the mock server has a public URL, it is useful beyond the frontend team:

  • CI runs the end-to-end tests against it, with predictable answers and no shared test database.
  • A partner building an integration can start against the contract while the real API is still being written.
  • A demo on a staging site works without the backend being deployed.

The Requests tab shows every call the mock answered and which route and response matched, so when the frontend sends something unexpected you see it immediately.

6. When the real API ships

Switch the frontend's base URL to the real API. If the backend matched the contract, nothing else changes; if it did not, you find out on the first request, with the mock's version of the contract to compare against.

Keep the mock server: turn off its Running switch to pause it, and turn it back on for the next feature, for tests that need an error the real API cannot easily produce, or for the next partner. If the real API is already running somewhere you can reach through a Mercur tunnel, you can also go the other way: capture a real request and response, and turn it into a mock route in one click.

Get started

Mock servers are available on Free, Solo and Team.

Create a mock server

The full reference, with every condition, template helper and limit, is in Mock an API.

mockingapi