Mocha

A framework for easily creating beautiful unit test using javascript

Presentation by Felipe F. Lima / @felipefdlima

Features

browser support

simple async support

string diff support

javascript API for running tests

auto-detects and disables coloring for non-ttys

reports test durations

highlights slow tests

file watcher support

node debugger support

use any assertion library you want

and... More... Very More

proper exit status for CI support etc

maps uncaught exceptions to the correct test case

async test timeout support

test-specific timeouts

growl notification support

global variable leak detection

optionally run tests that match a regexp

auto-exit to prevent "hanging" with an active loop

easily meta-generate suites & test-cases

mocha.opts file support

clickable suite titles to filter test execution

detects multiple calls to done()

use any assertion library you want

extensible reporting, bundled with 9+ reporters

extensible test DSLs or "interfaces"

before, after, before each, after each hooks

arbitrary transpiler support (coffee-script etc)

TextMate bundle

...

Hello Test World

This is a first test in Mocha

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));
    });
  });
});
					

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();
      });
    });
  });
});
					

Avoid code duplication

All "hooks", that is before(), after(), beforeEach(), afterEach() may be sync or async as well, behaving much like a regular test-case.

describe('Connection', function(){
  var db = new Connection,
      tobi = new User('tobi'),
      loki = new User('loki'),
      jane = new User('jane');

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

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

Beautiful! Now, how to start this test?

1. You need NODE.JS

2. You need Mocha


							npm install -g mocha
						

3. And run


							mocha test.js
						

4. By default mocha will use the pattern "./test/*.js", so it's usually a good place to put your tests.


							mocha
						

Browser support

Mocha runs in the browser.

<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link rel="stylesheet" href="mocha.css" />
</head>
<body>
  <div id="mocha"></div>
  <script src="jquery.js"></script>
  <script src="expect.js"></script>
  <script src="mocha.js"></script>
  <script>mocha.setup('bdd')</script>
  <script src="test.array.js"></script>
  <script src="test.object.js"></script>
  <script src="test.xhr.js"></script>
  <script>
    mocha.checkLeaks();
    mocha.globals(['jQuery']);
    mocha.run();
  </script>
</body>
</html>
					

Assertions

Mocha allows you to use any assertion library you want, if it throws an error, it will work!

should.js BDD style shown throughout these docs

expect.js expect() style assertions

better-assert c-style self-documenting assert()

chai expect(), assert() and should style assertions

...

Reports... Reports... Reports...

Mocha have any type of reports

And for use:


								mocha -R spec
							

For see all available:


								mocha --reporters
							

mocha -R dot

mocha -R list

mocha -R progress

mocha -R json

mocha -R spec

mocha -R landing

mocha -R nyan

And when failure?

The airplane down!

Or show a boring list

Code Coverage

Example test suites

The following test suites are from real projects putting Mocha to use, so they serve as good examples:
Express Connect
SuperAgent WebSocket.io
Mocha

Links

THE END

Thank you

Mocha

By Felipe F. Lima