r/reactjs • • 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:

  1. Hardcode mocked data in React component state or custom hooks (messy and leaks into production).
  2. 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:

  1. Tests Real HTTP Errors: If your form submits an invalid payload, Spec-Forge returns an actual RFC 7807 422 Unprocessable Entity matching the OpenAPI schema. You can build and test your form error banners, field-level validation messages, and disabled button logic against realistic server responses.
  2. Stateful Mutations: When you trigger a useMutation that POSTs new data and calls queryClient.invalidateQueries(), the subsequent query actually reflects the newly created item.
  3. Zero Frontend Code Contamination: Your React app talks to http://localhost:8088 as if it's the real backend. When the backend is ready, you only change the .env VITE_API_URL or NEXT_PUBLIC_API_URL.

How are you currently handling contract mocking in your React projects?

0 Upvotes

21 comments sorted by

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.

6

u/silverShower Oct 09 '26

I'm amazed that there's another way besides this. Whatever you use to mock data, you must first agree on the model. And then you can do anything to mock it, who cares. Just hardcode everything for testing. How does anything leak into prod? Do people look what they commit? Do they use .gitignore if they tend to overengineer stuff?

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.

3

u/Merry-Lane Oct 09 '26 edited Oct 10 '26

Ideally, your backend guys generate a swagger.json and you use something like Orval.js to generate your api endpoints, hooks/tanstack stuff/services/…, zod validation and types.

Best is the backend generates a swagger.json in the pipelines and passes it to the frontend that runs the Orval script then tsc/linter so that any unintended API change fails the step of the pipeline.

8

u/TonyAioli Oct 09 '26

Kinda reinventing the wheel here. 

https://mswjs.io/

1

u/Outrageous-Chip-3961 Oct 10 '26

Yeah... this is like the defacto industry standard for mocking in the front-end but people are mentioning orval, which is new to me.

5

u/Squigglificated Oct 09 '26

I use Orval

1

u/eddo_92 Oct 09 '26

Same here! It has been so helpful for typings and mocks

1

u/Outrageous-Chip-3961 Oct 10 '26

will check it out. been mentioned a bit in this post but i normally just hard code with MSW. How does this differ?

2

u/[deleted] Oct 09 '26

[removed] — view removed comment

1

u/Squigglificated Oct 09 '26

It's worked well for me. I use AI with Orval to generate anything from simple test mocks to fully mocked complete backends. The schema, types, generated MSW handlers and mock function forces ai to strictly follow what the backend actually can deliver.

I agree that configuring all the mocking manually was a bit tedious even with the generated orval helpers.

10

u/Greedy-Dependent-826 Oct 09 '26

Hey Claude, mock my API.

0

u/Noch_ein_Kamel Oct 09 '26

Yeah, I just told ai to build me an API mocking tool where I can just drop in open I specs and it serves them with validation and shit. Even created a nice dashboard to force response codes and delays

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/kettanaito Oct 12 '26

You can learn more about OpenAPI -> handlers here: https://mswjs.io/ecosystem/source/integrations/open-api
Let me know if you have any questions!

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.