Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!