I have a customized Axios instance with multiple rules, including interceptors, which I don't want to test directly. I'm looking to use MSW (Mock Service Worker) to mock API responses and replace my complex Axios instance with a simplified one for testing purposes.
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import axios from 'axios';
import { myApi } from '~/services/api';
const mockedApi = axios.create({ baseURL: 'http://localhost:3000' });
const server = setupServer(
http.get('http://localhost:3000/test', () => {
return HttpResponse.json({ id: '1' });
}),
);
jest.mock('~/services/api', () => {
return {
myApi: mockedApi,
};
});
describe('useVerifyIfPdvStillAvalaible Hook', () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
it('its thorw message: Cannot read properties of undefined (reading 'get')', async () => {
const response = await beepayApi.get('/test');
});
});