Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

343
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda