Sobes.tech
Middle

Have you ever had to mock API data? How did you do it?

sobes.tech AI

Answer from AI

Yes, it was necessary to mock API data to isolate the frontend from the real backend during development or testing. This is usually done in several ways:

  • Using mocking libraries, such as msw (Mock Service Worker), which intercept network requests and return predefined responses.
  • Creating local JSON files with test data and configuring proxies or URL replacements to load this data.
  • Built-in features of frameworks, for example, Angular provides HttpClientTestingModule for mocking HTTP requests.

Example using msw:

import { setupWorker, rest } from 'msw';

const worker = setupWorker(
  rest.get('/api/user', (req, res, ctx) => {
    return res(ctx.json({ id: 1, name: 'Ivan' }));
  })
);

worker.start();

This approach allows the frontend to work with predictable data without depending on the actual API.

Have you ever had to mock API data? How did you do… - sobes.tech