SlideShare a Scribd company logo
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 (20)

Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJS
Jim Lynch
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
FITC
 
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
Gil Fink
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher 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
 
Karma - JS Test Runner
Karma - JS Test RunnerKarma - JS Test Runner
Karma - JS Test Runner
Sebastiano Armeli
 
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
Lars Thorup
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
Peter Drinnan
 
Angular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewAngular JS Unit Testing - Overview
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
Prince Norin
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
Avi Engelshtein
 
Jasmine BDD for Javascript
Jasmine BDD for JavascriptJasmine BDD for Javascript
Jasmine BDD for Javascript
Luis Alfredo Porras Páez
 
Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018
Justin James
 
Angular Unit Testing from the Trenches
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the Trenches
Justin 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 & karma
Adam Klein
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
Knoldus Inc.
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
Michał Pierzchała
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
Anup 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 jasmine
Gil Fink
 
Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJS
Jim Lynch
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
FITC
 
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
Gil Fink
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher 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 Testing
Lars Thorup
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
Peter Drinnan
 
Angular JS Unit Testing - Overview
Angular JS Unit Testing - OverviewAngular JS Unit Testing - Overview
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
Prince Norin
 
Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018
Justin James
 
Angular Unit Testing from the Trenches
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the Trenches
Justin 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 & karma
Adam Klein
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
Knoldus Inc.
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
Michał Pierzchała
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
Anup 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 jasmine
Gil Fink
 

Viewers also liked (13)

Introduction to Express and Grunt
Introduction to Express and GruntIntroduction to Express and Grunt
Introduction to Express and Grunt
Peter deHaan
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testing
Dejan Toteff
 
Publish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design PatternsPublish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design Patterns
Rutvik 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 Gulp
Matthew Davis
 
Publish and Subscribe
Publish and SubscribePublish and Subscribe
Publish and Subscribe
Alexandru Badiu
 
Publish subscribe model overview
Publish subscribe model overviewPublish subscribe model overview
Publish subscribe model overview
Ishraq Al Fataftah
 
Workshop - E2e tests with protractor
Workshop - E2e tests with protractorWorkshop - E2e tests with protractor
Workshop - E2e tests with protractor
Walmyr Lima e Silva Filho
 
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
Ludmila Nesvitiy
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
Tuan Trung Vo
 
Bower & Grunt - A practical workflow
Bower & Grunt - A practical workflowBower & Grunt - A practical workflow
Bower & Grunt - A practical workflow
Riccardo Coppola
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJS
stefanmayer13
 
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
Ben Lesh
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
Simon Guest
 
Introduction to Express and Grunt
Introduction to Express and GruntIntroduction to Express and Grunt
Introduction to Express and Grunt
Peter deHaan
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testing
Dejan Toteff
 
Publish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design PatternsPublish Subscribe pattern - Design Patterns
Publish Subscribe pattern - Design Patterns
Rutvik 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 Gulp
Matthew Davis
 
Publish subscribe model overview
Publish subscribe model overviewPublish subscribe model overview
Publish subscribe model overview
Ishraq 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 applications
Ludmila Nesvitiy
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
Tuan Trung Vo
 
Bower & Grunt - A practical workflow
Bower & Grunt - A practical workflowBower & Grunt - A practical workflow
Bower & Grunt - A practical workflow
Riccardo Coppola
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJS
stefanmayer13
 
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
Ben Lesh
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
Simon Guest
 
Ad

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

Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
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
Gil Fink
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 
JavaScript Unit Testing with an Angular 5.x Use Case 101
JavaScript Unit Testing with an Angular 5.x Use Case 101JavaScript Unit Testing with an Angular 5.x Use Case 101
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
jasmine
jasminejasmine
jasmine
Asanka Indrajith
 
Unit testing using jasmine in Javascript
Unit testing using jasmine in JavascriptUnit testing using jasmine in Javascript
Unit testing using jasmine in Javascript
Deepak More
 
Angular Testing
Angular TestingAngular Testing
Angular Testing
Kourosh Sajjadi
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven TomacJavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
Jim Lynch
 
Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - 3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
Jasmine Testing to the Rescue!
Jasmine Testing to the Rescue!Jasmine Testing to the Rescue!
Jasmine Testing to the Rescue!
Christopher Steele
 
An Introduction to AngularJs Unittesting
An Introduction to AngularJs UnittestingAn Introduction to AngularJs Unittesting
An Introduction to AngularJs Unittesting
Inthra onsap
 
What the HTML? - The Holy Grail
What the HTML? - The Holy GrailWhat the HTML? - The Holy Grail
What the HTML? - The Holy Grail
James Ford
 
Unit-testing and E2E testing in JS
Unit-testing and E2E testing in JSUnit-testing and E2E testing in JS
Unit-testing and E2E testing in JS
Michael Haberman
 
Slaven tomac unit testing in angular js
Slaven tomac   unit testing in angular jsSlaven tomac   unit testing in angular js
Slaven tomac unit testing in angular js
Slaven Tomac
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
Troy Miles
 
Angular Unit testing.pptx
Angular Unit testing.pptxAngular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
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
Gil Fink
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 
JavaScript Unit Testing with an Angular 5.x Use Case 101
JavaScript Unit Testing with an Angular 5.x Use Case 101JavaScript Unit Testing with an Angular 5.x Use Case 101
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
Unit testing using jasmine in Javascript
Unit testing using jasmine in JavascriptUnit testing using jasmine in Javascript
Unit testing using jasmine in Javascript
Deepak More
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
Jim Lynch
 
Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API - 3 WAYS TO TEST YOUR COLDFUSION API -
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
Jasmine Testing to the Rescue!
Jasmine Testing to the Rescue!Jasmine Testing to the Rescue!
Jasmine Testing to the Rescue!
Christopher Steele
 
An Introduction to AngularJs Unittesting
An Introduction to AngularJs UnittestingAn Introduction to AngularJs Unittesting
An Introduction to AngularJs Unittesting
Inthra onsap
 
What the HTML? - The Holy Grail
What the HTML? - The Holy GrailWhat the HTML? - The Holy Grail
What the HTML? - The Holy Grail
James Ford
 
Unit-testing and E2E testing in JS
Unit-testing and E2E testing in JSUnit-testing and E2E testing in JS
Unit-testing and E2E testing in JS
Michael Haberman
 
Slaven tomac unit testing in angular js
Slaven tomac   unit testing in angular jsSlaven tomac   unit testing in angular js
Slaven tomac unit testing in angular js
Slaven Tomac
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
Troy Miles
 
Ad

Recently uploaded (20)

Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptxFIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptxFIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdfWar_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Data Validation and System Interoperability
Data Validation and System InteroperabilityData Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptxFIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptxFIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdfWar_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Data Validation and System Interoperability
Data Validation and System InteroperabilityData Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 

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 ([email protected]/[email protected])
  • 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