r/reactjs • u/gilbertlol • Oct 09 '26
How do you mock API calls while frontend and backend are developed in parallel? (React Query / SWR / Axios)
Hey React devs,
A recurring headache when building complex dashboards or forms with React Query / SWR / Axios is testing error states and validation feedback before the backend endpoints exist.
Usually we either:
- Hardcode mocked data in React component state or custom hooks (messy and leaks into production).
- Set up MSW (Mock Service Worker). MSW is great, but maintaining mock handlers by hand for 30+ endpoints gets tedious and doesn't validate payload schemas against the backend contract.
We built a lightweight OpenAPI mock server called Spec-Forge:
bashpip install spec-forge
spec-forge serve openapi.json --port 8088
Why this has been a game changer for our React development:
- Tests Real HTTP Errors: If your form submits an invalid payload, Spec-Forge returns an actual RFC 7807
422 Unprocessable Entitymatching the OpenAPI schema. You can build and test your form error banners, field-level validation messages, and disabled button logic against realistic server responses. - Stateful Mutations: When you trigger a
useMutationthat POSTs new data and callsqueryClient.invalidateQueries(), the subsequent query actually reflects the newly created item. - Zero Frontend Code Contamination: Your React app talks to
http://localhost:8088as if it's the real backend. When the backend is ready, you only change the.envVITE_API_URLorNEXT_PUBLIC_API_URL.
- Web Playground: https://spec-forge.dev2share.com
- 1-Min Demo: https://spec-forge.dev2share.com/demo.html
- Open Source Repo: https://github.com/gilbertlol/spec-forge
How are you currently handling contract mocking in your React projects?
0
Upvotes
14
u/zaibuf Oct 09 '26
Well, I'm fullstack but when I'm not doing the backend I just talk with the devs to agree on a contract. Then they just mock it at the API layer so we can start integrating with the real endpoint, even if it's just bogus data. Works very well and backend doesn't need to feel stressed that clients are waiting.
I find this simpler than needing to mock or take guesses in frontend that I need to refactor later.
8
u/Outrageous-Chip-3961 Oct 09 '26
I mean your post seems like an ad. But I'm pretty sure its best to just use MSW with tanstack query. It's seamless. You just toggle 'mock api' and MSW will hijack your apps natural requests then send back fake payload. Ideally, you'd want to agree on your API contracts with your team (even if loosely) and then communicate around that if it changes. Once you have the API contracts you can put them into AI and ask for mock data sets with types (or if you have the api contracted typed, you can generate types from it) then ask for data shapes. (or you could just make up fake stuff if you dont want to use AI, but it's pretty handy to just generate fake objects as you get a much wider spread than normal) Then just put that data into your front-end mocks while the back-end go and develop against the api spec.
8
u/TonyAioli Oct 09 '26
Kinda reinventing the wheel here.
https://mswjs.io/
5
u/Squigglificated Oct 09 '26
I use Orval
10
u/Greedy-Dependent-826 Oct 09 '26
Hey Claude, mock my API.
1
u/DARKDYNAMO Oct 27 '26
I built a tool for that. As a full stack dev I don't really want to spend time on building mock server. What I need a Api contract from backend dev ( or decided my me if I have backed task too) once I know which api returns what kind of json data I paste the api endpoint and json in my app turn on toggle in browser extension and UI gets what's needed.
I actually used to build simple mock dynamic backeneds in js scripts for testing turns out I was wasting my time on it cause that time could have spent on actual work.
If anyone wants to use it its called Bifurc
1
u/[deleted] Oct 09 '26
I built test-proxy-recorder package for that, just a proxy that can record api during the test run and replay it on CI/CD without backend, works with SSR like Next.js as well.
1
u/kettanaito Oct 12 '26
If you already have an API spec, use _that_ as the source of truth for MSW handlers. Don't write them by hand. Look at `@msw/source` to generate handlers from OpenAPI specs. This will make your experience with handlers much simpler. MSW already does all the rest, allowing you to test virtually anything.
1
u/arjun2504 Oct 17 '26
I use Mittelware, a desktop tool that intercepts HTTP traffic and can fail or modify requests without code changes. It's a different layer from a mock server, so it's more for testing real apps against errors than for building against a missing backend.