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.
TESTING WITH MOCHA 
@revathskumar
ABOUT 
Rubyist / JavaScripter 
Yeoman Team Member / @keralarb / 
@keralajs 
Works at 
Blog at 
Twitter/Github - 
Google+: ...
AGENDA 
Mocha Features 
BDD/TDD syntax 
Chai assert library 
Testing asynchronous code 
Testing in Node.js and Browser 
Gr...
NEVER TRUST YOUR CODE
TESTING FRAMEWORKS 
Mocha 
Jasmine 
QUnit
MOCHA
MOCHA: FEATURES 
Runs on Node.js/Browser 
Supports BDD/TDD 
Choose any assertion library 
Choose any Mocking library 
Asyn...
MOCHA: TDD 
suite('Calculator', function () { 
suite('Add', function(){ 
test("using both positive numbers", function(){ 
...
MOCHA: SETUP & TEARDOWN 
suite('Calculator', function () { 
setup(function(){ 
// runs before test 
}); 
//test("add using...
MOCHA: BDD 
describe('Calculator', function () { 
describe('add', function () { 
it('using both positive numbers',function...
MOCHA: BEFORE & AFTER 
describe('Calculator', function () { 
before(function(){ 
}) 
after(function(){ 
}) 
}); 
beforeEac...
CHAI.JS 
chai.assert 
chai.expect 
chai.should
CHAI: ASSERT 
chai.assert(expression, [message]) 
chai.assert(actual, expected, [message]) 
ok() 
notOk() 
fail() 
isTrue(...
CHAI: EXPECT 
chai.expect(expected).to.not.equal(actual); 
chai.expect(expected).to.be.a('string'); 
ok 
include() 
true 
...
CHAI: SHOULD 
value.should.be.an('object'); 
value.should.equal(10); 
value.should.be.a('string') 
object.should.have.prop...
NODE.JS 
npm install -g mocha 
npm install -g chai
NODE-TEST 
// calculator.js 
Calculator = { 
add: function(a, b) { 
return a + b; 
} 
} 
module.exports = Calculator 
// t...
EXECUTE IN CLI 
mocha --ui=tdd ./test-tdd.js 
mocha --ui=tdd --watch ./test-tdd.js 
mocha --ui=tdd --grep="positive" ./tes...
BROWSER 
bower install mocha 
bower install chai
BROWSER-TEST 
Calc = { 
add: function(a, b){ 
return a + b; 
} 
} 
mocha.setup('tdd'); 
var assert = chai.assert; 
suite("...
ASYNCHRONOUS 
Calc = { 
add: function(a, b, fn){ 
fn(a + b); 
} 
} 
suite("add", function(){ 
test("two positive", functio...
PENDING TESTS 
Mark as pending 
test without callback 
suite("add", function(){ 
test("two positive"); 
});
EXCLUSIVE TESTS 
Run only specific test/suite 
similar to grep 
suite("add", function(){ 
test.only("two positive", functi...
SKIP TESTS 
suite("add", function(){ 
test.skip("two positive", function(){ 
assert(Calc.add(1,3), 4); 
}); 
test("positiv...
USING GRUNT 
npm install -g grunt-cli 
npm install grunt --save-dev 
npm install grunt-mocha --save-dev
GRUNT CONFIG 
module.exports = function(grunt) { 
grunt.loadNpmTasks('grunt-mocha'); 
grunt.initConfig({ 
mocha: { 
test: ...
USING GULP 
npm install -g gulp 
npm install --save-dev gulp-mocha
GULP CONFIG 
var gulp = require('gulp'); 
var mocha = require('gulp-mocha'); 
gulp.task('default', function () { 
return g...
REFERENCES 
Mocha : 
Chai.js : 
Grunt : 
grunt-mocha : 
Gulp : 
gulp-mocha : 
Testing AJAX : 
code : 
slides : 
https://vi...
QUESTIONS? 
Revath S Kumar 
@revathskumar 
http://blog.revathskumar.com
Unit testing with mocha
Unit testing with mocha
Upcoming SlideShare
Loading in …5
×

Unit testing with mocha

3,365 views

Published on

Helps to get started with testing in JavaScript using mocha.

Published in: Technology
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y6a5rkg5 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Unit testing with mocha

  1. 1. TESTING WITH MOCHA @revathskumar
  2. 2. ABOUT Rubyist / JavaScripter Yeoman Team Member / @keralarb / @keralajs Works at Blog at Twitter/Github - Google+: @whatznear blog.revathskumar.com @revathskumar +RevathSKumar
  3. 3. AGENDA Mocha Features BDD/TDD syntax Chai assert library Testing asynchronous code Testing in Node.js and Browser Grunt and gulp tasks
  4. 4. NEVER TRUST YOUR CODE
  5. 5. TESTING FRAMEWORKS Mocha Jasmine QUnit
  6. 6. MOCHA
  7. 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. 8. MOCHA: TDD suite('Calculator', function () { suite('Add', function(){ test("using both positive numbers", function(){ // test assertion }); test("using both negative numbers", function(){ // test assertion }); }); });
  9. 9. MOCHA: SETUP & TEARDOWN suite('Calculator', function () { setup(function(){ // runs before test }); //test("add using both positive numbers", function(){ // test assertion //}); teardown(function(){ // runs after tests }); });
  10. 10. MOCHA: BDD describe('Calculator', function () { describe('add', function () { it('using both positive numbers',function(){ // test assertion }); it('using both negative numbers',function(){ // test assertion }); }); });
  11. 11. MOCHA: BEFORE & AFTER describe('Calculator', function () { before(function(){ }) after(function(){ }) }); beforeEach afterEach
  12. 12. CHAI.JS chai.assert chai.expect chai.should
  13. 13. CHAI: ASSERT chai.assert(expression, [message]) chai.assert(actual, expected, [message]) ok() notOk() fail() isTrue() isFalse() isNUll()
  14. 14. CHAI: EXPECT chai.expect(expected).to.not.equal(actual); chai.expect(expected).to.be.a('string'); ok include() true false empty
  15. 15. CHAI: SHOULD value.should.be.an('object'); value.should.equal(10); value.should.be.a('string') object.should.have.property('key')
  16. 16. NODE.JS npm install -g mocha npm install -g chai
  17. 17. NODE-TEST // calculator.js Calculator = { add: function(a, b) { return a + b; } } module.exports = Calculator // test.js var Calc = require("./calculator"); var assert = require("chai").assert suite("add", function(){ test("two positive", function(){ assert(Calc.add(1,3), 4); }); test("positive and negaive", function(){ assert(Calc.add(-5,4), 1); }) });
  18. 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. 19. BROWSER bower install mocha bower install chai
  20. 20. BROWSER-TEST Calc = { add: function(a, b){ return a + b; } } mocha.setup('tdd'); var assert = chai.assert; suite("add", function(){ test("two positive", function(){ assert(Calc.add(1,3), 4); }); test("positive and negaive", function(){ assert(Calc.add(-5,4), 1); }) }); mocha.run();
  21. 21. ASYNCHRONOUS Calc = { add: function(a, b, fn){ fn(a + b); } } suite("add", function(){ test("two positive", function(done){ Calc.add(1,3, function(result){ assert(result, 4); done(); }) }); });
  22. 22. PENDING TESTS Mark as pending test without callback suite("add", function(){ test("two positive"); });
  23. 23. EXCLUSIVE TESTS Run only specific test/suite similar to grep suite("add", function(){ test.only("two positive", function(){ assert(Calc.add(1,3), 4); }); test("positive and negaive", function(done){ assert(Calc.add(-5, 4), -1); }) });
  24. 24. SKIP TESTS suite("add", function(){ test.skip("two positive", function(){ assert(Calc.add(1,3), 4); }); test("positive and negaive", function(done){ assert(Calc.add(-5, 4), -1); }) });
  25. 25. USING GRUNT npm install -g grunt-cli npm install grunt --save-dev npm install grunt-mocha --save-dev
  26. 26. GRUNT CONFIG module.exports = function(grunt) { grunt.loadNpmTasks('grunt-mocha'); grunt.initConfig({ mocha: { test: { src: ['tests/**/*.html'], }, } }); }
  27. 27. USING GULP npm install -g gulp npm install --save-dev gulp-mocha
  28. 28. GULP CONFIG var gulp = require('gulp'); var mocha = require('gulp-mocha'); gulp.task('default', function () { return gulp.src('test.js', {ui: tdd}) .pipe(mocha()); });
  29. 29. 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
  30. 30. QUESTIONS? Revath S Kumar @revathskumar http://blog.revathskumar.com

×