JavaScript Testing
With Mocha and Chai
What we have?
• QUnit
• Mocha
• YUITest
• Jasmine
• JSUnit
• Suitest
• Sinon.js
• DOH
• Enhance JS
• RhUnit
• ….
Why Mocha?
• Client side
• Server side
• Well maintained
• Well documented
• Integration with CI
• We can choose desired assertion library
Mocha
Mocha
• Feature Rich
• Runs on node + the browser
• Simplifies async testing
• Supports TDD/BDD
• Choose any Mocking library
• File watcher support
Chai
• BBD / TDD
• For node + the browser
• Three assertion styles
• should - foo.should.be.a(‘string’)
• expect - expect(foo).to.be.a(‘string’)
• assert - assert.typeOf(foo,‘string’)
Getting started
• Requires node
• Requires npm
• npm install -g mocha
• npm install -g chai
Setup
• Expects tests to be in <project_root>/test
• Allows of per project options file:
• mocha.opts
• To run test:
• mocha
First 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);
})
})
})
Hooks
• before()
• after()
• beforeEach()
• AfterEach()
Hooks
describe('Array', function() {
before(function() {
});
after(function(){
});
})
Thanks!

Mocha Testing