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

118
Vistas
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 Respuestas
Responde la pregunta

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 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