SlideShare a Scribd company logo
Quick Tour to Front-End Unit 
Testing Using Jasmine 
#devconnections 
Gil Fink 
Senior Consultant 
sparXys
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Agenda 
• Why Unit Testing? 
• Unit Testing in JavaScript? 
• Behavior Driven Development 
• Jasmine 
• Jasmine and Karma
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Why Unit Testing?
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Behavior Driven Development 
• 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(); 
)}; 
});
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
BDD and TDD
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Unit Testing in JavaScript? 
• JavaScript is everywhere 
– Browsers 
– Servers 
– Operation Systems 
– Databases 
– Mobile 
– Devices 
• You are doing it in any other language…
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Jasmine 
• A JavaScript BDD framework 
• Can be downloaded from: 
http://jasmine.github.io/
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Demo 
SETTING THE ENVIRONMENT
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Suggested Folder Structure 
js 
|--tests 
| |--spec 
|--vendor 
| |--Jasmine 
SpecRunner.html 
#devconnections
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Jasmine Tests Suites 
• First create a Suite which is a container 
of Specs 
– Use the describe function 
– Typically a single suite should be written 
for each JavaScript file 
describe("Suite Name", function() { 
// Put here your tests 
});
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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 
describe("Suite Name", function() { 
it("a spec with one expectation", function() { 
– of the code 
// create the spec body 
}); 
});
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Matchers Example 
it("matchers", function() { 
var a = 12; 
var b = a; 
var c = function () { 
} 
expect(a).toBe(b); 
expect(a).not.toBe(null); 
expect(a).toEqual(12); 
expect(null).toBeNull(); 
expect(c).not.toThrow(); 
});
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Demo 
CREATING SUITES AND SPECS
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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(); 
}); 
}); 
#devconnections
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Setup and Teardown 
• Jasmine includes 
– beforeEach – runs before each test 
– afterEach – runs after each test 
• Should exists inside a test suite
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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); 
}); 
});
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Demo 
USING SETUP AND TEARDOWN
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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() { 
}); 
}); 
#devconnections
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Demo 
WORKING WITH SPIES
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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(); 
});
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Demo 
WORKING WITH ASYNC 
FUNCTIONS
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
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
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Demo 
JASMINE AND KARMA
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
QUESTIONS?
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Summary 
• Unit Tests are an important part of any 
development process 
• Jasmine library can help you test your 
JavaScript code 
• Add tests to your JavaScript code!
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
Resources 
• Session slide deck – 
• Jasmine – http://jasmine.github.io/ 
• My Website – http://www.gilfink.net 
• Follow me on Twitter – @gilfink
QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 
THANK YOU

More Related Content

What's hot (20)

PDF
Jasmine BDD for Javascript
Luis Alfredo Porras Páez
 
PPTX
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
PDF
AngularJS Unit Test
Chiew Carol
 
ODP
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
PDF
Angular testing
Raissa Ferreira
 
PDF
Painless JavaScript Testing with Jest
Michał Pierzchała
 
PPTX
Javascript Testing with Jasmine 101
Roy Yu
 
PDF
Unit testing JavaScript using Mocha and Node
Josh Mock
 
PDF
Intro to Unit Testing in AngularJS
Jim Lynch
 
PDF
Test-Driven Development of AngularJS Applications
FITC
 
PDF
Unit testing with mocha
Revath S Kumar
 
PDF
Unit Testing Express Middleware
Morris Singer
 
PPT
Testing in AngularJS
Peter Drinnan
 
PDF
Karma - JS Test Runner
Sebastiano Armeli
 
PPTX
AngularJS Unit Testing
Prince Norin
 
ODP
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
PDF
Unit Testing Express and Koa Middleware in ES2015
Morris Singer
 
PPTX
Full Stack Unit Testing
GlobalLogic Ukraine
 
PPT
Testing Javascript with Jasmine
Tim Tyrrell
 
PDF
Jasmine - why JS tests don't smell fishy
Igor Napierala
 
Jasmine BDD for Javascript
Luis Alfredo Porras Páez
 
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
AngularJS Unit Test
Chiew Carol
 
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
Angular testing
Raissa Ferreira
 
Painless JavaScript Testing with Jest
Michał Pierzchała
 
Javascript Testing with Jasmine 101
Roy Yu
 
Unit testing JavaScript using Mocha and Node
Josh Mock
 
Intro to Unit Testing in AngularJS
Jim Lynch
 
Test-Driven Development of AngularJS Applications
FITC
 
Unit testing with mocha
Revath S Kumar
 
Unit Testing Express Middleware
Morris Singer
 
Testing in AngularJS
Peter Drinnan
 
Karma - JS Test Runner
Sebastiano Armeli
 
AngularJS Unit Testing
Prince Norin
 
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
Unit Testing Express and Koa Middleware in ES2015
Morris Singer
 
Full Stack Unit Testing
GlobalLogic Ukraine
 
Testing Javascript with Jasmine
Tim Tyrrell
 
Jasmine - why JS tests don't smell fishy
Igor Napierala
 

Viewers also liked (12)

PPT
Automated Testing With Jasmine, PhantomJS and Jenkins
Work at Play
 
PDF
Detangling Your JavaScript
Chris Powers
 
KEY
Jasmine
Chris Powers
 
KEY
Javascript Tests with Jasmine for Front-end Devs
Chris Powers
 
PPTX
PVS-Studio and static code analysis technique
Andrey Karpov
 
PDF
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
PPT
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
PPTX
Jasmine Sandler Client Case Studies- Digital Marketing Consultant - Social Me...
Jasmine Sandler
 
PDF
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
PPTX
jasmine
Asanka Indrajith
 
PPTX
Unit Testing Concepts and Best Practices
Derek Smith
 
PPTX
UNIT TESTING PPT
suhasreddy1
 
Automated Testing With Jasmine, PhantomJS and Jenkins
Work at Play
 
Detangling Your JavaScript
Chris Powers
 
Jasmine
Chris Powers
 
Javascript Tests with Jasmine for Front-end Devs
Chris Powers
 
PVS-Studio and static code analysis technique
Andrey Karpov
 
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
Jasmine Sandler Client Case Studies- Digital Marketing Consultant - Social Me...
Jasmine Sandler
 
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
Unit Testing Concepts and Best Practices
Derek Smith
 
UNIT TESTING PPT
suhasreddy1
 
Ad

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

PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
Front end unit testing using jasmine
Gil Fink
 
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
PDF
Automated Web Testing using JavaScript
Simon Guest
 
PPTX
Unit testing using jasmine in Javascript
Deepak More
 
PPTX
Jasmine Testing to the Rescue!
Christopher Steele
 
PPT
Jasmine presentation Selenium Camp 2013
dimakovalenko
 
PPTX
Testing JavaScript with Jasmine in Rails Applications
Hector Correa
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
PPTX
Java script unit testing
Mats Bryntse
 
PPTX
Test driven development with Jasmine
harshit040591
 
PDF
Unit Testing Lightning Components with Jasmine
Keir Bowden
 
PDF
How to write Testable Javascript
ColdFusionConference
 
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
PDF
Reliable Javascript
Glenn Stovall
 
PDF
What the HTML? - The Holy Grail
James Ford
 
PDF
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
PDF
Testacular
James Ford
 
PDF
How do I write Testable Javascript
ColdFusionConference
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Front end unit testing using jasmine
Gil Fink
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
Automated Web Testing using JavaScript
Simon Guest
 
Unit testing using jasmine in Javascript
Deepak More
 
Jasmine Testing to the Rescue!
Christopher Steele
 
Jasmine presentation Selenium Camp 2013
dimakovalenko
 
Testing JavaScript with Jasmine in Rails Applications
Hector Correa
 
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
Java script unit testing
Mats Bryntse
 
Test driven development with Jasmine
harshit040591
 
Unit Testing Lightning Components with Jasmine
Keir Bowden
 
How to write Testable Javascript
ColdFusionConference
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
Reliable Javascript
Glenn Stovall
 
What the HTML? - The Holy Grail
James Ford
 
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
Testacular
James Ford
 
How do I write Testable Javascript
ColdFusionConference
 
Ad

More from Gil Fink (20)

PDF
Becoming a Tech Speaker
Gil Fink
 
PPTX
Web animation on steroids web animation api
Gil Fink
 
PDF
The Time for Vanilla Web Components has Arrived
Gil Fink
 
PDF
Stencil the time for vanilla web components has arrived
Gil Fink
 
PDF
Stencil the time for vanilla web components has arrived
Gil Fink
 
PDF
Stencil: The Time for Vanilla Web Components has Arrived
Gil Fink
 
PDF
Stencil the time for vanilla web components has arrived
Gil Fink
 
PDF
Being a tech speaker
Gil Fink
 
PDF
Working with Data in Service Workers
Gil Fink
 
PDF
Demystifying Angular Animations
Gil Fink
 
PDF
Redux data flow with angular
Gil Fink
 
PDF
Redux data flow with angular
Gil Fink
 
PDF
Who's afraid of front end databases?
Gil Fink
 
PDF
One language to rule them all type script
Gil Fink
 
PDF
End to-end apps with type script
Gil Fink
 
PDF
Web component driven development
Gil Fink
 
PDF
Web components
Gil Fink
 
PDF
Redux data flow with angular 2
Gil Fink
 
PDF
Biological Modeling, Powered by AngularJS
Gil Fink
 
PDF
Who's afraid of front end databases
Gil Fink
 
Becoming a Tech Speaker
Gil Fink
 
Web animation on steroids web animation api
Gil Fink
 
The Time for Vanilla Web Components has Arrived
Gil Fink
 
Stencil the time for vanilla web components has arrived
Gil Fink
 
Stencil the time for vanilla web components has arrived
Gil Fink
 
Stencil: The Time for Vanilla Web Components has Arrived
Gil Fink
 
Stencil the time for vanilla web components has arrived
Gil Fink
 
Being a tech speaker
Gil Fink
 
Working with Data in Service Workers
Gil Fink
 
Demystifying Angular Animations
Gil Fink
 
Redux data flow with angular
Gil Fink
 
Redux data flow with angular
Gil Fink
 
Who's afraid of front end databases?
Gil Fink
 
One language to rule them all type script
Gil Fink
 
End to-end apps with type script
Gil Fink
 
Web component driven development
Gil Fink
 
Web components
Gil Fink
 
Redux data flow with angular 2
Gil Fink
 
Biological Modeling, Powered by AngularJS
Gil Fink
 
Who's afraid of front end databases
Gil Fink
 

Recently uploaded (20)

PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 

Quick tour to front end unit testing using jasmine

  • 1. Quick Tour to Front-End Unit Testing Using Jasmine #devconnections Gil Fink Senior Consultant sparXys
  • 2. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Agenda • Why Unit Testing? • Unit Testing in JavaScript? • Behavior Driven Development • Jasmine • Jasmine and Karma
  • 3. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Why Unit Testing?
  • 4. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Behavior Driven Development • 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(); )}; });
  • 6. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE BDD and TDD
  • 7. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Unit Testing in JavaScript? • JavaScript is everywhere – Browsers – Servers – Operation Systems – Databases – Mobile – Devices • You are doing it in any other language…
  • 8. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Jasmine • A JavaScript BDD framework • Can be downloaded from: http://jasmine.github.io/
  • 9. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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
  • 10. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Demo SETTING THE ENVIRONMENT
  • 11. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Suggested Folder Structure js |--tests | |--spec |--vendor | |--Jasmine SpecRunner.html #devconnections
  • 12. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Jasmine Tests Suites • First create a Suite which is a container of Specs – Use the describe function – Typically a single suite should be written for each JavaScript file describe("Suite Name", function() { // Put here your tests });
  • 13. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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 describe("Suite Name", function() { it("a spec with one expectation", function() { – of the code // create the spec body }); });
  • 14. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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
  • 15. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Matchers Example it("matchers", function() { var a = 12; var b = a; var c = function () { } expect(a).toBe(b); expect(a).not.toBe(null); expect(a).toEqual(12); expect(null).toBeNull(); expect(c).not.toThrow(); });
  • 16. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Demo CREATING SUITES AND SPECS
  • 17. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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(); }); }); #devconnections
  • 18. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Setup and Teardown • Jasmine includes – beforeEach – runs before each test – afterEach – runs after each test • Should exists inside a test suite
  • 19. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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); }); });
  • 20. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Demo USING SETUP AND TEARDOWN
  • 21. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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() { }); }); #devconnections
  • 22. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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
  • 23. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Demo WORKING WITH SPIES
  • 24. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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(); });
  • 25. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Demo WORKING WITH ASYNC FUNCTIONS
  • 26. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE 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
  • 27. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Demo JASMINE AND KARMA
  • 28. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE QUESTIONS?
  • 29. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Summary • Unit Tests are an important part of any development process • Jasmine library can help you test your JavaScript code • Add tests to your JavaScript code!
  • 30. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE Resources • Session slide deck – • Jasmine – http://jasmine.github.io/ • My Website – http://www.gilfink.net • Follow me on Twitter – @gilfink
  • 31. QUICK TOUR TO FRONT-END UNIT TESTING USING JASMINE THANK YOU