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

145
Visualizações
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 à 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