Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!