Mock with a successful response
import axios from "axios";
jest.mock("axios");
const mockedAxios = axios as jest.Mocked<typeof axios="">;
const mockGet = jest.fn();
mockGet.mockImplementation(() => {
const myResponse = {
status: 200,
data: {
data: [{ id: 1 }, { id: 2 }],
},
};
return Promise.resolve(myResponse);
});
mockedAxios.get.mockImplementation(mockGet);
</typeof>
Mock with an error response
import axios from "axios";
jest.mock("axios");
const mockedAxios = axios as jest.Mocked<typeof axios="">;
const mockGet = jest.fn();
mockGet.mockImplementation(() => {
const error = {
response: {
status: 404,
},
};
return Promise.reject(error);
});
mockedAxios.get.mockImplementation(mockGet);
</typeof>
Photo by Green Chameleon on Unsplash