Skip to content

Instantly share code, notes, and snippets.

@bynaki
Last active December 1, 2015 12:30
Show Gist options
  • Save bynaki/d23513d2f01d3714d574 to your computer and use it in GitHub Desktop.
Save bynaki/d23513d2f01d3714d574 to your computer and use it in GitHub Desktop.
JavaScript :: Mocha - Installation - Getting Started - Synch

:origin:

JavaScript :: Mocha

Installation

Install with npm:

$ npm install -g mocha

Getting Started

$ npm install -g mocha
$ mkdir test
$ $EDITOR test/test.js

In your editor:

var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    });
  });
});

Back in the terminal:

$  mocha

  .

  ✔ 1 test complete (1ms)

Synchronous Code

When testing synchronous code, omit the callback and Mocha will automatically continue on to the next test.

describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      [1,2,3].indexOf(5).should.equal(-1);
      [1,2,3].indexOf(0).should.equal(-1);
    });
  });
});

Asynchronous Code

Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete. By adding a callback (usually named done) to it() Mocha will know that it should wait for completion.

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(function(err) {
        if (err) throw err;
        done();
      });
    });
  });
});

To make things even easier, the done() callback accepts an error, so we may use this directly:

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(done);
    });
  });
});
Working with Promises

Alternately, instead of using the done() callback, you may return a Promise. This is useful if the APIs you are testing return promises instead of taking callbacks:

beforeEach(function() {
  return db.clear()
    .then(function() {
      return db.save([tobi, loki, jane]);
    });
});

describe('#find()', function() {
  it('respond with matching records', function() {
    return db.find({ type: 'User' }).should.eventually.have.length(3);
  });
});

(The latter example uses Chai as Promised for fluent promise assertions.)

Hooks

Mocha provides the hooks before(), after(), beforeEach(), and afterEach(), which can be used to set up preconditions and clean up after your tests.

describe('hooks', function() {

  before(function() {
    // runs before all tests in this block
  });

  after(function() {
    // runs after all tests in this block
  });

  beforeEach(function() {
    // runs before each test in this block
  });

  afterEach(function() {
    // runs after each test in this block
  });

  // test cases
});
{"noteId":"1515be51de8-d345438f","main":"1515be51de8-d345438f.md","title":"JavaScript :: Mocha - Installation - Getting Started - Synchronous Code - Asynchronous Code - Working with Promises - Hooks"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment