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

342
Visualizações
How to test a function that returns an arrow function with Jest?

I have a function like this:

export const myFunc = (a, b ,callBack) => {
    const timer = setTimeout(() => {
        callBack(a+b);
    }, 10);
    return () => clearTimeout(timer);
};

I'm able to test the callback function just fine by doing this:

jest.useFakeTimers();

describe('myFunc', () => {
    const callBack = jest.fn();

    it('runs myFunc', () => {
        myFunc(1, 2, callBack);
        jest.runAllTimers();

        expect(callBack).toHaveBeenCalledWith(3);
    });

However, the last line return () => clearTimeout(timer) is never tested somehow. Jest report just says this line function not covered and statement not covered. Can anyone please tell me a way to cover this line without using any other external testing library like enzyme?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The function isn't called, so it's not covered.

Another test would be:

    let cleanup = myFunc(1, 2, callBack);
    cleanup()
    jest.runAllTimers();
    expect(callBack).not.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