How do I mock/spyOn the value of a function returned after calling another function? In the following example I would like to mock the resolved value for the 'get' method of api.get() but I've been having difficulty getting at that value with a jest mock or spyOn. The issue is that the get method is returned after the function is called as part of axiosInstanceWrapper({}).
axios-instance-wrapper.js:
...
const axiosInstanceWrapper = function(params) {
...
const instance = axios.create(otherInstanceParams);
...
return instance;
}
module.exports = axiosInstanceWrapper;
code:
import axiosInstanceWrapper from './axios-instance-wrapper';
const api = axiosInstanceWrapper({});
const result = api.get(MY_URL, myParams); <-- I want to mock this response
test file:
import axiosInstanceWrapper from './axios-instance-wrapper';
jest.mock('./axios-instance-wrapper', () => {
return () => ({
default: jest.fn().mockReturnValue({
get: jest.fn()
})
});
});
...
test('', () => {
const spy = jest.spyOn(axiosInstanceWrapper, 'get').mockResolvedValue({
data: {
customer: {
subscriptionContracts: [],
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
}
}
}
});
});
Error I get on that line:
Error: Cannot spy the get property because it is not a function; undefined given instead
Other things I've tried:
const spy = jest.spyOn(axiosInstanceWrapper.default, 'get').mockResolvedValue({
...
});