https://github.com/alextanhongpin/serverless-test
Simple test for aws serverless
https://github.com/alextanhongpin/serverless-test
aws serverless-testing sls
Last synced: 3 months ago
JSON representation
Simple test for aws serverless
- Host: GitHub
- URL: https://github.com/alextanhongpin/serverless-test
- Owner: alextanhongpin
- Created: 2017-05-18T01:43:19.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-30T16:47:12.000Z (about 9 years ago)
- Last Synced: 2025-03-24T16:15:18.699Z (over 1 year ago)
- Topics: aws, serverless-testing, sls
- Language: JavaScript
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Serverless testing with Mocha
This will briefly guide you on how to carry out testing with serverless lambda.
### Installation
The following dependencies is required:
```bash
# 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.
### Add Test Plugin
Once you have the mocha and the plugin installed, then add the following line to your `serverless.yml`:
```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:
1. create test
2. invoke test
### Create Test
Let's say you have a handler:
```javascript
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:
```bash
$ sls create test --function hello
# or short
$ sls create test -f hello
```
A test folder with the file `hello.js` will be created.
### Run the test
Now you can run the test with the command:
```bash
# Invokes the test
$ sls invoke test
```
### Mock Dependencies
If you are using SQS, SNS, or DynamoDB, you can mock them using the library `aws-sdk-mock`:
```js
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')
```