Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

340
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda