Two way to mock axios response in jest
📅2023-07-25🧐84
1. To use jest.mock
import axios from 'axios';
jest.mock('axios');
const mockedAxios = axios as jest.MockedFunction<typeof axios>;
const modelResponse = {
data: 'going along',
status: 200,
statusText: 'OK',
headers: {},
config: {}
};
// use
mockedAxios.mockResolvedValue(modelResponse);
2. Using jest-mock-axios
import mockAxios from 'jest-mock-axios';
export const modelResponse = {
data: 'going along',
status: 200,
statusText: 'OK',
headers: {},
config: {}
};
// example use
const resq = post(fileBlob, 'sample1.flac');
mockAxios.mockResponse(modelResponse, mockAxios.lastReqGet());
const result = await resq;