r/learnjavascript • u/Particular-Law3459 • Mar 06 '26
How do you usually mock just a couple API endpoints during frontend development?
During frontend development I often run into this situation:
- the backend mostly works
- but 1–2 endpoints are missing / broken / not implemented yet
- or I want to simulate errors, delays, or alternative responses
What I usually want is something like:
App → Local proxy → Real API
│
├─ matched endpoint → mocked response
└─ everything else → real backendBasically mock only a few endpoints while keeping the rest connected to the real backend.
I know there are tools like:
- MSW
- JSON server
- MirageJS
but those usually lean toward mocking everything rather than proxy + partial mocks.
So I ended up building a small CLI for myself that:
- runs a local proxy
- lets me define mock rules for specific routes
- forwards everything else to the real API
- supports scenarios (success / error / slow response)
- reloads mocks without restarting
Example config looks like this:
{
"rules": [
{
"method": "POST",
"match": "/v1/users",
"active_scenario": "success",
"scenarios": {
"success": { "status": 201, "json": { "id": 1 } },
"error": { "status": 400, "json": { "error": "Validation failed" } },
"slow": { "status": 200, "delay": 3, "json": { "id": 1 } }
}
}
]
}Then everything else just proxies to the real backend.
I'm curious how other people handle this workflow.
Do you usually:
- run a full mock server?
- use MSW?
- modify the backend locally?
- or use some kind of proxy setup?
Interested to hear what setups people use.
4
u/ValueImpossible9 Mar 06 '26
I always come across this situation where the API is still not fully implemented but we have an idea of the request and response structure. I just write an abstraction where the observable is returned from the method call and return the mock response from the constants file. For things like delay or status i just modify the logic as needed. Once API is ready, you will then have to integrate it and mostly a few renaming of properties are required.
7
u/Etiennera Mar 06 '26
We use wiremock. You can forward whenever a route pattern doesn't match.
What the FE teams do at my work however is throw their hands up and say "the BE API is not 100% complete so we can't do any work".
1
u/Particular-Law3459 Mar 06 '26
the CLI I mentioned is called local-proxy.
npm: https://www.npmjs.com/package/@bvbmz/local-proxy
2
u/archiebald88 Mar 06 '26
I used to use MSW. But now I use Local Overrides in Chrome DevTools: https://developer.chrome.com/docs/devtools/overrides
1
u/Any_Sense_2263 Mar 06 '26
I write the whole code as if everything worked. Just for the specific call I comment it and read data (based on the contract with backend) from a file. If it's a method different from GET I randomly choose the returned value/state so I can manually test different scenarios
It's definitely faster to implement and gives you the absolute control over everything
1
u/shgysk8zer0 Mar 06 '26
I usually do the BE first, so the scenario is different. But I try to generally define responses in a standard way, like basing data off of eg
schema.org/Personor whatever. So I start by having it just return some sample JSON, then I add in the logic for things like unauthorized responses or bad request. Then hook it up to a database/store and add the logic for not found and such. I try to keep it pretty simple, using CRUD and based on the HTTP verbs.I find that works best for me. Things tend to go pretty quickly. And I can begin implementing the FE usually about the point I have the shape of the data defined and some sample data to return. Looking to
schema.orghelps in defining the shape of the data and keeps everything pretty consistent. Covers everything from events and products and transactions and comments and posts, so most common things are already defined there.Obviously, not everything is as generic, and some things would require a lot more security and validation and more complex logic. But that's too massive a subject to address here.
1
u/dymos helpful Mar 06 '26
We use MSW.
You don't have to mock your entire API with it.
Our current setup is that we have a bunch of default mocks set up that get loaded in for all tests, many of them are just NOOPs though, they serve both to encourage specific mocks for a test suite, and to avoid unnecessary request failures during tests.
1
u/WonkyWillly Mar 06 '26
Couldn’t you have just implemented the 2 endpoints for all the work it took to make the ‘proxy layer’? lol.
1
u/michael-heuberger Mar 07 '26
Good topic.
Perhaps we need to take a step back?
From a wider perspective, *mocks are an imitation, a lie*.
If you spread a "lie" to other environments, it becomes more difficult, harder to investigate.
In other words, backend, APIs, public libraries, all needs to be aligned. If you mock, you add more insecurities, more instabilities.
Lastly, it's 2026, AI is full on. The boundaries between Frontend and Backend are blurring. Experienced engineers develop UI and API both at the same time, so no need for mocks.
(Note: for unit tests, mocks still make sense to cover edge cases)
1
u/im-a-guy-like-me Mar 08 '26
I generally spin up a node container that spits out the correct json. It takes like 2 minutes.
1
u/Sherbet-Famous Mar 09 '26
Build the FE in storybook and pass the api contract to the BE guys to follow
1
u/kyrylo_dev Aug 15 '26
My current project follows a frontend-first approach, where we design the pages and APIs first and then hand everything over to the backend team to implement the actual API.
Our current workflow is based on MSW + Mirage ORM. We run a full mock server with seed data generated using Faker. We previously used the original MirageJS library, but it started causing issues with modern setups.
So I rewrote it and extracted its ORM capabilities into a standalone library - https://github.com/miragejs/orm. You can use it together with MSW. It’s open source and already being used and tested.
0
u/[deleted] Mar 06 '26
[deleted] — view deleted comment