Question

I want to add tests to my SailsJS project, Mocha is pretty common so I decided to use it.

I configured and registered the grunt-mocha-test to run my tests when the "default" task trigger (tasks/register/default.js) runs, but it is not running anything!

How to properly configure and setup Mocha into SailsJS to run automatically with grunt?

Is it possible to run the tests manually?

Here is what I did:

1) install test packages npm install mocha --save-dev npm install grunt-mocha-test --save-dev npm install sinon --save-dev npm install assert --save-dev

3) added file tasks/config/mocha-test.js

module.exports = function(grunt) {

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

    grunt.loadNpmTasks('grunt-mocha-test');
};

**3) registered tasks in tasks/register/default.js **

module.exports = function (grunt) {
    grunt.registerTask('default', ['compileAssets', 'linkAssets',  'watch', 'mochaTest']);
};

2) created tests folder in the app root

tests/controller/session.spec.js

    var SessionController = require('../../api/controllers/SessionController'),
        sinon = require('sinon'),
        assert = require('assert');

        console.log('SessionController: ', SessionController);

    describe('The SessionController', function () {
        describe('When we load the signIn page', function () {
            it ('should render the view', function () {
                var view = sinon.spy();
                SessionController.index(null, {
                    view: view
                });
                assert.ok(view.called);
            });
        });
    });
Was it helpful?

Solution

I don't know that setting up a Grunt task to run your Mocha tests is the best solution. The Grunt tasks in the Sails "pipeline" are run every time you do sails lift; running your tests every time you lift the server could become very annoying as you add more tests to your suite, because you tend to do sails lift a lot during development!

You certainly can run the tests manually; you just need to install Mocha globally:

sudo npm install -g mocha

Then in your app's root directory, simply type mocha to run all the tests under /test. You can specify a subset of tests to run by giving the command a subdirectory, e.g. mocha test/controller.

As far as the tests themselves are concerned, it's possible to unit test a controller by requiring it the way you're doing, but it means you'll have to mock up request and response objects. A better way is to lift a Sails instance in your before function, and then make HTTP requests to your controllers. See answers to this question for a couple of examples of lifting Sails from within tests, either before every test or before each test. You can then use the Node http module to make requests to your controllers, or make life easier on yourself and use the request module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top