Testing your code

Preparation

For our test suite, we use mocha. Before you start building your test cases, you should first organise your ./test directory structure, for example in the following way:

./myApp
├── api
├── assets
├── ...
├── test
│  ├── unit
│  │  ├── controllers
│  │  │  └── UsersController.test.js
│  │  ├── models
│  │  │  └── Users.test.js
│  │  └── ...
│  ├── fixtures
│  ├── ...
│  ├── bootstrap.test.js
│  └── mocha.opts
└── views

bootstrap.test.js

This file is useful when you want to execute some code before and after running your tests (e.g. lifting and lowering your sails application):

var Sails = require('sails');

before(function(done) {
  Sails.lift({
    // configuration for testing purposes
  }, function(err, sails) {
    if (err) return done(err);
    // here you can load fixtures, etc.
    done(err, sails);
  });
});

after(function(done) {
  // here you can clear fixtures, etc.
  sails.lower(done);
});

mocha.opts

This file should contain mocha configuration as described here: [mocha.opts] (http://visionmedia.github.io/mocha/#mocha.opts)

Writing tests

Once you have prepared your directory you can start writing your unit tests.

./test/unit/models/Users.test.js

describe.only('UsersModel', function() {

  describe('#find()', function() {
    it('should check find function', function (done) {
      Users.find()
        .then(function(results) {
          // some tests
          done();
        })
        .catch(done);
    });
  });

});

Testing controllers

To test controller responses you can use Supertest library which provides several useful methods for testing HTTP requests.

./test/unit/controllers/UsersController.test.js

var request = require('supertest');

describe('UsersController', function() {

  describe('#login()', function() {
    it('should redirect to /mypage', function (done) {
      request(sails.hooks.http.app)
        .post('/users/login')
        .send({ name: 'test', password: 'test' })
        .expect(302)
        .expect('location','/mypage', done);
    });
  });

});

Code coverage

Another popular method for testing your code is Code Coverage.

You can use mocha and istanbul to check your code and prepare various coverage reports (HTML, Cobertura) which can be used in continuous integration services such as Jenkins.

To test your code and prepare a simple HTML report run the following commands:

istanbul cover -x "**/config/**" _mocha -- --timeout 5000
istanbul report html