mocha testing

12
JavaScript Testing With Mocha and Chai

Upload: erick-aky

Post on 23-Feb-2017

226 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Mocha Testing

JavaScript TestingWith Mocha and Chai

Page 2: Mocha Testing

What we have?• QUnit

• Mocha

• YUI Test

• Jasmine

• JSUnit

• Suitest

• Sinon.js

• DOH

• Enhance JS

• RhUnit

• ….

Page 3: Mocha Testing

Why Mocha?• Client side• Server side• Well maintained• Well documented• Integration with CI• We can choose desired assertion library

Page 4: Mocha Testing

Mocha

Page 5: Mocha Testing

Mocha• Feature Rich• Runs on node + the browser• Simplifies async testing• Supports TDD/BDD• Choose any Mocking library• File watcher support

Page 6: Mocha Testing

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’)

Page 7: Mocha Testing

Getting started

• Requires node• Requires npm• npm install -g mocha• npm install -g chai

Page 8: Mocha Testing

Setup

• Expects tests to be in <project_root>/test• Allows of per project options file:

• mocha.opts• To run test:

• mocha

Page 9: Mocha Testing

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

Page 10: Mocha Testing

Hooks• before()• after()• beforeEach()• AfterEach()

Page 11: Mocha Testing

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

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

})

Page 12: Mocha Testing

Thanks!