SlideShare a Scribd company logo
1 of 32
Download to read offline
TESTING WITH MOCHA 
@revathskumar
ABOUT 
Rubyist / JavaScripter 
Yeoman Team Member / @keralarb / 
@keralajs 
Works at 
Blog at 
Twitter/Github - 
Google+: 
@whatznear 
blog.revathskumar.com 
@revathskumar 
+RevathSKumar
AGENDA 
Mocha Features 
BDD/TDD syntax 
Chai assert library 
Testing asynchronous code 
Testing in Node.js and Browser 
Grunt and gulp tasks
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 
Async and promise support 
Highlights slow tests 
File watcher support 
Optionally run tests that match a regexp
MOCHA: TDD 
suite('Calculator', function () { 
suite('Add', function(){ 
test("using both positive numbers", function(){ 
// test assertion 
}); 
test("using both negative numbers", function(){ 
// test assertion 
}); 
}); 
});
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 
}); 
});
MOCHA: BDD 
describe('Calculator', function () { 
describe('add', function () { 
it('using both positive numbers',function(){ 
// test assertion 
}); 
it('using both negative numbers',function(){ 
// test assertion 
}); 
}); 
});
MOCHA: BEFORE & AFTER 
describe('Calculator', function () { 
before(function(){ 
}) 
after(function(){ 
}) 
}); 
beforeEach 
afterEach
CHAI.JS 
chai.assert 
chai.expect 
chai.should
CHAI: ASSERT 
chai.assert(expression, [message]) 
chai.assert(actual, expected, [message]) 
ok() 
notOk() 
fail() 
isTrue() 
isFalse() 
isNUll()
CHAI: EXPECT 
chai.expect(expected).to.not.equal(actual); 
chai.expect(expected).to.be.a('string'); 
ok 
include() 
true 
false 
empty
CHAI: SHOULD 
value.should.be.an('object'); 
value.should.equal(10); 
value.should.be.a('string') 
object.should.have.property('key')
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 
// 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); 
}) 
});
EXECUTE IN CLI 
mocha --ui=tdd ./test-tdd.js 
mocha --ui=tdd --watch ./test-tdd.js 
mocha --ui=tdd --grep="positive" ./test-tdd.js
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("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();
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(); 
}) 
}); 
});
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", function(){ 
assert(Calc.add(1,3), 4); 
}); 
test("positive and negaive", function(done){ 
assert(Calc.add(-5, 4), -1); 
}) 
});
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); 
}) 
});
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: { 
src: ['tests/**/*.html'], 
}, 
} 
}); 
}
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 gulp.src('test.js', {ui: tdd}) 
.pipe(mocha()); 
});
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
QUESTIONS? 
Revath S Kumar 
@revathskumar 
http://blog.revathskumar.com

More Related Content

What's hot

Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsYnon Perek
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineGil Fink
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmineTimothy Oxley
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Morris Singer
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaExoLeaders.com
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaChristopher Bartling
 
node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascriptEldar Djafarov
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaAndrey Kolodnitsky
 
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverageTesting NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoveragemlilley
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma Christopher Bartling
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasminefoxp2code
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsFITC
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with JestMichał Pierzchała
 
JavaScript Unit Testing with Jasmine
JavaScript Unit Testing with JasmineJavaScript Unit Testing with Jasmine
JavaScript Unit Testing with JasmineRaimonds Simanovskis
 
Adventures In JavaScript Testing
Adventures In JavaScript TestingAdventures In JavaScript Testing
Adventures In JavaScript TestingThomas Fuchs
 
Testing your javascript code with jasmine
Testing your javascript code with jasmineTesting your javascript code with jasmine
Testing your javascript code with jasmineRubyc Slides
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to ProtractorJie-Wei Wu
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introductionNir Kaufman
 
Testing javascript in the frontend
Testing javascript in the frontendTesting javascript in the frontend
Testing javascript in the frontendFrederic CABASSUT
 

What's hot (20)

Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript Applications
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
 
Full Stack Unit Testing
Full Stack Unit TestingFull Stack Unit Testing
Full Stack Unit Testing
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascript
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
 
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverageTesting NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
JavaScript Unit Testing with Jasmine
JavaScript Unit Testing with JasmineJavaScript Unit Testing with Jasmine
JavaScript Unit Testing with Jasmine
 
Adventures In JavaScript Testing
Adventures In JavaScript TestingAdventures In JavaScript Testing
Adventures In JavaScript Testing
 
Testing your javascript code with jasmine
Testing your javascript code with jasmineTesting your javascript code with jasmine
Testing your javascript code with jasmine
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to Protractor
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
 
Testing javascript in the frontend
Testing javascript in the frontendTesting javascript in the frontend
Testing javascript in the frontend
 

Similar to Unit testing with mocha

Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4jeresig
 
Stop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptStop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptRyan Anklam
 
An Introduction to AngularJs Unittesting
An Introduction to AngularJs UnittestingAn Introduction to AngularJs Unittesting
An Introduction to AngularJs UnittestingInthra onsap
 
Unit Testing Front End JavaScript
Unit Testing Front End JavaScriptUnit Testing Front End JavaScript
Unit Testing Front End JavaScriptFITC
 
Automated javascript unit testing
Automated javascript unit testingAutomated javascript unit testing
Automated javascript unit testingryan_chambers
 
Build Lightweight Web Module
Build Lightweight Web ModuleBuild Lightweight Web Module
Build Lightweight Web ModuleMorgan Cheng
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorNeeraj Kaushik
 
2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good TestsTomek Kaczanowski
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceTobie Langel
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using JasmineQuick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using JasmineGil Fink
 
javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closureHika Maeng
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014hezamu
 
Building Smart Async Functions For Mobile
Building Smart Async Functions For MobileBuilding Smart Async Functions For Mobile
Building Smart Async Functions For MobileGlan Thomas
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best PracticesJitendra Zaa
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 

Similar to Unit testing with mocha (20)

Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Stop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptStop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScript
 
An Introduction to AngularJs Unittesting
An Introduction to AngularJs UnittestingAn Introduction to AngularJs Unittesting
An Introduction to AngularJs Unittesting
 
Unit Testing Front End JavaScript
Unit Testing Front End JavaScriptUnit Testing Front End JavaScript
Unit Testing Front End JavaScript
 
Automated javascript unit testing
Automated javascript unit testingAutomated javascript unit testing
Automated javascript unit testing
 
Build Lightweight Web Module
Build Lightweight Web ModuleBuild Lightweight Web Module
Build Lightweight Web Module
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression Calculator
 
2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with Evidence
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using JasmineQuick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
 
javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closure
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014
 
Building Smart Async Functions For Mobile
Building Smart Async Functions For MobileBuilding Smart Async Functions For Mobile
Building Smart Async Functions For Mobile
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best Practices
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 

More from Revath S Kumar

More from Revath S Kumar (11)

Rack
RackRack
Rack
 
Meetups
MeetupsMeetups
Meetups
 
gulp
gulpgulp
gulp
 
VCR
VCRVCR
VCR
 
Modern frontend workflow
Modern frontend workflowModern frontend workflow
Modern frontend workflow
 
Web components
Web componentsWeb components
Web components
 
Setup nodejs
Setup nodejsSetup nodejs
Setup nodejs
 
Side projects : why it fails
Side projects : why it failsSide projects : why it fails
Side projects : why it fails
 
My webapp workflow
My webapp workflowMy webapp workflow
My webapp workflow
 
Promises in JavaScript
Promises in JavaScriptPromises in JavaScript
Promises in JavaScript
 
Prateek dayal backbonerails-110528024926-phpapp02
Prateek dayal backbonerails-110528024926-phpapp02Prateek dayal backbonerails-110528024926-phpapp02
Prateek dayal backbonerails-110528024926-phpapp02
 

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Unit testing with mocha

  • 1. TESTING WITH MOCHA @revathskumar
  • 2. ABOUT Rubyist / JavaScripter Yeoman Team Member / @keralarb / @keralajs Works at Blog at Twitter/Github - Google+: @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
  • 5. TESTING FRAMEWORKS Mocha Jasmine QUnit
  • 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 suite('Calculator', function () { suite('Add', function(){ test("using both positive numbers", function(){ // test assertion }); test("using both negative numbers", function(){ // test assertion }); }); });
  • 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. 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. MOCHA: BEFORE & AFTER describe('Calculator', function () { before(function(){ }) after(function(){ }) }); beforeEach afterEach
  • 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 // 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. 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.
  • 20. BROWSER bower install mocha bower install chai
  • 21. 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();
  • 22.
  • 23. 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(); }) }); });
  • 24. PENDING TESTS Mark as pending test without callback suite("add", function(){ test("two positive"); });
  • 25. 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); }) });
  • 26. 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); }) });
  • 27. USING GRUNT npm install -g grunt-cli npm install grunt --save-dev npm install grunt-mocha --save-dev
  • 28. GRUNT CONFIG module.exports = function(grunt) { grunt.loadNpmTasks('grunt-mocha'); grunt.initConfig({ mocha: { test: { src: ['tests/**/*.html'], }, } }); }
  • 29. USING GULP npm install -g gulp npm install --save-dev gulp-mocha
  • 30. GULP CONFIG var gulp = require('gulp'); var mocha = require('gulp-mocha'); gulp.task('default', function () { return gulp.src('test.js', {ui: tdd}) .pipe(mocha()); });
  • 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