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

142
Vistas
Proxyquire with HystrixJS, how to mock the hystrix call?

NodeJS / Express

I have a method inside a controller that call a external API through a POST service. This method uses HystrixJS between then to make the call.

request.js

...
const post = (url, req, res, xsd, body = {}) => {
    // ... simple execute the post method and return a promisse
    return client.postPromise(url, options).then((response) => (response));
};
...

controller.js

const { post } = require('./services/request');

function Xpto(req, res, xsd) {
  
  let url = `http://host.io/post-method`;
  
  const request = req.app.get('hystrix').hystrixRequestHandler(post, 'blablabla');

  const inRequest = req.body;

  const inResponse = request.execute(
    url,
    req,
    res,
    xsd,
    inRequest,
  );

  return inResponse;

}

// -----------------------------------

function handler(req, res, next) {

    const response = await Xpto(
      req,
      res,
      xsd
    );

    console.log('================>', response);

}

Testing the controller.js mocking services/request to avoid a live API call.

controller.test.js

...
it('should return success', async () => {
  mock01 = proxyquire("./controller", {
  "./services/request": {
    post: () =>
      Promise.resolve({
        response: { statusCode: 200 },
        data: {
          xxx: "00000",
        },
      }),
  },
  });

  const response = await mock01.handler(request, res, next);
  const data = response.body[0].data;

  expect(response.statusCode).to.equal(200);
});

...


it('should return error', async () => {
  mock02 = proxyquire("./controller", {
  "./services/request": {
    post: () =>
      Promise.resolve({
        response: { statusCode: 500 },
        data: {
          zzzz: "99999",
        },
      }),
  },
  });

  const response = await mock02.handler(request, res, next);
  const data = response.body[0].data;

  expect(response.statusCode).to.equal(500);
});

Everything works fine except due the Hystrix cache it always return statusCode = 200 even in the mock2 where I explicit define it to return statusCode = 500.

 AssertionError: expected 200 to equal 500
      + expected - actual

      -200
      +500

I looking to find a way to mock the Hystrix call const request = req.app.get('hystrix').hystrixRequestHandler(post, 'blablabla'); or another way that avoid this behavior.

about 4 years ago · Juan Pablo Isaza
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