![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
aws-lambda-test-utils
Advanced tools
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).
The simplest possilbe way we could think of for Testing our AWS Lambda functions.
aws-lambda-test-utils
from NPMnpm install aws-lambda-test-utils --save-dev
An example of using the mockContextCreator
in an example test.
'use strict';
var test = require('tape');
var utils = require('aws-lambda-test-utils')
var mockContextCreator = utils.mockContextCreator;
var index = require('./index.js'); // lambda function
var ctxOpts = {
functionName: 'LambdaTest',
functionVersion: '1',
invokedFunctionArn: 'arn:aws:lambda:eu-west-1:655240711487:function:LambdaTest:ci'
};
var testEvent = { key1: 'value1' };
test('LambdaTest', function(t){
t.test("LambdaTest: returns value when given event with key1 property", function(st) {
function test(result){
st.equals(result, "value1")
st.end();
};
var context = mockContextCreator({}, test); // no options and test as the callback
index.handler(testEvent, context);
});
t.test("LambdaTest: returns value when given event with key1 property", function(st) {
function test(result){
st.equals(result, "value1")
st.end();
};
var context = mockContextCreator(ctxOpts, test); // context options specified and test as the callback
index.handler(testEvent, context);
});
t.end();
});
This helper can be used to mock event objects created by AWS services like S3, SNS, or DynamoDB.
var utils = require('aws-lambda-test-utils');
// has 3 methods on it: createDynamoDBEvent, createSNSEvent, createS3Event
var mockEventCreator = utils.mockEventCreator;
createDynamoDBEvent(options)
Creates a mock DynamoDB event.
var utils = require('aws-lambda-test-utils');
var dynamoEvent = utils.mockEventCreator.createDynamoDBEvent();
// 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 = require('aws-lambda-test-utils');
var dynamoEvent = utils.mockEventCreator.createSNSEvent();
// Default options (which can be overridden):
{
message: "default test message"
};
createS3Event()
var utils = require('aws-lambda-test-utils');
var dynamoEvent = utils.mockEventCreator.createS3Event();
createAPIGatewayEvent(options)
var utils = require('aws-lambda-test-utils');
var APIGatewayEvent = utils.mockEventCreator.createAPIGatewayEvent();
// 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"
}
event
& context
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.
FAQs
Test your AWS Lambda Functions for reals!
The npm package aws-lambda-test-utils receives a total of 3,799 weekly downloads. As such, aws-lambda-test-utils popularity was classified as popular.
We found that aws-lambda-test-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.