Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing with mocha

Revath S Kumar
September 27, 2014

Testing with mocha

Testing with mocha and chaijs

Revath S Kumar

September 27, 2014
Tweet

More Decks by Revath S Kumar

Other Decks in Programming

Transcript

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

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

    code Testing in Node.js and Browser Grunt and gulp tasks
  3. 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
  4. 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 } ) ; } ) ; } ) ;
  5. 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 } ) ; } ) ;
  6. 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 } ) ; } ) ; } ) ;
  7. 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
  8. 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 ) ; } ) } ) ;
  9. 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 ( ) ;
  10. 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 ( ) ; } ) } ) ; } ) ;
  11. 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 " ) ; } ) ;
  12. 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 ) ; } ) } ) ;
  13. 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 ) ; } ) } ) ;
  14. 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 ' ] , } , } } ) ; }
  15. 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 ( ) ) ; } ) ;
  16. 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