Joseph Jude

Introduction To Sailsjs Testing

2014.08.26 / code / sailsjs / testing / nodejs /

Basics on getting started with testing Sailsjs apps

Sailsjs is a superb web-app framework for nodejs. It's easy to learn and comes with all required components for developing web-apps (except database migrations).

To learn Sailsjs, I scratched an itch I had and developed a self-hosted static blog engine (the code lives in Bitbucket).

In the beginning, there were not many features and so testing everything manually was okay. But as I added features, testing all possible scenarios became difficult. So I am starting with automated tests.

Sailsjs itself is tested using mocha. So I selected mocha for testing my app too. This post is an introductory post on starting with testing sails app, not a detailed guide. As I go along, I will post more details of the testing.

Starting with testing consists of the following steps:

  1. Install dependencies
  2. Change grunt tasks
  3. Execute grunt test to test

1. Installing Dependencies

We need to install mocha, grunt-mocha-test and assert. These are not required for production use, so install them as dev-dependencies.

npm install mocha --save-dev
npm install grunt-mocha-test --save-dev
npm install assert --save-dev

After installation, you should see the following in the package.json:

  "devDependencies": {
    "assert": "^1.1.2",
    "grunt-mocha-test": "^0.11.0",
    "mocha": "^1.21.4"

2. Change grunt tasks

Under tasks/config, create a new file (test.js is fine) and copy, paste the following code:

 * test.js

module.exports = function(grunt) {
  grunt.config.set('mochaTest', {
    test : {
      options: {
        reporter: 'spec'
      src: ['tests/**/*.spec.js']

This means that tests should be appended with .spec.js.

Under tasks/register create a new file (again test.js is fine) and copy, paste the following code:

module.exports = function (grunt) {
     grunt.registerTask('test', [

3. Execute grunt test to test

Now you are ready to execute the test (remember, this is only an introductory guide, not a detailed one).

If you type grunt test, you will see the following results

Running "mochaTest:test" (mochaTest) task
No files to check...OK

Done, without errors.

The basic setup for testing an app is done. Detailed testing of models and controllers will follow.


Thanks for reading. Would you please let others know by sharing in Twitter?