1

JS File

export const refreshInfo = () => {
   console.log("Users refreshed")
}
export const function2 = () => {
  refreshInfo();
}

Test File

import * as helper from './helper';

test("Test Case 1", ()=> {
jest.spyOn(helper, 'refreshInfo').mockImplementationOnce(() => console.log("1"));
helper.function2();
expect(helper.refreshInfo).toHaveBeenCalledTimes(1);
}

test("Test Case 2", ()=> {
jest.spyOn(helper, 'refreshInfo').mockImplementationOnce(() => console.log("2"));
helper.function2();
expect(helper.refreshInfo).toHaveBeenCalledTimes(1);
}

I am mocking the refreshInfo function from helper file, to check how many times it is getting called, while testing function2. Second test case is failing, it is giving 2. means refreshInfo is getting called 2 times, and the expected count should be 1. I think it is considering the call that takes place in test case 1 as well while checking for testcase 2. Is there a way to keep both the calls instances separate. Can they be called in any way such that both the test cases remain independent of each other and should return 1 as the answer for both?

Kishan Mishra
  • 129
  • 1
  • 9

0 Answers0