Writing tests for a Lambda function.
In the code below, when the test calls handler, it throws an error saying: TypeError: handler is not a function.
index.js (Lambda)
const aws = require('aws-sdk')
exports.handler = async function(event, context) {
//if incoming data is valid then proceed
let message = parse_event(event);
if(abc(message))
{
await doSomething(message);
}
};
function parse_event(message)
{
return JSON.parse(message)
}
function abc(message)
{
conssole.log('xyz')
return true
}
async function doSomething(message)
{
console.log('doing something with: ' + JSON.stringify(message))
}
module.exports = {abc, doSomething, parse_event}
index.spec.js
const mod = require("./index");
describe('Tests', () =>
{
test('does it do something', async ()=>
{
var ev = '{"xyz": 123}'
...
//Here it throws -- TypeError: handler is not a function.
await mod.handler(ev, "")
expect(1=1)
});
});
I've tried putting handler in the module.exports list, but no go.
When I break at the handler call, it shows as undefined. But when not in debug mode, if I dot the module where it resides, it is listed as a property/function.
Anyone have any advice here?
Thanks!