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

339
Views
Jest mocked function is called, but asserting toBeCalled returns false

I am mocking a function:

jest.mock('utils/downloadData')

In utils/download data I created a mock file:

export default {
    general: jest.fn(() => {
        console.log('mock fired')
    }),
    csv: jest.fn(() => {
        console.log('mock fired')
    }),
}

This function is being called inside a function I am testing:

...
const convertToCsv = () => {
        downloadData.csv(CSV.stringify(csvData), fileName)
        onFinish()
    }
...

When debugging I see that the mock function is correctly set in the closure, when debugging step by step I see that the code reaches inside the mocked function and that console logs the message. Yet this test fails:

expect(downloadData.csv).toBeCalledTimes(1)

Jest accepts downloadData.csv inside the expect so I believe it sees the mocked function.

How is it possible that it doesn't register it as being called?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The tested function involved an async data fetching, which was mocked too. But the asynchronous nature of that caused the test to evaluate expected values before it finished running the tested function.

What helped was not just making the test function asynchronous (test('output', async() => {) but also including this line above the evaluation: await new Promise(process.nextTick) This allows the test to finish the asynchronous bits before evaluating the results.

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!