Jumping into Jest and unit testing lambda functions. I'm trying to mock the aws dynamodb-document client scan in my unit test. I'm getting back the actual scan from the real db though so i know something with my mock isn't working. This is my .test.js:
var AWS = require('aws-sdk-mock');
const sinon = require('sinon');
let index = require('./index.js');
beforeEach(()=> {
AWS.mock('DynamoDB.DocumentClient', 'scan', function(params, callback) {
callback(null, {test: "value"});
})
});
afterEach(()=> {
AWS.restore();
});
it( 'test invocation', async() => {
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
const result = await index.handler({}, {}, (err, result) => {
expect(result).toEqual({test: "value"});
});
});
My actual lambda function code looks like this:
'use strict';
var AWS = require("aws-sdk");
var DOC = require("dynamodb-doc");
AWS.config.update({region: "us-east-1"//,
});
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "table-name",
FilterExpression: "#PK = :cat_val",
ExpressionAttributeNames: {"#PK": "PK",},
ExpressionAttributeValues: { ":cat_val": "Value"}
};
exports.handler = (event, context, callback) => {
var response;
docClient.scan(params, onScan);
function onScan(err, data) {
if (err) {
response = {
statusCode: 500,
body: "Unable to complete scan. Error JSON: " + JSON.stringify(err,null,2)
}
} else {
data.Items.sort(compare);
response = {
statusCode: 200,
body: data.Items
}
}
callback(null, response);
console.log(response);
}
}
Any direction you guys can point me in. The test fails on the equal compare because it has the actual response from the Dynamo scan.
Thanks, Tim