I'm using jest to test code in a nodejs server. I'm trying to figure out the best way to write the code for integration tests. It seems like jest's mock function necessitates that any sub-function that a function uses should be passed in as a callback.
For example:
async function apiFunction(){
return await apiCall();
};
async function subFunction1(){
return await apiFunction();
};
mainFunction(){
return await subFunction1();
};
Would be better written as:
async function apiFunction(){
return await apiCall();
};
async function subFunction1(apiFunctionAsCallback){ // all sub-functions passed as a callback
return await apiFunctionAsCallback();
};
mainFunction(){
return await subFunction1(apiFunction); // passed as a callback
};
Is this generally a better practice for all code? Only I've not seen this pattern much previously and I'm wondering if there are alternative testing packages to jest that handle this differently?