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

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

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 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