This will briefly guide you on how to carry out testing with serverless lambda.
The following dependencies is required:
# Install the mocha test framework globally
$ npm i -g mocha
# Install the serverless-mocha-plugin as development dependencies
$ npm i --save-dev serverless-mocha-plugin
$ npm i -g serverless
You might also need to include your AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID in your environment.
Once you have the mocha and the plugin installed, then add the following line to your serverless.yml:
# Code not shown due to brevity
functions:
hello:
handler: handler.hello
plugins:
- serverless-mocha-plugin
Run $ sls to see a list of available commands. You should be able to see additional commands available for testing, namely:
- create test
- invoke test
Let's say you have a handler:
module.exports.hello = (event, context, callback) => {
// Code not shown due to brevity
}In order to create a test for hello function, type the following in your terminal:
$ sls create test --function hello
# or short
$ sls create test -f helloA test folder with the file hello.js will be created.
Now you can run the test with the command:
# Invokes the test
$ sls invoke testIf you are using SQS, SNS, or DynamoDB, you can mock them using the library aws-sdk-mock:
const AWS = require('aws-sdk-mock')
AWS.mock('DynamoDB', 'putItem', function (params, callback){
callback(null, "successfully put item in database")
})
AWS.mock('SNS', 'publish', 'test-message')
/**
TESTS
**/
AWS.restore('SNS', 'publish')