export function onClickTest() {
fakeItem();
}
export function fakeItem() {
console.log("fakeItem called");
}
describe("HaveBeenCalled test!!", () => {
test("test", () => {
const fakeItemSpy = jest.spyOn(fakescript, "fakeItem");
script.onClickTest();
expect(fakeItemSpy).toHaveBeenCalled();
fakeItemSpy.mockRestore();
});
});
In the script.js file, the onClickTest function calls the fakeItem function from the same module. However, the test case fails, saying that the call to fakeItem was not detected. Despite checking thoroughly, I can’t figure out why this is happening. Has anyone experienced a similar issue and found a solution? Any feedback would be appreciated.
Expected number of calls: >= 1
Received number of calls: 0
I was able to resolve fakeItem() by moving to a different module for testing, but is there a way to achieve success in testing without separating the module and keeping the original intact?
2