npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-lambda-test-utils

v1.3.0

Published

Test your AWS Lambda Functions for reals!

Downloads

13,616

Readme

aws-lambda-test-utils

Testing your AWS Lambda Functions does not need to be difficult or complex.

Build Status codecov.io Dependency Status devDependency Status

NPM

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)

install aws-lambda-test-utils from NPM

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               = 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();
});

Mock Events

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"
}

Documentation

Lambda Function 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.

Background Reading

TODO

  • Add stubs for AWS SDK methods