Quick tour to front end unit testing using jasmine

Gil Fink
Gil FinkFounder and Owner at sparXys
Quick Tour to Front-End Unit Testing Using 
Jasmine 
Gil Fink 
Join the conversation on Twitter: @SoftArchConf #SA2014
Agenda 
• Why Unit Testing? 
• Behavior Driven Development 
• Unit Testing in JavaScript? 
• Jasmine 
• Jasmine and Karma
Life Without Unit Testing
Why Unit Testing? 
• Tests 
– Act as safety net when you modify your code 
• Increase your confidence in your code 
– Encourage good design 
– Help to detect bugs in early stages of the project 
– Serve as live documentation
Behavior Driven Development 
• Development approach based on test driven 
development (TDD) 
• Combines the good parts of different 
development approaches 
– Test driven development (TDD) 
– Domain driven development (DDD) 
– Object oriented analysis (OOA) 
– Object oriented design (OOD) 
– The combination of approaches isn’t mandatory
Behavior Driven Development – 
Cont. 
• BDD specifies that tests should be created for 
the requirements (behaviors) 
– Set by the business owners 
• Developers and business owners should 
collaborate to specify the requirements 
• The output of the tests should be readable 
– For to the developer 
– For the business owners
BDD and TDD
BDD Language 
• In BDD you 
– describe your code 
– tell the test what it (the code) should do 
– expect your code to do something 
//suite 
describe ('', function(){ 
//test 
it ('', function(){ 
//expectation 
expect(); 
)}; 
});
Unit Testing in JavaScript? 
• JavaScript is everywhere 
– Browsers 
– Servers 
– Operation Systems 
– Databases 
– Mobile 
– Devices 
• You are doing it in any other language…
Jasmine 
• JavaScript BDD framework 
• Can be downloaded from: 
http://jasmine.github.io/
Setting Up The Environment 
• Download Jasmine 
– or use a package manager such as Bower or Nuget 
• Create a Spec (Test) Runner file 
– Responsible to run all the unit tests 
– Runs in the browser 
• Create the Unit Test files 
• Jasmine can also run headless 
– Without a browser context
Demo 
SETTING THE ENVIRONMENT
Suggested Folder Structure 
js 
|--src 
|--tests 
| |--spec 
|--vendor 
| |--jasmine 
SpecRunner.html
Jasmine Tests Suites 
• First create a Suite which is a Specs container 
– Use the describe function 
– Typically a single suite should be written for each 
JavaScript file 
describe("Suite Name", function() { 
// Put here your tests 
});
Jasmine Tests 
• A Spec (Test) is defined by calling the it 
function 
– Receives a spec name and a spec callback function 
– Contains expectations that test the state of the 
code 
de scribe("Suite Name", function() { 
it("a spec with one expectation", function() { 
// create the spec body 
}); 
});
Expectations 
• Expectations are assertions 
– Can be either true or false 
• Use the expect function within a spec to 
declare an expectation 
– Receives the actual value as parameter 
• Include fluent API for matchers 
– A Matcher is a comparison between the actual 
and the expected values
Matchers Example 
it("matchers", function() { 
var a = 12, 
b = a, 
c = function () { 
}; 
expect(a).toBe(b); 
expect(a).not.toBe(null); 
expect(a).toEqual(12); 
expect(null).toBeNull(); 
expect(c).not.toThrow(); 
});
Demo 
CREATING SUITES AND SPECS
More on Suites and Specs 
• You can disable a test suite by using xdescribe 
• You can mark a spec as pending (not running) 
– Using xit 
– By calling the pending function 
xdescribe("A spec", function() { 
xit(“that is pending", function() { 
pending(); 
}); 
});
Creating a Custom Matcher 
• You can create your own custom matcher 
• The matcher should return true/false 
according to the actual value and the 
expectation you set
Setup and Teardown 
• Jasmine includes 
– beforeEach – runs before each test 
– afterEach – runs after each test 
• Should exists inside a test suite
Setup/Teardown Example 
describe("A suite", function() { 
var index = 0; 
beforeEach(function() { 
index += 1; 
}); 
afterEach(function() { 
index = 0; 
}); 
it("a spec", function() { 
expect(index).toEqual(1); 
}); 
});
Demo 
USING SETUP AND TEARDOWN
Nested describe Calls 
• Calls for describe can be nested 
– Good for creation of hierarchies 
• The beforeEach/afterEach of nested describe 
runs after a parent describe 
describe("A spec", function() { 
describe("nested inside a second describe", function() { 
}); 
});
Working with Spies 
• A spy is a test double object 
• It replaces the real implementation and fake 
its behavior 
• Use spyOn, createSpy and createSpyObj 
functions
Demo 
WORKING WITH SPIES
Jasmine Async Support 
• Jasmine enables to test async code 
• Calls to beforeEach, it, and afterEach take an 
additional done function 
beforeEach(function(done) { 
setTimeout(function() { 
value = 0; 
done(); 
}, 1); 
}); 
// spec will not start until the done in beforeEach is called 
it("should support async execution", function(done) { 
value++; 
expect(value).toBeGreaterThan(0); 
done(); 
});
Demo 
WORKING WITH ASYNC FUNCTIONS
Jasmine and Karma 
• Karma is a test runner for JavaScript 
– Executes JavaScript code in multiple browser 
instances 
– Makes BDD/TDD development easier 
– Can use any JavaScript testing library 
• In this session we will use Jasmine
Demo 
JASMINE AND KARMA
Questions?
Summary 
• Unit Tests are an important part of any 
development process 
• Jasmine library can help you to test your 
JavaScript code 
• Add tests to your JavaScript code!
Resources 
• Session slide deck – 
• Jasmine – http://jasmine.github.io/ 
• My Website – http://www.gilfink.net 
• Follow me on Twitter – @gilfink
Thank You
1 of 34

Recommended

Unit testing JavaScript: Jasmine & karma intro by
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introMaurice De Beijer [MVP]
2.9K views17 slides
Unit testing of java script and angularjs application using Karma Jasmine Fra... by
Unit testing of java script and angularjs application using Karma Jasmine Fra...Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...Samyak Bhalerao
1.1K views17 slides
Angularjs - Unit testing introduction by
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introductionNir Kaufman
9.7K views34 slides
Angular Unit Testing by
Angular Unit TestingAngular Unit Testing
Angular Unit TestingAvi Engelshtein
663 views12 slides
Angular Unit Testing by
Angular Unit TestingAngular Unit Testing
Angular Unit TestingAlessandro Giorgetti
2.7K views71 slides
Karma - JS Test Runner by
Karma - JS Test RunnerKarma - JS Test Runner
Karma - JS Test RunnerSebastiano Armeli
10.9K views34 slides

More Related Content

What's hot

Quick Tour to Front-End Unit Testing Using Jasmine by
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
1.1K views28 slides
RSpec and Rails by
RSpec and RailsRSpec and Rails
RSpec and RailsAlan Hecht
2.8K views27 slides
Ruby onrails cucumber-rspec-capybara by
Ruby onrails cucumber-rspec-capybaraRuby onrails cucumber-rspec-capybara
Ruby onrails cucumber-rspec-capybaraBindesh Vijayan
3K views18 slides
Karate - Web-Service API Testing Made Simple by
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleVodqaBLR
3.2K views22 slides
Test automation with cucumber jvm by
Test automation with cucumber jvmTest automation with cucumber jvm
Test automation with cucumber jvmNibu Baby
813 views13 slides
Angular Unit Testing NDC Minn 2018 by
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018Justin James
240 views256 slides

What's hot(20)

Quick Tour to Front-End Unit Testing Using Jasmine by Gil Fink
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
Gil Fink1.1K views
RSpec and Rails by Alan Hecht
RSpec and RailsRSpec and Rails
RSpec and Rails
Alan Hecht2.8K views
Ruby onrails cucumber-rspec-capybara by Bindesh Vijayan
Ruby onrails cucumber-rspec-capybaraRuby onrails cucumber-rspec-capybara
Ruby onrails cucumber-rspec-capybara
Bindesh Vijayan3K views
Karate - Web-Service API Testing Made Simple by VodqaBLR
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
VodqaBLR3.2K views
Test automation with cucumber jvm by Nibu Baby
Test automation with cucumber jvmTest automation with cucumber jvm
Test automation with cucumber jvm
Nibu Baby813 views
Angular Unit Testing NDC Minn 2018 by Justin James
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018
Justin James240 views
Karate for Complex Web-Service API Testing by Peter Thomas by intuit_india
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
intuit_india6.2K views
Angular Unit Testing from the Trenches by Justin James
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the Trenches
Justin James1.1K views
Karate - powerful and simple framework for REST API automation testing by Roman Liubun
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testing
Roman Liubun5.4K views
Testing Legacy Rails Apps by Rabble .
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
Rabble .4.7K views
Improve your development skills with Test Driven Development by John Stevenson
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
John Stevenson1.6K views
Karate - MoT Dallas 26-Oct-2017 by Peter Thomas
Karate - MoT Dallas 26-Oct-2017Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017
Peter Thomas1.5K views
Automate right start from API by Roman Liubun
Automate right start from APIAutomate right start from API
Automate right start from API
Roman Liubun277 views
Rspec and Capybara Intro Tutorial at RailsConf 2013 by Brian Sam-Bodden
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
Brian Sam-Bodden29.7K views
TypeScript for Java Developers by Yakov Fain
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
Yakov Fain3.9K views

Similar to Quick tour to front end unit testing using jasmine

Quick tour to front end unit testing using jasmine by
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
3K views31 slides
Unit Testing and Coverage for AngularJS by
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSKnoldus Inc.
4.8K views22 slides
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit by
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF SummitOrtus Solutions, Corp
745 views87 slides
Unit testing in JavaScript with Jasmine and Karma by
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaAndrey Kolodnitsky
3.3K views22 slides
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017 by
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
382 views42 slides
3 WAYS TO TEST YOUR COLDFUSION API - by
3 WAYS TO TEST YOUR COLDFUSION API - 3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - Ortus Solutions, Corp
1K views86 slides

Similar to Quick tour to front end unit testing using jasmine(20)

Quick tour to front end unit testing using jasmine by Gil Fink
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
Gil Fink3K views
Unit Testing and Coverage for AngularJS by Knoldus Inc.
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
Knoldus Inc.4.8K views
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit by Ortus Solutions, Corp
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Unit testing in JavaScript with Jasmine and Karma by Andrey Kolodnitsky
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky3.3K views
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017 by Ortus Solutions, Corp
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
3 WAYS TO TEST YOUR COLDFUSION API by Gavin Pickin
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin859 views
Understanding JavaScript Testing by Kissy Team
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
Kissy Team1.4K views
Useful practices of creation automatic tests by using cucumber jvm by Anton Shapin
Useful practices of creation automatic tests by using cucumber jvmUseful practices of creation automatic tests by using cucumber jvm
Useful practices of creation automatic tests by using cucumber jvm
Anton Shapin793 views
Test Driven Development with JavaFX by Hendrik Ebbers
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
Hendrik Ebbers16.5K views
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage by mlilley
Testing NodeJS with Mocha, Should, Sinon, and JSCoverageTesting NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
mlilley20.5K views
Intro To JavaScript Unit Testing - Ran Mizrahi by Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi2.4K views
Describe's Full of It's by Jim Lynch
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
Jim Lynch296 views
Java script unit testing by Mats Bryntse
Java script unit testingJava script unit testing
Java script unit testing
Mats Bryntse3K views
Angular Intermediate by LinkMe Srl
Angular IntermediateAngular Intermediate
Angular Intermediate
LinkMe Srl1.3K views

More from Gil Fink

Becoming a Tech Speaker by
Becoming a Tech SpeakerBecoming a Tech Speaker
Becoming a Tech SpeakerGil Fink
203 views18 slides
Web animation on steroids web animation api by
Web animation on steroids web animation api Web animation on steroids web animation api
Web animation on steroids web animation api Gil Fink
312 views5 slides
The Time for Vanilla Web Components has Arrived by
The Time for Vanilla Web Components has ArrivedThe Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has ArrivedGil Fink
726 views51 slides
Stencil the time for vanilla web components has arrived by
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedGil Fink
278 views46 slides
Stencil the time for vanilla web components has arrived by
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedGil Fink
284 views51 slides
Stencil: The Time for Vanilla Web Components has Arrived by
Stencil: The Time for Vanilla Web Components has ArrivedStencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has ArrivedGil Fink
310 views50 slides

More from Gil Fink(20)

Becoming a Tech Speaker by Gil Fink
Becoming a Tech SpeakerBecoming a Tech Speaker
Becoming a Tech Speaker
Gil Fink203 views
Web animation on steroids web animation api by Gil Fink
Web animation on steroids web animation api Web animation on steroids web animation api
Web animation on steroids web animation api
Gil Fink312 views
The Time for Vanilla Web Components has Arrived by Gil Fink
The Time for Vanilla Web Components has ArrivedThe Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has Arrived
Gil Fink726 views
Stencil the time for vanilla web components has arrived by Gil Fink
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink278 views
Stencil the time for vanilla web components has arrived by Gil Fink
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink284 views
Stencil: The Time for Vanilla Web Components has Arrived by Gil Fink
Stencil: The Time for Vanilla Web Components has ArrivedStencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has Arrived
Gil Fink310 views
Stencil the time for vanilla web components has arrived by Gil Fink
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink290 views
Being a tech speaker by Gil Fink
Being a tech speakerBeing a tech speaker
Being a tech speaker
Gil Fink355 views
Working with Data in Service Workers by Gil Fink
Working with Data in Service WorkersWorking with Data in Service Workers
Working with Data in Service Workers
Gil Fink2.2K views
Demystifying Angular Animations by Gil Fink
Demystifying Angular AnimationsDemystifying Angular Animations
Demystifying Angular Animations
Gil Fink1.2K views
Redux data flow with angular by Gil Fink
Redux data flow with angularRedux data flow with angular
Redux data flow with angular
Gil Fink787 views
Redux data flow with angular by Gil Fink
Redux data flow with angularRedux data flow with angular
Redux data flow with angular
Gil Fink352 views
Who's afraid of front end databases? by Gil Fink
Who's afraid of front end databases?Who's afraid of front end databases?
Who's afraid of front end databases?
Gil Fink345 views
One language to rule them all type script by Gil Fink
One language to rule them all type scriptOne language to rule them all type script
One language to rule them all type script
Gil Fink286 views
End to-end apps with type script by Gil Fink
End to-end apps with type scriptEnd to-end apps with type script
End to-end apps with type script
Gil Fink317 views
Web component driven development by Gil Fink
Web component driven developmentWeb component driven development
Web component driven development
Gil Fink1.2K views
Web components by Gil Fink
Web componentsWeb components
Web components
Gil Fink517 views
Redux data flow with angular 2 by Gil Fink
Redux data flow with angular 2Redux data flow with angular 2
Redux data flow with angular 2
Gil Fink1.8K views
Biological Modeling, Powered by AngularJS by Gil Fink
Biological Modeling, Powered by AngularJSBiological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
Gil Fink301 views
Who's afraid of front end databases by Gil Fink
Who's afraid of front end databasesWho's afraid of front end databases
Who's afraid of front end databases
Gil Fink330 views

Recently uploaded

Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...ShapeBlue
105 views15 slides
Future of AR - Facebook Presentation by
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook PresentationRob McCarty
54 views27 slides
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...ShapeBlue
86 views25 slides
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or... by
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...ShapeBlue
128 views20 slides
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...ShapeBlue
113 views18 slides
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueShapeBlue
134 views54 slides

Recently uploaded(20)

Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by ShapeBlue
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
ShapeBlue105 views
Future of AR - Facebook Presentation by Rob McCarty
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
Rob McCarty54 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue86 views
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or... by ShapeBlue
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
ShapeBlue128 views
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue113 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue134 views
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates by ShapeBlue
Keynote Talk: Open Source is Not Dead - Charles Schulz - VatesKeynote Talk: Open Source is Not Dead - Charles Schulz - Vates
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates
ShapeBlue178 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker50 views
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by ShapeBlue
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
ShapeBlue120 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software373 views
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely76 views
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O... by ShapeBlue
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
ShapeBlue59 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson142 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue68 views
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool by ShapeBlue
Extending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPoolExtending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPool
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool
ShapeBlue56 views
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by ShapeBlue
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
ShapeBlue52 views
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays49 views
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
ShapeBlue147 views

Quick tour to front end unit testing using jasmine

  • 1. Quick Tour to Front-End Unit Testing Using Jasmine Gil Fink Join the conversation on Twitter: @SoftArchConf #SA2014
  • 2. Agenda • Why Unit Testing? • Behavior Driven Development • Unit Testing in JavaScript? • Jasmine • Jasmine and Karma
  • 4. Why Unit Testing? • Tests – Act as safety net when you modify your code • Increase your confidence in your code – Encourage good design – Help to detect bugs in early stages of the project – Serve as live documentation
  • 5. Behavior Driven Development • Development approach based on test driven development (TDD) • Combines the good parts of different development approaches – Test driven development (TDD) – Domain driven development (DDD) – Object oriented analysis (OOA) – Object oriented design (OOD) – The combination of approaches isn’t mandatory
  • 6. Behavior Driven Development – Cont. • BDD specifies that tests should be created for the requirements (behaviors) – Set by the business owners • Developers and business owners should collaborate to specify the requirements • The output of the tests should be readable – For to the developer – For the business owners
  • 8. BDD Language • In BDD you – describe your code – tell the test what it (the code) should do – expect your code to do something //suite describe ('', function(){ //test it ('', function(){ //expectation expect(); )}; });
  • 9. Unit Testing in JavaScript? • JavaScript is everywhere – Browsers – Servers – Operation Systems – Databases – Mobile – Devices • You are doing it in any other language…
  • 10. Jasmine • JavaScript BDD framework • Can be downloaded from: http://jasmine.github.io/
  • 11. Setting Up The Environment • Download Jasmine – or use a package manager such as Bower or Nuget • Create a Spec (Test) Runner file – Responsible to run all the unit tests – Runs in the browser • Create the Unit Test files • Jasmine can also run headless – Without a browser context
  • 12. Demo SETTING THE ENVIRONMENT
  • 13. Suggested Folder Structure js |--src |--tests | |--spec |--vendor | |--jasmine SpecRunner.html
  • 14. Jasmine Tests Suites • First create a Suite which is a Specs container – Use the describe function – Typically a single suite should be written for each JavaScript file describe("Suite Name", function() { // Put here your tests });
  • 15. Jasmine Tests • A Spec (Test) is defined by calling the it function – Receives a spec name and a spec callback function – Contains expectations that test the state of the code de scribe("Suite Name", function() { it("a spec with one expectation", function() { // create the spec body }); });
  • 16. Expectations • Expectations are assertions – Can be either true or false • Use the expect function within a spec to declare an expectation – Receives the actual value as parameter • Include fluent API for matchers – A Matcher is a comparison between the actual and the expected values
  • 17. Matchers Example it("matchers", function() { var a = 12, b = a, c = function () { }; expect(a).toBe(b); expect(a).not.toBe(null); expect(a).toEqual(12); expect(null).toBeNull(); expect(c).not.toThrow(); });
  • 18. Demo CREATING SUITES AND SPECS
  • 19. More on Suites and Specs • You can disable a test suite by using xdescribe • You can mark a spec as pending (not running) – Using xit – By calling the pending function xdescribe("A spec", function() { xit(“that is pending", function() { pending(); }); });
  • 20. Creating a Custom Matcher • You can create your own custom matcher • The matcher should return true/false according to the actual value and the expectation you set
  • 21. Setup and Teardown • Jasmine includes – beforeEach – runs before each test – afterEach – runs after each test • Should exists inside a test suite
  • 22. Setup/Teardown Example describe("A suite", function() { var index = 0; beforeEach(function() { index += 1; }); afterEach(function() { index = 0; }); it("a spec", function() { expect(index).toEqual(1); }); });
  • 23. Demo USING SETUP AND TEARDOWN
  • 24. Nested describe Calls • Calls for describe can be nested – Good for creation of hierarchies • The beforeEach/afterEach of nested describe runs after a parent describe describe("A spec", function() { describe("nested inside a second describe", function() { }); });
  • 25. Working with Spies • A spy is a test double object • It replaces the real implementation and fake its behavior • Use spyOn, createSpy and createSpyObj functions
  • 27. Jasmine Async Support • Jasmine enables to test async code • Calls to beforeEach, it, and afterEach take an additional done function beforeEach(function(done) { setTimeout(function() { value = 0; done(); }, 1); }); // spec will not start until the done in beforeEach is called it("should support async execution", function(done) { value++; expect(value).toBeGreaterThan(0); done(); });
  • 28. Demo WORKING WITH ASYNC FUNCTIONS
  • 29. Jasmine and Karma • Karma is a test runner for JavaScript – Executes JavaScript code in multiple browser instances – Makes BDD/TDD development easier – Can use any JavaScript testing library • In this session we will use Jasmine
  • 32. Summary • Unit Tests are an important part of any development process • Jasmine library can help you to test your JavaScript code • Add tests to your JavaScript code!
  • 33. Resources • Session slide deck – • Jasmine – http://jasmine.github.io/ • My Website – http://www.gilfink.net • Follow me on Twitter – @gilfink