I am writing unit tests for a function which calls another function inside the same module.
Eg.
export function add(a, b) {
return a + b
}
export function showMessage(a, b) {
let sum = add(a, b)
return `The sum is ${sum}`
}
Test:
import * as Logics from './logics;
describe('showMessage', () => {
it('should return message with sum', () => {
let addSpy = jest.spyOn(Logics, 'add')
let showMessageResponse = Logics.showMessage(2, 2)
expect(addSpy).toHaveBeenCalledTimes(1)
});
});
I want to test if the add function is being called when showMessage is executed. The above one is giving the following error:
Expected number of calls: 1 Received number of calls: 0
I have found a solution but that requires changes in the way functions are exported:
function add(a, b) {
return a + b
}
function showMessage(a, b) {
const sum = Logics.add(a, b)
return `The sum is ${sum}`
}
const Logics = {
showMessage,
add
}
export default Logics
I do not want to change the way I am exporting the functions.