lambda-tester
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/lambda-tester package

4.0.1 • Public • Published

Build Status npm version

lambda-tester

Simplifies writing unit tests for AWS Lambda functions using Node.js.

Features

  • Verifies correct handler behavior
  • Mocks event types with code
  • Works asynchronously
  • Supports Promises
  • Verifies Node.js runtime version
  • AWS X-Ray support [experimental]
  • Detects resource leaks [experimental]
  • Easily integrates with test frameworks (Mocha and Jasmine)
  • Handlers can be loaded and removed after execution
  • Lightweight and won't impact performance
  • Maps the environment variable LAMBDA_TASK_ROOT to the application's root
  • Automatically loads .env files
  • Works with Node 10.x, and 12.x

Installation

Install via npm.

npm install lambda-tester --save-dev

Getting Started

Lambda handlers with support for callbacks use the typical Node.js asynchronous signature:

exports.handler = function( event, context, callback ) {
 
    callback( null, 'success!' );
}

The following example shows a simple case for validating that the Lambda (handler) was called successfully (i.e. callback( null, result ):

const LambdaTester = require( 'lambda-tester' );
 
const myHandler = require( '../index' ).handler;
 
describe( 'handler', function() {
 
    it( 'test success', async function() {
 
        await LambdaTester( myHandler )
            .event( { name: 'Fred' } )
            .expectResult();
    });
});

If the handler calls callback( err ), then the test will fail.

Additionally, if one wanted to test for failure, then the following code would be used:

const LambdaTester = require( 'lambda-tester' );
 
const myHandler = require( '../index' ).handler;
 
describe( 'handler', function() {
 
    it( 'test failure', async function() {
 
        await LambdaTester( myHandler )
            .event( { name: 'Unknown' } )
            .expectError();
    });
});

Note: you must either return the LambdaTester instance back to the testing framework or use the await/async keywords.

Documentation

Complete documentation can be found in our documentation page.

Projects Using lambda-tester

  • vandium - Secures and simplifies AWS Lambda handlers

Feedback

We'd love to get feedback on how you're using lambda-tester and things we could add to make this tool better. Feel free to contact us at feedback@vandium.io

Compatibility

Starting with version 4.0, lambda-tester supports node versions 10 and higher. If you require support for older versions of node, then use a previous version of lambda-tester.

License

BSD-3-Clause

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.0.119,192latest

Version History

VersionDownloads (Last 7 Days)Published
4.0.119,192
4.0.014
3.6.0942
3.5.0166
3.4.10
3.4.00
3.3.00
3.2.02
3.1.10
3.1.07
3.0.216
3.0.14
3.0.00
2.8.1122
2.8.04
2.7.02
2.6.114
2.6.00
2.5.10
2.5.00
2.4.00
2.3.00
2.2.11
2.2.01
2.1.00
2.0.00
1.0.11
1.0.00

Package Sidebar

Install

npm i lambda-tester

Weekly Downloads

16,541

Version

4.0.1

License

BSD-3-Clause

Unpacked Size

28.7 kB

Total Files

14

Last publish

Collaborators

  • richardhyatt