Test file:
const webhook = require('../../../../../../src/lib/apis/webhook');
const internalFunc = jest.spyOn(webhook, 'handleWebhookCreation').mockImplementation(() => {
return expectedReturn;
});
File being tested:
const { handleWebhookCreation } = require('../../../apis/webhook');
const createWebhook = async (req, res) => {
for (const webhook of payload) {
results.push(await handleWebhookCreation(webhook, t));
}
};
This does not work. The normal function is called, not a mocked/spied version
I then found out, this fixes it, instead of destructuring the method, get entire object then call it as such with the following changes
const _webhook = require('../../../apis/webhook');
...
results.push(await _webhook.handleWebhookCreation(webhook, t));
Now the spy/mocking is correctly done in my unit test
I have no idea why this is, would anyone know why?