Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

373
Vistas
Mock an axios 404 with Jest?

When mocking a 404 error the result is

Uncaught UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: error".

Setup a basic Mock

const mockAccountEnquiry = () => {
  axios.post.mockImplementation(async (url, testParams) => {
    if (url === 'https://blah/v1/payIDEnquiry') {
      if (testParams.payID === 'not-found') {
        jest.fn().mockReturnValue(Promise.reject(Error('error')));
      }
      return {
        status: 200,
        data: {
          AccountStatus: {
            nppReachable: testParams.nppReachable,
          },
        },
      };
    }
  });
};

Create a test case

it('Failed request', async() => {
  mockAccountEnquiry();
  const response = await backend.handler(testData);
  expect(axios.post.mock.calls[0][0]).toBe(enquiryUrl);
});

How to mock the 404 request?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Using https://www.npmjs.com/package/axios-mock-adapter will make mocking API very simple.

var axios = require("axios");
var MockAdapter = require("axios-mock-adapter");

var mock = new MockAdapter(axios);

mock.onPost(enquiryUrl).reply(404);

it('Failed request', async() => {
  const response = await backend.handler(testData);
  expect(mock.history.post[0].url).toBe(enquiryUrl);
});
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda