Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 a...
Mocha
Mocha
• Feature Rich
• Runs on node + the browser
• Simplifies async testing
• Supports TDD/BDD
• Choose any Mocking libra...
Chai
• BBD / TDD
• For node + the browser
• Three assertion styles
• should - foo.should.be.a(‘string’)
• expect - expect(...
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:
• moc...
First test
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not p...
Hooks
• before()
• after()
• beforeEach()
• AfterEach()
Hooks
describe('Array', function() {
before(function() {
});
after(function(){
});
})
Thanks!
Upcoming SlideShare
Loading in …5
×

Mocha Testing

Mocha Testing

  • Be the first to comment

Mocha Testing

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

×