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

373
Visualizações
How do I Jest unit test an async method that catches an error and then rethrows it?

I have a Class module that I'm trying to unit test with Jest. Specifically an async method. I want to test a branch in the method where an error is thrown in a try, caught in the catch, and then rethrown from the catch. Here's a slimmed down example of the structure.

myClass.js

const helper = require("helper");

module.exports = class myClass {
  constructor() {
    ...
  }

  async myMethod() {
    try {
      const response = await helper.getSomeObject();

      if (response.someArray.length == 0)
        throw new Error("Whoops!");

      return response;
    }
    catch (err) {
      ...
      throw err;
    }
  }
}

myClass.test.js (Attempt #1)

const myClass = require("myClass");
const helper = require("helper");

describe("Testing myClass", () => {
  test("myMethod should throw error", async () => {
    helper.getSomeObject = jest.fn().mockResolvedValueOnce({someArray: []});

    const mc = new myClass();
    const response = await mc.myMethod();

    expect(response).rejects.toEqual(Error("Whoops!"));
  });
});

myClass.test.js (Attempt #2)

const myClass = require("myClass");
const helper = require("helper");

describe("Testing myClass", () => {
  test("myMethod should throw error", async () => {
    helper.getSomeObject = jest.fn().mockResolvedValueOnce({someArray: []});

    const mc = new myClass();
    await mc.myMethod();

    expect(async () => await mc.MyMethod()).toThrow("Whoops!");
  });
});

Obviously, I don't know what I'm doing. In either attempt, the unit test fails, but it doesn't seem to fail when checking the expect statement. It seems like the expect statement isn't even being called. All I'm seeing in the output is the error message "Whoops!" and that's it.

I'd greatly appreciate any help. Thank you!

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

0

Figured it out! I got it to work with the following.

myClass.test.js (Attempt #3)

const myClass = require("myClass");
const helper = require("helper");

describe("Testing myClass", () => {
  test("myMethod should throw error", async () => {
    helper.getSomeObject = jest.fn().mockResolvedValueOnce({someArray: []});

    try {
      const mc = new myClass();
      await mc.myMethod();
    }
    catch (err) {
      expect(err).toEqual(Error("Whoops!"));
    }
  });
});
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