SlideShare a Scribd company logo
UNIT TESTING USING JASMINE
JAVASCRIPT
IMPORTANCE OF UNIT TEST
 Makes the process Agile
 Improves the Quality of Code
 Finds application issues/bugs early
 Detect Changes that may break the design contract in refactoring
 Provide debugging process
 Provides documentation
 Reduces cost
AVAILABLE UNIT TEST FRAMEWORKS
Find more unit test frameworks - https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
WHY JASMINE FRAMEWORK
 Behaviour driven development framework, also supports TDD
 Used for synchronous and asynchronous javascript code
 Does not rely on DOM or any browser
 Has built in assertion library and command line utility to run the tests
 Has simple syntax 
HELLO WORLD EXAMPLE
Javascript function –
- return ‘Hello World’ String
Unit test case for Javascript function –
- Calls actual function and matches the
output with expected output
INSTALL JASMINE FRAMEWORK
 Download latest version of Jasmine framework – click here
 Extract all the files
 Open ‘SpecRunner.html’ file
JASMINE FILE STRUCTURE
Boot Js –
this file "boots" Jasmine,
performing all of the
necessary initialization
before executing the
loaded environment and
all of a project's specs.
Spec folder -
Contains the Javascript
tesintg files unit test
files
Src folder -
Contains the Javascript
source files
Jasmine-html.js –
Javascript library which
has pure js functions to
capture the running
statistics of suits / specs
Jasmine.js –
Jasmine’s core engine
which responsible to
manage test life cycle
SpecRunner.html–
Test case runner HTML
file
CREATE TEST AND TEST SUITE
 describe(string suiteName) - Declare test suites using this function
 Group of all respective it() /test cases
 it (string testName) – Declare test case
 Write multiple it() calls in a single describe() function
 The string you passed to the describe() as the first parameter will be concatenated with the string passed to the
it(), to describe the complete name of the spec.
Syntax :
describe(‘Suite Name – Scenario name’, function() {
it(‘test case 1 name’, function() {
...
};
it(‘test case 2 name’, function() {
...
};
})
MATCHERS
• Any matcher can evaluate to a negative
assertion by chaining the call to expect with
a not before calling the matcher
• Each matcher implements a boolean
comparison between the actual value and
the expected value.
• It is responsible for reporting to Jasmine if
the expectation is true or false. Jasmine will
then pass or fail the spec.
LIST OF MATCHERS
SETUP AND TEARDOWN METHODS
 beforeEach() and afterEach() – runs before and after for every suite
 beforeAll() and afterAll() – runs before and after all the suites
before All afterAll
beforeEach
Test
afterEach
Test Suits
TEARDOWN METHODS EXAMPLE
beforeEach and afterEach Example beforeAll and afterAll Example
EXAMPLE - MATH.JS
• Create new file in SRC Folder
• Set file name as ‘Math.js’
• Create these functions for addition,
subtraction
• Make an entry in specRunner.html file
EXAMPLE - MATH-SPEC.JS
• Create new file in ‘spec’ Folder
• Set file name as ‘Math-spec.js’
• Make an entry in SpecRunner.html
file
JASMINE SPEC-RUNNER DASHBOARD
• Open SpecRunner.html file in any browser
• Calculator is the name of the ‘Test suite’
• Name start with ‘should’ are the spec /scenario
description
FAILING SPEC
 The fail () function causes a spec to fail.
 It can take a failure message or an Error object as a parameter.
FAILING SPEC RESULTS
THANK YOU

More Related Content

PDF
One mock to far
PPTX
Template pattern
PPTX
Moq presentation
PPTX
Unit Testing in PHP
PPT
New Features Of Test Unit 2.x
PPTX
Selenium TestNG
PDF
Manage Selenium Grid with Terraform by Omkar Khatavkar (https://www.linkedin....
PPTX
Java Unit Test - JUnit
One mock to far
Template pattern
Moq presentation
Unit Testing in PHP
New Features Of Test Unit 2.x
Selenium TestNG
Manage Selenium Grid with Terraform by Omkar Khatavkar (https://www.linkedin....
Java Unit Test - JUnit

What's hot (20)

PPTX
Refactoring: Improving the design of existing code. Chapter 6.
PPTX
Unit Testing with Python
PPTX
Unit testing using Munit Part 1
PDF
Unit testing Ch. 13 of Programming Ruby
PPTX
TestNG vs Junit
PPTX
Action filter
PPTX
Tdd & unit test
PPTX
Wix Automation - Test State Pattern - 11.9.16
PDF
Workshop unit test
PPT
Intro to junit
PPTX
Var arg methods
PPTX
Part 11 sql model clause-return all rows
PPTX
Unit test
PDF
Unit Testing in WordPress
PDF
Unit testing.pptx [repaired]
PPTX
Intro To Unit and integration Testing
PDF
JavaScript Unit Testing with an Angular 5.x Use Case 101
PPTX
TestNG vs JUnit: cease fire or the end of the war
PDF
TestNG Annotations in Selenium | Edureka
PPTX
Unit testing
Refactoring: Improving the design of existing code. Chapter 6.
Unit Testing with Python
Unit testing using Munit Part 1
Unit testing Ch. 13 of Programming Ruby
TestNG vs Junit
Action filter
Tdd & unit test
Wix Automation - Test State Pattern - 11.9.16
Workshop unit test
Intro to junit
Var arg methods
Part 11 sql model clause-return all rows
Unit test
Unit Testing in WordPress
Unit testing.pptx [repaired]
Intro To Unit and integration Testing
JavaScript Unit Testing with an Angular 5.x Use Case 101
TestNG vs JUnit: cease fire or the end of the war
TestNG Annotations in Selenium | Edureka
Unit testing
Ad

Similar to Unit testing using jasmine in Javascript (20)

PPTX
PDF
Quick tour to front end unit testing using jasmine
PDF
Front end unit testing using jasmine
PDF
Quick Tour to Front-End Unit Testing Using Jasmine
PDF
Quick tour to front end unit testing using jasmine
PDF
JavaScript TDD with Jasmine and Karma
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
PPTX
Jasmine framework
PDF
Up & running with jasmine
PPTX
Jasmine
PPTX
Java Script Isn\'t a Toy Anymore
PPTX
Unit testing in JavaScript with Jasmine and Karma
PPTX
Testing JavaScript with Jasmine in Rails Applications
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PPTX
Jasmine Testing to the Rescue!
PDF
Jasmine - why JS tests don't smell fishy
PPTX
Javascript Testing with Jasmine 101
PDF
JAVASCRIPT Test Driven Development & Jasmine
PDF
3 WAYS TO TEST YOUR COLDFUSION API
Quick tour to front end unit testing using jasmine
Front end unit testing using jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
Quick tour to front end unit testing using jasmine
JavaScript TDD with Jasmine and Karma
Unit testing of java script and angularjs application using Karma Jasmine Fra...
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Jasmine framework
Up & running with jasmine
Jasmine
Java Script Isn\'t a Toy Anymore
Unit testing in JavaScript with Jasmine and Karma
Testing JavaScript with Jasmine in Rails Applications
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Jasmine Testing to the Rescue!
Jasmine - why JS tests don't smell fishy
Javascript Testing with Jasmine 101
JAVASCRIPT Test Driven Development & Jasmine
3 WAYS TO TEST YOUR COLDFUSION API
Ad

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Classroom Observation Tools for Teachers
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial disease of the cardiovascular and lymphatic systems
Classroom Observation Tools for Teachers
PPH.pptx obstetrics and gynecology in nursing
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Renaissance Architecture: A Journey from Faith to Humanism
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Unit testing using jasmine in Javascript

  • 1. UNIT TESTING USING JASMINE JAVASCRIPT
  • 2. IMPORTANCE OF UNIT TEST  Makes the process Agile  Improves the Quality of Code  Finds application issues/bugs early  Detect Changes that may break the design contract in refactoring  Provide debugging process  Provides documentation  Reduces cost
  • 3. AVAILABLE UNIT TEST FRAMEWORKS Find more unit test frameworks - https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
  • 4. WHY JASMINE FRAMEWORK  Behaviour driven development framework, also supports TDD  Used for synchronous and asynchronous javascript code  Does not rely on DOM or any browser  Has built in assertion library and command line utility to run the tests  Has simple syntax 
  • 5. HELLO WORLD EXAMPLE Javascript function – - return ‘Hello World’ String Unit test case for Javascript function – - Calls actual function and matches the output with expected output
  • 6. INSTALL JASMINE FRAMEWORK  Download latest version of Jasmine framework – click here  Extract all the files  Open ‘SpecRunner.html’ file
  • 7. JASMINE FILE STRUCTURE Boot Js – this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. Spec folder - Contains the Javascript tesintg files unit test files Src folder - Contains the Javascript source files Jasmine-html.js – Javascript library which has pure js functions to capture the running statistics of suits / specs Jasmine.js – Jasmine’s core engine which responsible to manage test life cycle SpecRunner.html– Test case runner HTML file
  • 8. CREATE TEST AND TEST SUITE  describe(string suiteName) - Declare test suites using this function  Group of all respective it() /test cases  it (string testName) – Declare test case  Write multiple it() calls in a single describe() function  The string you passed to the describe() as the first parameter will be concatenated with the string passed to the it(), to describe the complete name of the spec. Syntax : describe(‘Suite Name – Scenario name’, function() { it(‘test case 1 name’, function() { ... }; it(‘test case 2 name’, function() { ... }; })
  • 9. MATCHERS • Any matcher can evaluate to a negative assertion by chaining the call to expect with a not before calling the matcher • Each matcher implements a boolean comparison between the actual value and the expected value. • It is responsible for reporting to Jasmine if the expectation is true or false. Jasmine will then pass or fail the spec.
  • 11. SETUP AND TEARDOWN METHODS  beforeEach() and afterEach() – runs before and after for every suite  beforeAll() and afterAll() – runs before and after all the suites before All afterAll beforeEach Test afterEach Test Suits
  • 12. TEARDOWN METHODS EXAMPLE beforeEach and afterEach Example beforeAll and afterAll Example
  • 13. EXAMPLE - MATH.JS • Create new file in SRC Folder • Set file name as ‘Math.js’ • Create these functions for addition, subtraction • Make an entry in specRunner.html file
  • 14. EXAMPLE - MATH-SPEC.JS • Create new file in ‘spec’ Folder • Set file name as ‘Math-spec.js’ • Make an entry in SpecRunner.html file
  • 15. JASMINE SPEC-RUNNER DASHBOARD • Open SpecRunner.html file in any browser • Calculator is the name of the ‘Test suite’ • Name start with ‘should’ are the spec /scenario description
  • 16. FAILING SPEC  The fail () function causes a spec to fail.  It can take a failure message or an Error object as a parameter.