aws-lambda-test-utils
Testing your AWS Lambda Functions does not need to be difficult or complex.
If you are new to Amazon WebServices Lambda
(or need a refresher),
please checkout our our
Beginners Guide to AWS Lambda:
https://github.com/dwyl/learn-aws-lambda
Why?
Testing your code is essential everywhere you need reliability.
AWS Lambda has a Testing Console which is a web-based way of invoking your function(s) with a given input and monitoring the result. But this quite slow and cannot be automated (yet).
What?
The simplest possilbe way we could think of for Testing our AWS Lambda functions.
How? (Usage)
aws-lambda-test-utils
from NPM
install npm install aws-lambda-test-utils --save-dev
Use in your Tests
Mock Context
An example of using the mockContextCreator
in an example test.
'use strict';var test = ;var utils = var mockContextCreator = utilsmockContextCreator;var index = ; // lambda function var ctxOpts = functionName: 'LambdaTest' functionVersion: '1' invokedFunctionArn: 'arn:aws:lambda:eu-west-1:655240711487:function:LambdaTest:ci';var testEvent = key1: 'value1' ; ;
Mock Events
This helper can be used to mock event objects created by AWS services like S3, SNS, or DynamoDB.
var utils = ; // has 3 methods on it: createDynamoDBEvent, createSNSEvent, createS3Eventvar mockEventCreator = utilsmockEventCreator;
createDynamoDBEvent(options)
Creates a mock DynamoDB event.
var utils = ;var dynamoEvent = utilsmockEventCreator; // Default options (which can be overridden): awsRegion: "eu-west-1" eventSourceARN: "arn:aws:dynamodb:us-west-2:account-id:table/ExampleTableWithStream/stream/2015-06-27T00:48:05.899" events: type: "INSERT" number: 1;
createSNSEvent(options)
var utils = ;var dynamoEvent = utilsmockEventCreator; // Default options (which can be overridden): message: "default test message";
createS3Event()
var utils = ;var dynamoEvent = utilsmockEventCreator;
createAPIGatewayEvent(options)
var utils = ;var APIGatewayEvent = utilsmockEventCreator; // Default options (which can be overridden): path: "default/path" method: "GET" headers: "default-header": 'default' queryStringParameters: query: "default" pathParameters: uuid: '1234' stageVariables: ENV: "test" body: "default body"
Documentation
event
& context
Lambda Function Every AWS Lambda function take two parameters event
& context
context
The 'context' object has the following form:
//methods success done fail getRemainingTimeInMillis //properties functionName functionVersion invokedFunctionArn memoryLimitInMB awsRequestId logGroupName logStreamName identity: cognito_identity_id cognito_identity_pool_id clientContext: client: installation_id app_title app_version_name app_version_code app_package_name Custom env: platform_version platform make model locale
The properties of the context object can be specified in an options parameter to the mockContextCreator
function.
Background Reading
TODO
- Add stubs for AWS SDK methods