I want to mock a function post from an object. I did it like this:
jest.mock('../../apis.js', () => {
return () => {
return {
post: jest.fn(() => {
console.log('POST was called !');
return Promise.resolve();
}),
};
};
});
Then, I make my require:
const apis = require('../../apis');
Finally I call my service which will call the post() function from apis module:
client.doPost()
.then(() => {
console.log('count: ', apis().post.mock.calls.length);
expect((apis().post)).toBeCalled();
});
The doPost() function calls the mocked post() one ('POST was called !' message is printed) but count is equal to 0 and expect fails.