I have a javascript application with a file called index.js as follows:
module.exports.func = async (obj) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Success!',
input: obj,
}),
};
};
To test this, I have written another file called index.test.js, which has the following code (it's not yet complete but has the code to illustrate my problem):
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 = exportedModule.func(obj);
console.log(actualReturn);
expect(actualReturn).toBe(expectedReturn);
});
When this code runs, the console.log(actualReturn) function is outputting Promise { <pending> }. Normally I would address this by adding an await before exportedModule.func, but for some reason this does not work and an error is thrown saying exportedModule.func is not asynchronous (even though I have used async when writing the function). Would anyone know of the correct way to get the actual returned value of the fucntion, and not a promise? Or is there something wrong with the way I'm getting the func function that's causing it to not be recognised as asynchronous?