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
PPTX
Jasmine framework
PPT
An Introduction to AngularJs Unittesting
PDF
Quick tour to front end unit testing using jasmine
ODP
Unit Testing and Coverage for AngularJS
PPTX
In search of JavaScript code quality: unit testing
PPT
Mxunit
ODP
S313352 optimizing java device testing with automatic feature discovering
PDF
Testing akka-actors
PPTX
Angular Unit Testing
PDF
Spring 3.1 and MVC Testing Support
PPTX
Junit_.pptx
PPTX
Testing And Mxunit In ColdFusion
PPTX
Unit testing
PDF
Workshop 23: ReactJS, React & Redux testing
PPT
Unit Testing using PHPUnit
PDF
Unit testing with JUnit
PPTX
Testing Ansible
PPTX
Appium TestNG Framework and Multi-Device Automation Execution
PDF
Javascript tdd byandreapaciolla
Jasmine framework
An Introduction to AngularJs Unittesting
Quick tour to front end unit testing using jasmine
Unit Testing and Coverage for AngularJS
In search of JavaScript code quality: unit testing
Mxunit
S313352 optimizing java device testing with automatic feature discovering
Testing akka-actors
Angular Unit Testing
Spring 3.1 and MVC Testing Support
Junit_.pptx
Testing And Mxunit In ColdFusion
Unit testing
Workshop 23: ReactJS, React & Redux testing
Unit Testing using PHPUnit
Unit testing with JUnit
Testing Ansible
Appium TestNG Framework and Multi-Device Automation Execution
Javascript tdd byandreapaciolla
Ad

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RMMM.pdf make it easy to upload and study
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
A systematic review of self-coping strategies used by university students to ...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
01-Introduction-to-Information-Management.pdf
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chinmaya Tiranga quiz Grand Finale.pdf
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial diseases, their pathogenesis and prophylaxis

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.