Package Exports
- lambda-tdd
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lambda-tdd) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Test Framework for AWS Lambda
Testing Framework for AWS Lambda. Very useful for integration testing as you can examine how your lambda function executes for certain input and specific environment variables. Tries to model the cloud execution as closely as possible.
What it does
- Tests are defined as JSON files
- Test are dynamically evaluated using Chai
- Lambda functions are executed using Lambda-Wrapper
- Supports external request mocking using Nock
- Allows setting of environment variables on a per test granularity
- Freeze execution to specific timestamp with Timekeeper
- Set lambda timeout (
context.getRemainingTimeInMillis()
) - Set test timeout
- Specify event input
- Test success and error responses
Getting Started
To install run
$ npm install --save-dev lambda-tdd
Initialize Test Runner and Execute
const lambdaTester = require("lambda-tdd")({ cwd: __dirname });
describe("Testing Tester", () => {
lambdaTester.execute();
});
You can pass an array of test files to the execute()
function. By default tests are auto detected.
Test Runner Options
cwd
Type: string
Default: process.cwd()
Directory which other defaults are relative to.
name
Type string
Default: lambda-test
Name of this test runner for debug purposes.
verbose
Type boolean
Default: false
Display console output while running tests. Useful for debugging.
handlerFile
Type: string
Default: handler.js
Handler file containing the handler functions (specified in test).
cassetteFolder
Type: string
Default: __cassettes
Folder containing nock recordings.
envVarYml
Type: string
Default: env.yml
Specify yaml file containing environment variables. No existing environment variables can be overwritten.
testFolder
Type: string
Default: ``
Folder containing test files.
Test File Format
handler
Type: string
Required
The handler inside the handler file, i.e. if handler.js
contained
module.exports.returnEvent = (event, context, cb) => cb(null, event);
we would set this to returnEvent
.
env
Type object
Default: {}
Contains environment variables that are set for this test. Existing environment variables can be overwritten.
timestamp
Type unix
Default: Unfrozen
Set unix timestamp that test executing will see. Time does not progress if this option is set.
timeout
Type integer
Default: Mocha Default Timeout
Set custom timeout in ms for lambda execution. Handy e.g. when recording nock requests.
event
Type object
Default: undefined
Event object that is passed to lambda handler.
lambdaTimeout
Type integer
Default: 300000
Set initial lambda timeout in ms. Exposed in lambda function through context.getRemainingTimeInMillis()
.
The timeout is not enforced, but progresses as expected unless timestamp
option is used.
success
Type boolean
Required
True iff execution is expected to succeed, i.e. no error is passed into callback.
response
Type array
Default: []
Dynamic expect logic executed against the response string. More details below.
error
Type array
Default: []
Dynamic expect logic executed against the error string. More details below.
body
Type array
Default: []
Dynamic expect logic executed against the response.body string. More details below.
logs
Type array
Default: []
Dynamic expect logic executed against the console.log
output array. More details below.
nock
Type array
Default: []
Dynamic expect logic executed against the nock recording. More details below. Note that the nock recording must already exists for this check to evaluate correctly.
Dynamic Expect Logic
Uses Chai Assertion Library syntax written as json. Lets assume we have an output array [1, 2]
we want to validate. We can write
expect([1,2]).to.contain(1);
expect([1,2]).to.contain(2);
as the following json
[{
"to.contain": 1
}, {
"to": {
"contain": 2
}
}]
Note that targets are either arrays or strings, but never objects (design limitation).
Test File Examples
You can find some examples of JSON test files here.