14
JavaScript Testing With Mocha and Chai

JavaScript Testing: Mocha + Chai

Embed Size (px)

DESCRIPTION

Introduction to Mocha with Chai for TDD/BDD with JavaScript

Citation preview

Page 1: JavaScript Testing: Mocha + Chai

JavaScript TestingWith Mocha and Chai

Page 2: JavaScript Testing: Mocha + Chai

JavaScript Test Frameworks

• QUnit

• Mocha

• YUI Test

• Jasmine

• JSUnit

• Suitest

• Sinon.js

• DOH

• Enhance JS

• RhUnit

• CrosscheckJ3Unit JSNUnitJSSpec UnitTestingJSpec

screw-unit

Test.SimpleRhinoUnitBuster.JS

Page 3: JavaScript Testing: Mocha + Chai

Choosing a framework

• Client side?

• Server side?

• Well maintained?

• Well documented?

• Integration with CI

Page 4: JavaScript Testing: Mocha + Chai

Mocha

Page 5: JavaScript Testing: Mocha + Chai

Mocha

• Feature Rich

• Runs on node + the browser

• Simplifies async testing

• Growl notifications

• Choose your own assertion library

Page 6: JavaScript Testing: Mocha + Chai

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: JavaScript Testing: Mocha + Chai

Getting started

• Requires node

• Requires npm

• npm install -g mocha

• npm install -g chai

Page 8: JavaScript Testing: Mocha + Chai

Setup

• Expects tests to be in <project_root>/test

• Allows of per project options file:

• mocha.opts

• To run test:

• mocha

Page 9: JavaScript Testing: Mocha + Chai

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

Page 10: JavaScript Testing: Mocha + Chai

Hooks

• before()

• after()

• beforeEach()

• afterEach()

Page 11: JavaScript Testing: Mocha + Chai

Modifying Test Cases

• Pending tests - no callback

• Exclusive tests - append .only

• Inclusive tests - append .skip

Page 12: JavaScript Testing: Mocha + Chai

Other features

• mocha --reports

• mocha --watch

• mocha --growl

• mocha --compilers

• Interface support for: TDD, BDD + QUnit

Page 13: JavaScript Testing: Mocha + Chai

CoffeeScript

• mocha --compilers coffee:coffee-script

describe 'Task instance', ->  task1 = task2 = null

  it 'should have a name', ->    task1 = new Task 'feed the cat'    task1.name.should.equal 'feed the cat'

Page 14: JavaScript Testing: Mocha + Chai

Fin