I have a service and sometimes I need to call an external API. To make this call easier to write, I created an httpService. Currently I only have a post method that receives the url and data.
import axios from 'axios' const httpClient = axios.create({ headers: { common: { 'Content-Type': 'application/json' } } }) export async function post(url, data) { try { const response = await httpClient.post(url, data) return response.data } catch (error) { console.log(error) throw new Error('Error in POST request') } }
I need to write some tests for this code using jest, but since my url is generic, I want to pass a url like "http://test.com/api" and fake a positive result. How can I do this?
You can use axios-mock-adapter package:
For example