Skip to content

Instantly share code, notes, and snippets.

@leommoore
Last active October 4, 2015 08:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leommoore/2606388 to your computer and use it in GitHub Desktop.
Save leommoore/2606388 to your computer and use it in GitHub Desktop.
Node - Mocha Testing

#Node - Testing with Mocha

http://visionmedia.github.com/mocha/

##Mocha

npm install mocha -g

Mocha looks for something called test or a directory called test

You can run mocha using

mocha

You can specify the runner output using

mocha -R spec

You can add this to your mocha options using

echo "-R spec" > test/mocha.opts

To set the test runner to watch for changes use:

mocha -w

##Sample

#####\lib\customer.js

var Customer = function(firstName,lastName){

  this.firstName = firstName;
  this.lastName = lastName;

  var _register = function(){
    console.log("Customer registered");
  }
  
  return {
    register: _register
  }
}();

module.exports = Customer;

#####\test\customerSpec.js

This uses should.js which is available from npm install should, but you can also use expect.js or regular node asserts.

var should = require("should");
var customer = require("../lib/customers");
describe("Customer",function(){

  it("exists",function(){
    should.exist(customer);
  });
  
  it("has a register function",function(){
    should.exist(customer.register);
  });
  
 });

#####\test\mocha.opts

You can specify the options you want to use in a mocha.opts file.

-R spec

#####More Detailed Example

//All the MongoDB logic is encapsulated in the model
var User = require('../data/models/User');

/*
 * Mocha Test
 *
 * Tests are organized by having a "describe" and "it" method. Describe
 * basically creates a "section" that you are testing and the "it" method
 * is what runs your test code.
 *
 * For asynchronous tests you need to have a done method that you call after
 * your code should be done executing so Mocha runs to test properly.
 */

describe('Users', function(){
  var currentUser = null;

  /*
   * beforeEach Method
   *
   * The before each method will execute every time Mocha is run. This
   * code will not run every time an individual test is run.
   */

  beforeEach(function(done){
    user.register('test@test.com', 'password', function(doc){
      currentUser = doc;
      done();
    });
  });

  /*
   * afterEach Method
   *
   * Just like the beforeEach, afterEach is run after Mocha has completed
   * running it's queue.
   */

  afterEach(function(done){
    user.model.remove({}, function(){
      done();
    });
  });

  it('registers a new user', function(done){
    user.register('test2@test.com', 'password', function(doc){
      doc.email.should.eql('test2@test.com');
      done();
    });
  });

  it('fetches user by email', function(done){
    user.findByEmail('test@test.com', function(doc){
      doc.email.should.eql('test@test.com');
      done();
    });
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment