I have an application which makes call to a third party API using AXIOS. But instead of making actual third party call I would like to mock the 3rd party axios call based on some condition and provide my dummy response i.e, override the axios.post() method to provide my own implementation and provide success response.
Below is my code block where I would not want to pass the actual baseURL or if passed would like to call my override post() method and the dummy success response and not call the 3rd party endpoint. I want to do this in my code and not the Tests.
const axiosConfig = {
baseURL: 'https://someURL.com',
timeout: 60000, // 60 seconds
responseType: 'json',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
const myAxiosInstance = axios.create(axiosConfig);
const response = await myAxiosInstance.post('/somepath/to/the/resuource', somepayload);
How can I achieve this ?
Try this library: axios-mock-adapter