Mocha Testing

Post on 23-Feb-2017

224 views 1 download

Transcript of Mocha Testing

JavaScript TestingWith Mocha and Chai

What we have?• QUnit

• Mocha

• YUI Test

• 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 testdescribe('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()

Hooksdescribe('Array', function() { before(function() {

}); after(function(){ });

})

Thanks!