Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Mock the inner function in Jest

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!