mercredi 27 novembre 2019

How to mock an axios api call to return different values based on input?

Say one has a function that, among other tasks, makes a few api calls using axios. Is there a way, when testing this function, to mock all the axios api calls and specify return values from the calls depending on the input. For example, say the function you want to test is this:

function someFunction (a, b, c) {
    const apiReturnA = axiosApiCall(a)
    const returnB = b + 1
    const apiReturnC = axiosApiCall(c)
    return [apiReturnA, returnB, apiReturnC]
}

I'd like to test someFunction and specify that, every time axiosApiCall gets called, don't execute the function, simply return a value based on the input to this function. How can one do this?

Aucun commentaire:

Enregistrer un commentaire