Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use mocha test framework with node.js and sails.js

I want to use mocha for node.js. The last test framework I used was Rspec from Ruby on Rails so I'm trying to do it in the same way but I get cnfused by the huge framework and all the libraries I could use.

I'm following the official get started but it doesn't explains how organize the tests.

http://visionmedia.github.io/mocha/#installation

Now, I'm reading that I can use the following libraries:

  • https://github.com/visionmedia/should.js - Something to test model instances.
  • https://github.com/LearnBoost/expect.js - Minimalistic BDD assertion toolkit based on should.js
  • http://chaijs.com/ - Looks big, includes should, expect and another lib
  • https://github.com/visionmedia/better-assert - Better c-style assertions using callsite for self-documenting failure messages. (I actually don't understand the purpose so far, looks not better than others)
  • https://github.com/rjanicek/mocha.js-haxe - Looks to be used browser side, but mocha also said that it's running browser side.

And I know there are more, this is just the list I saw on mocha official website. For what I can understand, it looks like chai is the one to use with mocha, what do you think about that?

And, so far, I never saw anything to help me to decide where write the tests (okay, in /test/, of course) and how organize everything.

I'm also use the great sails.js framework (based on express) and pomelo.js for different projects, I need to use the same kind of tests on both frameworks, so I'm looking for a general architecture and libraries that I can use on both (so, something not specific to sails.js but usable directly from any other framework)

This is how I plan to organize my tests, do you think that's a correct architecture? enter image description here

The main issue with node is that there are a lot of frameworks, plugins, libraries and I dont know what's the best choice, node.js is really huge with a big community and that's really difficult to have an overview of all the possibilities.

How do you deal with your tests?

like image 730
Vadorequest Avatar asked Feb 15 '14 14:02

Vadorequest


1 Answers

I found the blog post Unit Testing Sails.js Applications With Mocha helpful in regards to setting up and organizing tests.

However, because I'm using Sails version 0.10.5, I decided to clean up the Grunt configuration a bit. I added a file named mocha.js in the tasks/config/ folder with the following code:

module.exports = function(grunt) {

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

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

};

I then registered the task by creating a file named test.js inside of tasks/register. Inside this file, I added the following code.

module.exports = function (grunt) {
    grunt.registerTask('test', [
            'mochaTest'
        ]
    );
};

You can then run grunt test to see the results.

like image 81
Rob Sawyer Avatar answered Nov 01 '22 16:11

Rob Sawyer