Save 37% off PRO during our Black Friday Sale! »

Testing with mocha

Testing with mocha

Testing with mocha and chaijs

Fb7edc7cc7a53c903af74d07dcecf9dc?s=128

Revath S Kumar

September 27, 2014
Tweet

Transcript

  1. TESTING WITH MOCHA @revathskumar

  2. ABOUT Rubyist / JavaScripter Team Member / / Works at

    Blog at Twitter/Github - Google+: Yeoman @keralarb @keralajs @whatznear blog.revathskumar.com @revathskumar +RevathSKumar
  3. AGENDA Mocha Features BDD/TDD syntax Chai assert library Testing asynchronous

    code Testing in Node.js and Browser Grunt and gulp tasks
  4. NEVER TRUST YOUR CODE

  5. TESTING FRAMEWORKS Mocha Jasmine QUnit

  6. MOCHA

  7. MOCHA: FEATURES Runs on Node.js/Browser Supports BDD/TDD Choose any assertion

    library Choose any Mocking library Async and promise support Highlights slow tests File watcher support Optionally run tests that match a regexp
  8. MOCHA: TDD s u i t e ( ' C

    a l c u l a t o r ' , f u n c t i o n ( ) { s u i t e ( ' A d d ' , f u n c t i o n ( ) { t e s t ( " u s i n g b o t h p o s i t i v e n u m b e r s " , f u n c t i o n ( ) { / / t e s t a s s e r t i o n } ) ; t e s t ( " u s i n g b o t h n e g a t i v e n u m b e r s " , f u n c t i o n ( ) { / / t e s t a s s e r t i o n } ) ; } ) ; } ) ;
  9. MOCHA: SETUP & TEARDOWN s u i t e (

    ' C a l c u l a t o r ' , f u n c t i o n ( ) { s e t u p ( f u n c t i o n ( ) { / / r u n s b e f o r e t e s t } ) ; / / t e s t ( " a d d u s i n g b o t h p o s i t i v e n u m b e r s " , f u n c t i o n ( ) { / / t e s t a s s e r t i o n / / } ) ; t e a r d o w n ( f u n c t i o n ( ) { / / r u n s a f t e r t e s t s } ) ; } ) ;
  10. MOCHA: BDD d e s c r i b e

    ( ' C a l c u l a t o r ' , f u n c t i o n ( ) { d e s c r i b e ( ' a d d ' , f u n c t i o n ( ) { i t ( ' u s i n g b o t h p o s i t i v e n u m b e r s ' , f u n c t i o n ( ) { / / t e s t a s s e r t i o n } ) ; i t ( ' u s i n g b o t h n e g a t i v e n u m b e r s ' , f u n c t i o n ( ) { / / t e s t a s s e r t i o n } ) ; } ) ; } ) ;
  11. MOCHA: BEFORE & AFTER d e s c r i

    b e ( ' C a l c u l a t o r ' , f u n c t i o n ( ) { b e f o r e ( f u n c t i o n ( ) { } ) a f t e r ( f u n c t i o n ( ) { } ) } ) ; beforeEach afterEach
  12. CHAI.JS chai.assert chai.expect chai.should

  13. CHAI: ASSERT chai.assert(expression, [message]) chai.assert(actual, expected, [message]) ok() notOk() fail()

    isTrue() isFalse() isNUll()
  14. CHAI: EXPECT chai.expect(expected).to.not.equal(actual); chai.expect(expected).to.be.a('string'); ok include() true false empty

  15. CHAI: SHOULD value.should.be.an('object'); value.should.equal(10); value.should.be.a('string') object.should.have.property('key')

  16. NODE.JS npm install -g mocha npm install -g chai

  17. NODE-TEST / / c a l c u l a

    t o r . j s C a l c u l a t o r = { a d d : f u n c t i o n ( a , b ) { r e t u r n a + b ; } } m o d u l e . e x p o r t s = C a l c u l a t o r / / t e s t . j s v a r C a l c = r e q u i r e ( " . / c a l c u l a t o r " ) ; v a r a s s e r t = r e q u i r e ( " c h a i " ) . a s s e r t s u i t e ( " a d d " , f u n c t i o n ( ) { t e s t ( " t w o p o s i t i v e " , f u n c t i o n ( ) { a s s e r t ( C a l c . a d d ( 1 , 3 ) , 4 ) ; } ) ; t e s t ( " p o s i t i v e a n d n e g a i v e " , f u n c t i o n ( ) { a s s e r t ( C a l c . a d d ( - 5 , 4 ) , 1 ) ; } ) } ) ;
  18. EXECUTE IN CLI mocha --ui=tdd ./test-tdd.js mocha --ui=tdd --watch ./test-tdd.js

    mocha --ui=tdd --grep="positive" ./test-tdd.js
  19. None
  20. BROWSER bower install mocha bower install chai

  21. BROWSER-TEST C a l c = { a d d

    : f u n c t i o n ( a , b ) { r e t u r n a + b ; } } m o c h a . s e t u p ( ' t d d ' ) ; v a r a s s e r t = c h a i . a s s e r t ; s u i t e ( " a d d " , f u n c t i o n ( ) { t e s t ( " t w o p o s i t i v e " , f u n c t i o n ( ) { a s s e r t ( C a l c . a d d ( 1 , 3 ) , 4 ) ; } ) ; t e s t ( " p o s i t i v e a n d n e g a i v e " , f u n c t i o n ( ) { a s s e r t ( C a l c . a d d ( - 5 , 4 ) , 1 ) ; } ) } ) ; m o c h a . r u n ( ) ;
  22. None
  23. ASYNCHRONOUS C a l c = { a d d

    : f u n c t i o n ( a , b , f n ) { f n ( a + b ) ; } } s u i t e ( " a d d " , f u n c t i o n ( ) { t e s t ( " t w o p o s i t i v e " , f u n c t i o n ( d o n e ) { C a l c . a d d ( 1 , 3 , f u n c t i o n ( r e s u l t ) { a s s e r t ( r e s u l t , 4 ) ; d o n e ( ) ; } ) } ) ; } ) ;
  24. PENDING TESTS Mark as pending test without callback s u

    i t e ( " a d d " , f u n c t i o n ( ) { t e s t ( " t w o p o s i t i v e " ) ; } ) ;
  25. EXCLUSIVE TESTS Run only specific test/suite similar to grep s

    u i t e ( " a d d " , f u n c t i o n ( ) { t e s t . o n l y ( " t w o p o s i t i v e " , f u n c t i o n ( ) { a s s e r t ( C a l c . a d d ( 1 , 3 ) , 4 ) ; } ) ; t e s t ( " p o s i t i v e a n d n e g a i v e " , f u n c t i o n ( d o n e ) { a s s e r t ( C a l c . a d d ( - 5 , 4 ) , - 1 ) ; } ) } ) ;
  26. SKIP TESTS s u i t e ( " a

    d d " , f u n c t i o n ( ) { t e s t . s k i p ( " t w o p o s i t i v e " , f u n c t i o n ( ) { a s s e r t ( C a l c . a d d ( 1 , 3 ) , 4 ) ; } ) ; t e s t ( " p o s i t i v e a n d n e g a i v e " , f u n c t i o n ( d o n e ) { a s s e r t ( C a l c . a d d ( - 5 , 4 ) , - 1 ) ; } ) } ) ;
  27. USING GRUNT npm install -g grunt-cli npm install grunt --save-dev

    npm install grunt-mocha --save-dev
  28. GRUNT CONFIG m o d u l e . e

    x p o r t s = f u n c t i o n ( g r u n t ) { g r u n t . l o a d N p m T a s k s ( ' g r u n t - m o c h a ' ) ; g r u n t . i n i t C o n f i g ( { m o c h a : { t e s t : { s r c : [ ' t e s t s / * * / * . h t m l ' ] , } , } } ) ; }
  29. USING GULP npm install -g gulp npm install --save-dev gulp-mocha

  30. GULP CONFIG v a r g u l p =

    r e q u i r e ( ' g u l p ' ) ; v a r m o c h a = r e q u i r e ( ' g u l p - m o c h a ' ) ; g u l p . t a s k ( ' d e f a u l t ' , f u n c t i o n ( ) { r e t u r n g u l p . s r c ( ' t e s t . j s ' , { u i : t d d } ) . p i p e ( m o c h a ( ) ) ; } ) ;
  31. REFERENCES Mocha : Chai.js : Grunt : grunt-mocha : Gulp

    : gulp-mocha : Testing AJAX : code : slides : https://visionmedia.github.io/mocha/ http://chaijs.com/ http://gruntjs.com/ https://github.com/kmiyashiro/grunt-mocha http://gulpjs.com/ https://github.com/sindresorhus/gulp-mocha http://blog.revathskumar.com/2013/03/testing-jquery-ajax- with-mocha-and-sinon.html http://jsbin.com/veqop/6/edit https://speakerdeck.com/revathskumar/testing-with- mocha
  32. QUESTIONS? Revath S Kumar @revathskumar http://blog.revathskumar.com