{ sailsit }

Sails Js Tutorials for Beginners and Forum

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Unit Testing with Mocha.js

Mocha.js is Javascript test framework, which can be used for writing unit test, and test your code.
In this tutorial we will be writing unit tests for testing RestAPI built in SailsJs

1. Setting up project

For this tutorial we will be using our sails-simple-crud project, which we can created in one of our old tutorial
To setup the project, follow the tutorial at below URL
SAILS_SIMPLE_CRUD

2. Installing Packages

Install mocha package using NPM, using the below command it will install only in dev environment.

npm install --save-dev mocha

You can also install mocha at global level using below command

npm install --global mocha

We will also need supertest package as we need to test RestAPI

npm install supertest --save

 

3. Setting up directory

For unit testing we will be creating test directory, create a directory in project root directory

mkdir test

Now create a ApiTest.js file inside test directory

4. Writing Test Case

Paste the below code in your test/ApiTest.js

var request = require('supertest');
request = request('http://localhost:1337/api/'); //root url

describe("Mocha Unit Test", function () {
    describe("simple crud operations test", function () {
        it("should return failed response if no records found", function (done) {
            request.get("student/get")
                .set("Accept", "application/json")
                .expect(200, {
                    "success": false,
                    "message": "No records found in DB"
                } ,done);
        });
        it("should create student and return success", function (done) {
            request.post("student/create")
                .set("Accept", "application/json")
                .send({name: "Sailsit", email: "developer@sailsit.com", phone: "9999999999", department: "sailsjs", student_id: 1})
                .expect(200)
                .end(function (err, res) {
                    if(res.body.data.email === "developer@sailsit.com") {
                        done();
                    }
                });
        });
        it("should update name and return success", function (done) {
            request.post("student/update/1")
                .set("Accept", "application/json")
                .send({name: "SailsitUpdated", email: "developer@sailsit.com", phone: "9999999999", department: "sailsjs", student_id: 1})
                .expect(200)
                .end(function (err, res) {
                    if(res.body.success) {
                        done();
                    } else {
                        throw ({msg: "not created"});
                    }
                });
        });
        it("should return student list", function (done) {
            request.get("student/get")
                .set("Accept", "application/json")
                .expect(200)
                .end(function (err, res) {
                    if(res.body.data instanceof Array && res.body.data.length > 0) {
                        done();
                    }
                });
        });
        it("should delete the student with id 1", function (done) {
            request.delete("student/delete/1")
                .set("Accept", "application/json")
                .expect(200)
                .end(function (err, res) {
                    if(res.body.success) {
                        done();
                    }
                });
        })
    });
});

 

5. Running Test Case

To run the test cases, first start your sails application.
Use the below command to run the test cases
Mocha installed at project level

node_modules/mocha/bin/mocha test/ApiTest.js

Mocha installed at global level

mocha test/ApiTest.js

Running this command will return the result of all the test cases written in your test/ApiTest.js file

This tutorial demonstrate unit testing at very basic level, you can also write logic and even DB related validations with this.

Complete source code is available at below URL
Sails-Unit-Test-Mocha

You can aslo watch the video showing Unit Test running demo