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

92
Vistas
how to correctly import an asynchronous module.exports function in javascript

I have written an asynchronous function in a file called index.js as follows:

module.exports.func = async (obj) => {
    return {
      statusCode: 200,
      body: JSON.stringify({
        message: 'Success!',
        input: obj,
      }),
    };
};

However, when I try to write a test file called index.test.js, with the following code:

const exportedModule = require('./index');

test('Successfully execute func when empty object passed', () => {
  const obj = {};
  const expectedReturn = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Success!',
      input: obj,
    }),
  };
  const actualReturn = await exportedModule.func(obj);
  console.log(actualReturn);
  expect(actualReturn).toBe(expectedReturn);
});

, I get an error saying that exportedModule.func is not asynchronous, even through it clearly is. I cannot use promises (.then and .catch) to run the tests, as the test file will be deemed finished before the code in the .then/.catch is executed. Is there a way I'm supposed to be properly importing func such that I can use await? Ideally I would like this to be done within the test file itself without changing index.js.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I get an error saying that exportedModule.func is not asynchronous

I think you're misreading the error. It's saying that the function in your test is not async. You can only use await in an async function. To fix it, add async to the function.

//                                                 added:  VVVVV
test('Successfully execute func when empty object passed', async () => {
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