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

117
Visualizações
Async Await with Mocha + Axios TrobuleShooting

we are using mocha + nodejs + axios

Below is the piece of code i have , they are all in different file.

File 1 : IT block

   it('Perform Post Payment Actions', function () {
    postPaymentController.triggerPostPaymentActions(params);
  });

File 2 : Controller

module.exports.triggerPostPaymentActions = function (params) {
  const actions = params.postAuthActionId.split('-');  // // 'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL

  Object.entries(actions).forEach(([, action]) => {
    const actionAsArray = action.split(','); // 'PARTIALREFUND, 50, SUCCESS 

    switch (actionAsArray[0]) {. // PARTIALREFUND
      case 'PARTIALREFUND':
        paymentsPartialRefundApi.submitPaymentsPartialRefundApi(actionAsArray, params);
        break;
      default:
        break;
    }
  });
};

File 3: Acutal API Call using axios


module.exports.submitPaymentsPartialRefundApi = async function (actionDetails, params) {

  const postOptions = {
    url: partialRefundApiUri,
    method: 'POST',
    headers: restHeaders,
    data: params.partialRefundRequest,
  };

  try {
    const response = await axios(postOptions);
    params.partialRefundResponse = response.data;
    process.env.PARTIALREFUNDRESPONSE = JSON.stringify(params.partialRefundResponse);
  } catch (err) {
    process.env.ERRORCODE = err;
    process.env.SCRIPT_STATUS = 'failure';
    process.env.PARTIALREFUNDRESPONSE = 'Partial Refund request Failed';
    throw err;
  }



};

PROBLEM :

When the control is passed to submitPaymentsPartialRefundApi , then it builds the request for axios but rather than waiting for the axios call , it takes the control back to the switch case. my expectation was that since i am using async await it will execute and wait till the axios call is completed but rather than completing the call it returns the control back to the switch statement and from the switch it goes back to the IT block. so i have 2 problems. 1 Its not waiting for the axios call to end 2 Its not running switch twice in loop , its goes back to IT block and then comes back.

Solutions Tried :

I already tried using promises for this , with and without timeout but still it did not worked. Any help on this is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

submitPaymentsPartialRefundApi is an asynchronous function but you are not awaiting its completion in your controller.

Your code is very weird and I don't understand what it is supposed to do as you don't return anything from your controller function and your test script does not actually test anything. That being said, your controller needs to do something like the following:

module.exports.triggerPostPaymentActions = function (params) {
  const actions = params.postAuthActionId.split('-');  // // 'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL
  
  return Promise.all(Object.entries(actions).map(([, action]) => {
    const actionAsArray = action.split(',');

    switch (actionAsArray[0]) {
      case 'PARTIALREFUND':
        return paymentsPartialRefundApi.submitPaymentsPartialRefundApi(actionAsArray, params);
        break;
      default:
        // return something here or use Array.filter before Array.map
        break;
    }
  }));
}

Then your it block should test that the returned promise gets resolved or something...

it('Perform Post Payment Actions', function () {
    return expectAsync(postPaymentController.triggerPostPaymentActions(params)).toBeResolved();
});
about 4 years ago · Juan Pablo Isaza Relatório
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