SlideShare a Scribd company logo
1 of 17
UnitTesting Of JavaScript
and Angularjs Application
Using Karma-Jasmine
Framework
-Samyak Bhalerao
(smk.bhalerao@gmail.com/samyak.bhalerao@xoriant.com)
Agenda
• What is testing?
• Black box testing vsWhite box testing
• What is Unit testing?
• Prerequisites
• What is Jasmine?
• Rules/Specs for writing test cases using jasmine
• What is Karma?
• How to configure Karma
• How to create Karma configuration file
• Testing sample JavaScript
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
Agenda
• Testing Angularjs application
• Testing Controller
•Testing variables
•Testing functions
• Testing Service
• Testing Directive
•Directive without external html template
•Directive with external template
• Testing filters
• Testing http requests(GET,POST,etc)
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Testing ?
• Testing is the process of evaluating a system or its component(s) with the
intent to find whether it satisfies the specified requirements or not.
• Testing is executing a system in order to identify any gaps, errors, or missing
requirements in contrary to the actual requirements.
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
Black box testing vs White box testing
• Black box testing:
• The technique of testing without having any knowledge of the interior workings of the
application is called black-box testing
• The tester is oblivious to the system architecture and does not have access to the
source code
• Typically, while performing a black-box test, a tester will interact with the system's
user interface by providing inputs and examining outputs without knowing how and
where the inputs are worked upon.
• Inefficient testing, due to the fact that the tester only has limited knowledge about an
application.
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
Black box testing vs White box testing
• White box testing
• White-box testing is the detailed investigation of internal logic and structure of the
code.
• White-box testing is also called glass testing or open-box testing.
• The tester needs to have a look inside the source code and find out which unit/chunk of
the code is behaving inappropriately.
• It helps in optimizing the code.
• Extra lines of code can be removed which can bring in hidden defects.
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Unit testing?
• This type of testing is performed by developers before the setup is handed
over to the testing team to formally execute the test cases
• The goal of unit testing is to isolate each part of the program and show that
individual parts are correct in terms of requirements and functionality.
• There is a limit to the number of scenarios and test data that a developer
can use to verify a source code.
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
Prerequisites
• Install nodejs
• Install it in any folder of your choice
• Download nodejs suitable setup file from https://nodejs.org/download/ website
• Install nodejs using setup file
• Run node –version in command prompt to check node version
• Install npm
• Node Package Manager(npm)
• Run npm –version to check installation of npm
4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
What is Jasmine?
• Jasmine is a behavior-driven development framework for testing JavaScript code
• It does not depend on any other JavaScript frameworks
• It does not require a DOM
• http://jasmine.github.io/2.2/introduction.html
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Jasmine?
• Rules/Specs for writing test cases using jasmine
• describe(‘string ’,function(){ }) block
• A test suite begins with a call to the global Jasmine function describe
• It accepts two parameters first is string and second is function()
• String is a name or title for test suit or test cases. It usually describes what is being tested
• Function is block of code that implements test cases
• Any number of nesting of describe block is possible
• Eg . describe(‘Testing sampleApp.js :’,function(){
//code to implement test case goes here
});
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Jasmine?
• Rules/Specs for writing test cases using jasmine
• beforeEach(function(){ });
• This block is executes before test cases
• It is used to load modules before execution of test cases
• afterEach(function(){ });
• This block is executed after each test cases
• It is use for task like flushing memory , destroying instance etc.
4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
What is Jasmine?
• Rules/Specs for writing test cases using jasmine
• it(‘string’ , function(){ }) block
• Specs or test cases are defined by calling jasmine global function ‘it()’
• Variables declared inside describe() block are accessible to all it block within it.
• It accepts two parameters first is string and second is function()
• String is a name or title for test suit or test cases. It usually describes the expected behavior or functionality of
block of code
• Function is block of code that implements test cases. It contains one or more expectations that test the state
of the code.
• All assertions to test the code are inside this function
• A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec.
• Eg . It(‘expect true to be true’,function(){
expect(true).toBe(true);
});
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Jasmine?
• Rules/Specs for writing test cases using jasmine
• Expectations
• Expectations are built with the function expect() which takes a value, called the actual. It is
chained with a Matcher function(eg. .toBe(),.toBeTruthy()), which takes the expected value.
• 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.
• Any matcher can evaluate to a negative assertion by chaining the call to expect with
a not before calling the matcher.
• Eg. expect(true).toBe(true);
expect(true).not.toBe(false);
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Jasmine?
• Rules/Specs for writing test cases using jasmine
• Expectations
• Jasmine has a rich set of matchers included.
• expect().toBe(), expect().not.toBe();('toBe' matcher compares with ===)
• expect().toHaveBeenCalled(),expect().toHaveBeenCalledWith().(it check for method call)
• expect().toBeDefined(), expect().toBeUndefined().(compares for defined/defination )
• expect().toEqual(),expect().not.toEqual();(it is used for simple literals and variables)
• expectGET(‘url’,data).respond(), expectPOST(‘url’,data).respond(), expectDELETE(‘url’,data).respond(),etc.(it is
used for http request assertion)
• expect().toBeNull().(compared against null)
• expect().toBeTruthy(),expect().toBeFalsy()..(use for Boolean casting testing)
• expect().toContain()..(use for pattern matching)
• expect().toMatch().(it is used for regular expression)
Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
What is Karma?
• Karma is “TEST RUNNER”
• Tool to spawn a web server that executes source code against test code
• Can run tests for different browsers
• Provides watches for source files, whenever file changes it triggers the test
run and run tests again
• http://karma-runner.github.io/0.12/intro/installation.html
4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
Karma Configuration Steps
4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
Create Karma Configuration File
(karma.config.js)
4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework

More Related Content

What's hot

Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSJim Lynch
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsFITC
 
Quick tour to front end unit testing using jasmine
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
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSKnoldus Inc.
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaChristopher Bartling
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma Christopher Bartling
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingLars Thorup
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJSPeter Drinnan
 
Angular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewAngular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewThirumal Sakthivel
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit TestingPrince Norin
 
Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018Justin James
 
Angular Unit Testing from the Trenches
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the TrenchesJustin James
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karmaClient side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karmaAdam Klein
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQueryKnoldus Inc.
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with JestMichał Pierzchała
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineAnup Singh
 
Quick tour to front end unit testing using jasmine
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
 

What's hot (20)

Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJS
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
 
Quick tour to front end unit testing using jasmine
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
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
Karma - JS Test Runner
Karma - JS Test RunnerKarma - JS Test Runner
Karma - JS Test Runner
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit Testing
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Angular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewAngular JS Unit Testing - Overview
Angular JS Unit Testing - Overview
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Jasmine BDD for Javascript
Jasmine BDD for JavascriptJasmine BDD for Javascript
Jasmine BDD for Javascript
 
Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018
 
Angular Unit Testing from the Trenches
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the Trenches
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karmaClient side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
 
Quick tour to front end unit testing using jasmine
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
 

Viewers also liked

Introduction to Express and Grunt
Introduction to Express and GruntIntroduction to Express and Grunt
Introduction to Express and GruntPeter deHaan
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testingDejan Toteff
 
Publish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design PatternsPublish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design PatternsRutvik Bapat
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpMatthew Davis
 
Publish subscribe model overview
Publish subscribe model overviewPublish subscribe model overview
Publish subscribe model overviewIshraq Al Fataftah
 
Protractor framework – how to make stable e2e tests for Angular applications
Protractor framework – how to make stable e2e tests for Angular applicationsProtractor framework – how to make stable e2e tests for Angular applications
Protractor framework – how to make stable e2e tests for Angular applicationsLudmila Nesvitiy
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2Trung Vo Tuan
 
Bower & Grunt - A practical workflow
Bower & Grunt - A practical workflowBower & Grunt - A practical workflow
Bower & Grunt - A practical workflowRiccardo Coppola
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJSstefanmayer13
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015Ben Lesh
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScriptSimon Guest
 

Viewers also liked (13)

Introduction to Express and Grunt
Introduction to Express and GruntIntroduction to Express and Grunt
Introduction to Express and Grunt
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testing
 
Publish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design PatternsPublish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design Patterns
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
 
Publish and Subscribe
Publish and SubscribePublish and Subscribe
Publish and Subscribe
 
Publish subscribe model overview
Publish subscribe model overviewPublish subscribe model overview
Publish subscribe model overview
 
Workshop - E2e tests with protractor
Workshop - E2e tests with protractorWorkshop - E2e tests with protractor
Workshop - E2e tests with protractor
 
Protractor framework – how to make stable e2e tests for Angular applications
Protractor framework – how to make stable e2e tests for Angular applicationsProtractor framework – how to make stable e2e tests for Angular applications
Protractor framework – how to make stable e2e tests for Angular applications
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Bower & Grunt - A practical workflow
Bower & Grunt - A practical workflowBower & Grunt - A practical workflow
Bower & Grunt - A practical workflow
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJS
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
 

Similar to Unit testing of java script and angularjs application using Karma Jasmine Framework

Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptxRiyaBangera
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the UntestableMark Baker
 
[FullStack NYC 2019] Effective Unit Tests for JavaScript
[FullStack NYC 2019] Effective Unit Tests for JavaScript[FullStack NYC 2019] Effective Unit Tests for JavaScript
[FullStack NYC 2019] Effective Unit Tests for JavaScriptHazem Saleh
 
Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Ryan Cuprak
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSSanil Mhatre
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scaladatamantra
 
Testing strategies -2
Testing strategies -2Testing strategies -2
Testing strategies -2Divya Tiwari
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 
Unit testing in Force.com platform
Unit testing in Force.com platformUnit testing in Force.com platform
Unit testing in Force.com platformChamil Madusanka
 
<p>Software Testing</p>
<p>Software Testing</p><p>Software Testing</p>
<p>Software Testing</p>Atul Mishra
 
An overview to Software Testing
An overview to Software TestingAn overview to Software Testing
An overview to Software TestingAtul Mishra
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choicetoddbr
 
Building Efficient Software with Property Based Testing
Building Efficient Software with Property Based TestingBuilding Efficient Software with Property Based Testing
Building Efficient Software with Property Based TestingCitiusTech
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildAndrei Sebastian Cîmpean
 
Software engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designSoftware engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designMaitree Patel
 
Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad Sathya Technologies
 

Similar to Unit testing of java script and angularjs application using Karma Jasmine Framework (20)

Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptx
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
[FullStack NYC 2019] Effective Unit Tests for JavaScript
[FullStack NYC 2019] Effective Unit Tests for JavaScript[FullStack NYC 2019] Effective Unit Tests for JavaScript
[FullStack NYC 2019] Effective Unit Tests for JavaScript
 
Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)
 
Angular Testing
Angular TestingAngular Testing
Angular Testing
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Database Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTSDatabase Unit Testing Made Easy with VSTS
Database Unit Testing Made Easy with VSTS
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Testing strategies -2
Testing strategies -2Testing strategies -2
Testing strategies -2
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Unit testing in Force.com platform
Unit testing in Force.com platformUnit testing in Force.com platform
Unit testing in Force.com platform
 
<p>Software Testing</p>
<p>Software Testing</p><p>Software Testing</p>
<p>Software Testing</p>
 
An overview to Software Testing
An overview to Software TestingAn overview to Software Testing
An overview to Software Testing
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choice
 
Building Efficient Software with Property Based Testing
Building Efficient Software with Property Based TestingBuilding Efficient Software with Property Based Testing
Building Efficient Software with Property Based Testing
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
 
Software engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designSoftware engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit design
 
Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad Selenium course training institute ameerpet hyderabad
Selenium course training institute ameerpet hyderabad
 

Recently uploaded

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 

Recently uploaded (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Unit testing of java script and angularjs application using Karma Jasmine Framework

  • 1. UnitTesting Of JavaScript and Angularjs Application Using Karma-Jasmine Framework -Samyak Bhalerao (smk.bhalerao@gmail.com/samyak.bhalerao@xoriant.com)
  • 2. Agenda • What is testing? • Black box testing vsWhite box testing • What is Unit testing? • Prerequisites • What is Jasmine? • Rules/Specs for writing test cases using jasmine • What is Karma? • How to configure Karma • How to create Karma configuration file • Testing sample JavaScript Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 3. Agenda • Testing Angularjs application • Testing Controller •Testing variables •Testing functions • Testing Service • Testing Directive •Directive without external html template •Directive with external template • Testing filters • Testing http requests(GET,POST,etc) Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 4. What is Testing ? • Testing is the process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not. • Testing is executing a system in order to identify any gaps, errors, or missing requirements in contrary to the actual requirements. Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 5. Black box testing vs White box testing • Black box testing: • The technique of testing without having any knowledge of the interior workings of the application is called black-box testing • The tester is oblivious to the system architecture and does not have access to the source code • Typically, while performing a black-box test, a tester will interact with the system's user interface by providing inputs and examining outputs without knowing how and where the inputs are worked upon. • Inefficient testing, due to the fact that the tester only has limited knowledge about an application. Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 6. Black box testing vs White box testing • White box testing • White-box testing is the detailed investigation of internal logic and structure of the code. • White-box testing is also called glass testing or open-box testing. • The tester needs to have a look inside the source code and find out which unit/chunk of the code is behaving inappropriately. • It helps in optimizing the code. • Extra lines of code can be removed which can bring in hidden defects. Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 7. What is Unit testing? • This type of testing is performed by developers before the setup is handed over to the testing team to formally execute the test cases • The goal of unit testing is to isolate each part of the program and show that individual parts are correct in terms of requirements and functionality. • There is a limit to the number of scenarios and test data that a developer can use to verify a source code. Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 8. Prerequisites • Install nodejs • Install it in any folder of your choice • Download nodejs suitable setup file from https://nodejs.org/download/ website • Install nodejs using setup file • Run node –version in command prompt to check node version • Install npm • Node Package Manager(npm) • Run npm –version to check installation of npm 4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
  • 9. What is Jasmine? • Jasmine is a behavior-driven development framework for testing JavaScript code • It does not depend on any other JavaScript frameworks • It does not require a DOM • http://jasmine.github.io/2.2/introduction.html Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 10. What is Jasmine? • Rules/Specs for writing test cases using jasmine • describe(‘string ’,function(){ }) block • A test suite begins with a call to the global Jasmine function describe • It accepts two parameters first is string and second is function() • String is a name or title for test suit or test cases. It usually describes what is being tested • Function is block of code that implements test cases • Any number of nesting of describe block is possible • Eg . describe(‘Testing sampleApp.js :’,function(){ //code to implement test case goes here }); Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 11. What is Jasmine? • Rules/Specs for writing test cases using jasmine • beforeEach(function(){ }); • This block is executes before test cases • It is used to load modules before execution of test cases • afterEach(function(){ }); • This block is executed after each test cases • It is use for task like flushing memory , destroying instance etc. 4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
  • 12. What is Jasmine? • Rules/Specs for writing test cases using jasmine • it(‘string’ , function(){ }) block • Specs or test cases are defined by calling jasmine global function ‘it()’ • Variables declared inside describe() block are accessible to all it block within it. • It accepts two parameters first is string and second is function() • String is a name or title for test suit or test cases. It usually describes the expected behavior or functionality of block of code • Function is block of code that implements test cases. It contains one or more expectations that test the state of the code. • All assertions to test the code are inside this function • A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec. • Eg . It(‘expect true to be true’,function(){ expect(true).toBe(true); }); Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 13. What is Jasmine? • Rules/Specs for writing test cases using jasmine • Expectations • Expectations are built with the function expect() which takes a value, called the actual. It is chained with a Matcher function(eg. .toBe(),.toBeTruthy()), which takes the expected value. • 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. • Any matcher can evaluate to a negative assertion by chaining the call to expect with a not before calling the matcher. • Eg. expect(true).toBe(true); expect(true).not.toBe(false); Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 14. What is Jasmine? • Rules/Specs for writing test cases using jasmine • Expectations • Jasmine has a rich set of matchers included. • expect().toBe(), expect().not.toBe();('toBe' matcher compares with ===) • expect().toHaveBeenCalled(),expect().toHaveBeenCalledWith().(it check for method call) • expect().toBeDefined(), expect().toBeUndefined().(compares for defined/defination ) • expect().toEqual(),expect().not.toEqual();(it is used for simple literals and variables) • expectGET(‘url’,data).respond(), expectPOST(‘url’,data).respond(), expectDELETE(‘url’,data).respond(),etc.(it is used for http request assertion) • expect().toBeNull().(compared against null) • expect().toBeTruthy(),expect().toBeFalsy()..(use for Boolean casting testing) • expect().toContain()..(use for pattern matching) • expect().toMatch().(it is used for regular expression) Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework 4/17/2015
  • 15. What is Karma? • Karma is “TEST RUNNER” • Tool to spawn a web server that executes source code against test code • Can run tests for different browsers • Provides watches for source files, whenever file changes it triggers the test run and run tests again • http://karma-runner.github.io/0.12/intro/installation.html 4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
  • 16. Karma Configuration Steps 4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework
  • 17. Create Karma Configuration File (karma.config.js) 4/17/2015Unit Testing Of JavaScript and Angularjs Application Using Karma-Jasmine Framework