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

245
Visualizações
How to use Jest .mockImplementation to both throw and error and return status

I'm trying to test the catch block of this try/catch.

  try {
    const response = await request.get({
        // ... required params here
    });

    return response.data;
  } catch (error) {

    myFunction({
      statusCode: error.response.status,
    });

    throw error;
  }

My aim is to test the call of myFunction inside the catch. I want to confirm that the function was called with the correct parameters of { statusCode: 400 } for example.

I also want to test that an error was thrown.

I've written a mockImplementation of request.get in an attempt to get this error response, but it does not seem to call into my catch block.

it("should throw an error and call myFunction with the status code", async () => {
  await request.get.mockImplementation(() => {
    throw new Error();
    // how do I add return of statusCode here?
  });

  expect(addToNewRelic).toHaveBeenCalledWith(
    expect.objectContaining({
      productAPIRequestStatusCode: 404,
    })
  );
});
about 4 years ago · Juan Pablo Isaza
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