SlideShare a Scribd company logo
SELA DEVELOPER PRACTICE
Gil Fink
Front-End Unit Testing
Using Jasmine
Agenda
Unit Testing in JavaScript?
Behavior Driven Development
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
Unit Testing in JavaScript?
JavaScript is everywhere
Browsers
Servers
Operation Systems
Databases
Mobile
Devices
You are doing it in any other language…
Behavior Driven Development (BDD)
BDD – Cont.
In BDD you
describe your code
tell the test what it (the code) should do
expect your code to do something
The user can read the output and understand it
//suite
describe ('', function(){
//test
it ('', function(){
//expectation
expect();
)};
});
Jasmine
A 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
Jasmine Tests Suites
First create a Suite which is a container of Specs
Use the describe function
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
describe("Suite Name", function() {
it("a spec with one expectation", function() {
// create the spec body
});
});
Expectations
Expectations are assertions that can be either
true or false
Use the expect function within a spec to declare
an expectation
Receives the actual value to test as parameter
Include fluent API for matchers
A Matcher is a Boolean comparison between the
actual value and the expected value
Matchers
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();
});
Demo
Creating Suites and Specs
Setup and Teardown
Jasmine includes
beforeEach – runs before each test
afterEach – runs after each test
Should exists inside a test suite
Setup and 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
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 – http://slidesha.re/1mO08Mq
Jasmine – http://jasmine.github.io/
My Website – http://www.gilfink.net
Follow me on Twitter – @gilfink
THANK YOU
Gil Fink
@gilfink
http://www.gilfink.net

More Related Content

PDF
Quick Tour to Front-End Unit Testing Using Jasmine
KEY
iOS Unit Testing
PPT
Unit Testing in iOS - Ninjava Talk
PPT
Unit Testing in iOS
PDF
Quick tour to front end unit testing using jasmine
PDF
Intro to Unit Testing in AngularJS
PPTX
Interpreter RPG to Java
ODP
Unit Testing and Coverage for AngularJS
Quick Tour to Front-End Unit Testing Using Jasmine
iOS Unit Testing
Unit Testing in iOS - Ninjava Talk
Unit Testing in iOS
Quick tour to front end unit testing using jasmine
Intro to Unit Testing in AngularJS
Interpreter RPG to Java
Unit Testing and Coverage for AngularJS

What's hot (20)

PPT
TDD, BDD, RSpec
PDF
Describe's Full of It's
PPTX
Clean Code
PDF
TDD, BDD and mocks
PPTX
Angular Unit Testing
PDF
Testing Legacy Rails Apps
PPT
Unit Testing RPG with JUnit
PPTX
Angular Unit Test
PPT
RPG Program for Unit Testing RPG
ODP
Jquery- One slide completing all JQuery
PDF
Unit-testing and E2E testing in JS
PDF
Hitchhiker's guide to Functional Testing
PDF
[FullStack NYC 2019] Effective Unit Tests for JavaScript
PDF
AngularJS Unit Test
PDF
AngularJS Unit Testing w/Karma and Jasmine
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
PPTX
Angular Unit Testing
ODP
Improve your development skills with Test Driven Development
KEY
Unit Test Your Database
PPTX
Laravel Unit Testing
TDD, BDD, RSpec
Describe's Full of It's
Clean Code
TDD, BDD and mocks
Angular Unit Testing
Testing Legacy Rails Apps
Unit Testing RPG with JUnit
Angular Unit Test
RPG Program for Unit Testing RPG
Jquery- One slide completing all JQuery
Unit-testing and E2E testing in JS
Hitchhiker's guide to Functional Testing
[FullStack NYC 2019] Effective Unit Tests for JavaScript
AngularJS Unit Test
AngularJS Unit Testing w/Karma and Jasmine
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Angular Unit Testing
Improve your development skills with Test Driven Development
Unit Test Your Database
Laravel Unit Testing
Ad

Similar to Front end unit testing using jasmine (20)

PDF
Quick tour to front end unit testing using jasmine
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PPTX
PDF
JavaScript TDD with Jasmine and Karma
PPTX
Unit testing using jasmine in Javascript
PPTX
Jasmine Testing to the Rescue!
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
PPTX
Unit testing in JavaScript with Jasmine and Karma
PPT
Jasmine presentation Selenium Camp 2013
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
PDF
How to write Testable Javascript
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
PPTX
Testing JavaScript with Jasmine in Rails Applications
PPT
Jasmine - A BDD test framework for JavaScript
PPT
TDD, unit testing and java script testing frameworks workshop
PDF
What the HTML? - The Holy Grail
PPTX
Java Script Isn\'t a Toy Anymore
PPTX
JavaScript Unit Testing
PDF
Angularjs - Unit testing introduction
Quick tour to front end unit testing using jasmine
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
JavaScript TDD with Jasmine and Karma
Unit testing using jasmine in Javascript
Jasmine Testing to the Rescue!
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API -
Unit testing in JavaScript with Jasmine and Karma
Jasmine presentation Selenium Camp 2013
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
How to write Testable Javascript
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Testing JavaScript with Jasmine in Rails Applications
Jasmine - A BDD test framework for JavaScript
TDD, unit testing and java script testing frameworks workshop
What the HTML? - The Holy Grail
Java Script Isn\'t a Toy Anymore
JavaScript Unit Testing
Angularjs - Unit testing introduction
Ad

More from Gil Fink (20)

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

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PPTX
Spectroscopy.pptx food analysis technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PPTX
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Spectroscopy.pptx food analysis technology
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Network Security Unit 5.pdf for BCA BBA.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Review of recent advances in non-invasive hemoglobin estimation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Programs and apps: productivity, graphics, security and other tools

Front end unit testing using jasmine

  • 1. SELA DEVELOPER PRACTICE Gil Fink Front-End Unit Testing Using Jasmine
  • 2. Agenda Unit Testing in JavaScript? Behavior Driven Development 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. Unit Testing in JavaScript? JavaScript is everywhere Browsers Servers Operation Systems Databases Mobile Devices You are doing it in any other language…
  • 7. BDD – Cont. In BDD you describe your code tell the test what it (the code) should do expect your code to do something The user can read the output and understand it //suite describe ('', function(){ //test it ('', function(){ //expectation expect(); )}; });
  • 8. Jasmine A JavaScript BDD framework Can be downloaded from: http://jasmine.github.io/
  • 9. 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
  • 11. Jasmine Tests Suites First create a Suite which is a container of Specs Use the describe function describe("Suite Name", function() { // Put here your tests });
  • 12. 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 describe("Suite Name", function() { it("a spec with one expectation", function() { // create the spec body }); });
  • 13. Expectations Expectations are assertions that can be either true or false Use the expect function within a spec to declare an expectation Receives the actual value to test as parameter Include fluent API for matchers A Matcher is a Boolean comparison between the actual value and the expected value
  • 14. Matchers 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. Setup and Teardown Jasmine includes beforeEach – runs before each test afterEach – runs after each test Should exists inside a test suite
  • 17. Setup and 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); }); });
  • 19. 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
  • 21. 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(); });
  • 23. 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
  • 26. 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!
  • 27. Resources Session slide deck – http://slidesha.re/1mO08Mq Jasmine – http://jasmine.github.io/ My Website – http://www.gilfink.net Follow me on Twitter – @gilfink