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

140
Views
Jest check if function was called

I'm using Jest to test some utility functions.

my_util.js:

export function myFunc(i) {
  if (i === 1){
    anotherFunc();
  }
}

another_util.js:

export function anotherFunc(i) {
    console.log('in anotherFunc');
}

What is the simplest way to test that anotherFunc() was called? Here is my current unit test, which is failing. Is there a way to test that a function was called by it's name?

import { myFunc } from './my_util.js'

...

  it('myFunc should call anotherFunc', () => {
    const anotherFunc = jest.fn();
    myFunc(1);
    expect(anotherFunc).toHaveBeenCalled();  
  });

Results:

Expected number of calls: >= 1
Received number of calls:    0
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Maybe you should just inject anotherFunc to myFunc as argument, it will make testing easier:

function myFunc(i, cb) {
  if (i === 1){
    cb();
  }
}
  it('myFunc should call anotherFunc', () => {
    const anotherFunc = jest.fn();
    myFunc(1, anotherFunc);
    expect(anotherFunc).toHaveBeenCalled();  
  });
about 4 years ago · Juan Pablo Isaza Report
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!