Serverless Dynamodb Local Unit Testing


Testing plays an integral role within the software development life cycle. As a developer testing is mainly divided into integration testing and unit testing. Today’s article is about how we can write unit test cases for serverless plugins.

Serverless is a cloud based solution to write and maintain applications. I will take Dynamodb Local plugin as an example and explain about how to write unit testing with Mocha and chai (expect) test frameworks. Mocha is a JavaScript test framework that runs on NodeJS. To start up, we’ll see the requirements.

Requirements:

- Serverless project/template
- Dynamodb Local Plugin
- NodeJS latest version and dependencies
- Test Framework

Getting Started:

Step 1: Go to your terminal/command line and clone the plugin from GitHub as follows. Then create a test folder inside the root folder.

- git clone https://github.com/99xt/serverless-dynamodb-local.git
- cd test
- mkdir test

Step 2: After creating a test folder, inside it, create a sample-template folder. Inside the sample-template folder, install NPM, so we can install dependencies for the project.

- mkdir sample-template
- cd sample-template
- npm init — yes

Step 3: Install the dependencies according to the dynamodb local plugin. We will need serverless offline since we are testing the plugin offline. And according to the installation guide in dynamodb local, we will be installing the serverless 1.0.0-rc.1. AWS SDK is needed since we are running an aws plugin. Dynamodb client wrapper is needed to run the queries and test them locally. It will help to pass the queries which we will be testing soon.

- npm install — save-dev serverless-offline
- npm install -g serverless@1.0.0-rc.1
- npm install aws-sdk
- npm install — save dynamodb-doc-client-wrapper

Step 4: Now let’s create a template the in sample-template folder. We will need a serverless project or at least a template in order to run a unit test to a plugin.

- serverless create — template aws-nodejs

Step 5: Now Go back to serverless dynamodb local folder and link the plugin via symlink. Symlink or soft link is a way to attach the references in another directory or a file in theform of a relative or an absolute path.

We will link the main folder, and then go back to our sample-template folder/node_modules folder and link the path and create a shortcut.

- npm link
- cd test/sample-template/node_modules
- npm link serverless-dynamodb-local

Step 6: Go back to the sample-template folder, you can see serverless.yml and input the following lines to the file.

serverless.yml

Step 7: It’s time to install the testing dependencies to our project. Basically we will run the test cases with the mocha framework. Should, Expect, request and chai are some of the different modules which we can use to test our code in several ways. The above mentioned modules are mainly used in TDD (Test driven development). You can run the test cases according to your wish. For the tutorial purpose, I will be using should module.

- npm install — save-dev mocha
- npm install — save-dev should

Note: In the cloned project, mocha is already included in the Package.json folder. But if you are writing test cases to another project, include the following line in the scripts.

package.json

Step 8: We have installed all the dependencies needed to run the project. Therefore, now it’s time to install dynamodb local and run the project. Before installing the plugin, go to sample-template folder and try the following code, see whether we have installed everything properly.

- sls -h
Available commands

Step 9: If you have installed everything correctly, you will see the above commands in your terminal/command line. Now let’s install the dynamodb local and run it on port 3000 in detached mode.

- sls dynamodb install
- sls dynamodb start -p 3000 &

Congratulations! It’s up and running…


Unit Testing

Now we can test our dynamodb local plugin.

Go to serverless-dynamodb-local/test folder. Create a test.js file and insert the following code.

Initial configuration— Test.js

We have to install the required modules and then link index.js file. After that we will configure the aws and setup mocha testing environment. Our goal is to check whether the table creation works with the locally setup serverless dynamodb.

Here we are testing an asynchronous code with mocha. To invoke the callback when your test is complete, we use (done) in the it(), then mocha will know that it should wait for the function in order to complete the test. Inside the “it” include the create table query for dynamodb.

Create Table — Dynamodb Query

Now let’s insert a test command from ShouldJS to check if the unit test passes or not.

Writing test case from should

If an error occurred, data should not be created is the first test case which is demonstrated as,

should.not.exist(data);

If the table is created, it should contain the data we inserted in the above code and the TableName property should have “Movies” as the value of it.

should.exist(data);
data.TableDescription.should.have.property(“TableName”,”Movies”);

We wrote our very first test case, now let’s run the command and check whether the test passes.

Go to your terminal/command line, make sure you are in the serverless-dynamodb-local directory, and then run “npm run test”.

Your first test case

Congratulations! You have successfully created your first unit test. If you have any questions to clarify please comment below.