SlideShare a Scribd company logo
JASMINE & KARMA
Unit testing your JavaScript code
Maurice de Beijer
Goals
• Understand how to write Jasmine BDD specs
• Run Jasmine specs using Karma
• Test AngularJS code with Jasmine and Karma
Unit testing JavaScript: Jasmine & karma intro
Jasmine
• Test suites describe the functionality
• Using a describe() function
• Can be nested
• Specs test the functionality
• Using an it() function
• Contain one or more expectations
Basic Jasmine tests
Running Jasmine tests
• We need a test runner to execute tests
• The browser
• Visual Studio with R#
• Karma
Successful test in the Browser
Unit testing JavaScript: Jasmine & karma intro
Matchers
• Used to verify expectations
• toBe()
• toEqual()
• toMatch()
• toBeDefined()
• toBeTruthy()
• toContain()
• toBeGreaterThan()
• toBeCloseTo()
• And more
• Can create custom matchers if needed
• You can also throw an Error to fail a test
Jasmine spies
• Can intercept function calls
• Verify if the function was called
• Optionally with specific parameters
• Can be configured to:
• Call original function
• Return a predefined result
• Call a fake function
• Throw an Error
Unit testing JavaScript: Jasmine & karma intro
Karma Setup
• Karma is a NodeJS application
• So first install Node.js
• Install Karma using NPM
• NPM Install Karma-Cli – g
• NPM Install Karma –save-dev
• Karma requires a configuration file
• Create using karma init
• Requires a browser to run tests
• PhantomJS is an invisible browser
• Can use multiple browser at the same time
• Either in the config file
• Or by navigating to http://localhost:9876/
Successful test in Karma
Failing test in Karma
Testing AngularJS code
• Include angular-mocks.js as the last AngularJS script
• Changes some of the normal behavior
• Create Jasmine test as normal
• Add beforeEach() to load module under test
• Use the inject() function to trigger Dependency Injection
• The $controller service is the factory to create controllers
• Usually requires at least a $scope
• Other dependencies can be provided
• If not the normal DI lookup happens.
$httpBackend
• Fake implementation of the XmlHttpRequest
• Expected requests can be configured
• whenGET, whenPOST etc
• expectGET, expectPOST etc
• When... functions are soft requirement
• Do not fail a test
• Will resolve multiple time
• Expect… functions are assertions
• Fail a test is not satisfied
• Appears to the calling code as asynchronous
• Use flush() to resolve requests
Questions?
• Source code on GitHub
• https://github.com/mauricedb/Jasmine-Karma-Intro
Ad

Recommended

Angular Unit Testing
Angular Unit Testing
Shailendra Chauhan
 
Introduction to jest
Introduction to jest
pksjce
 
Unit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
Selenium with java
Selenium with java
Gousalya Ramachandran
 
Unit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Selenium IDE LOCATORS
Selenium IDE LOCATORS
Mindfire Solutions
 
Automation - web testing with selenium
Automation - web testing with selenium
Tzirla Rozental
 
Spring boot
Spring boot
Bhagwat Kumar
 
Unit and integration Testing
Unit and integration Testing
David Berliner
 
Junit
Junit
FAROOK Samath
 
What Is Cucumber?
What Is Cucumber?
QATestLab
 
JavaScript Tutorial
JavaScript Tutorial
Bui Kiet
 
BDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVA
Srinivas Katakam
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
Advanced Javascript
Advanced Javascript
Adieu
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Test Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and Cucumber
Rhoynar Software Consulting
 
Automation testing
Automation testing
Biswajit Pratihari
 
Saving Time By Testing With Jest
Saving Time By Testing With Jest
Ben McCormick
 
The New JavaScript: ES6
The New JavaScript: ES6
Rob Eisenberg
 
Automated Testing vs Manual Testing
Automated Testing vs Manual Testing
Directi Group
 
Jest
Jest
Lucas Lira Gomes
 
Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 
Mutation Testing: Testing your tests
Mutation Testing: Testing your tests
Stephen Leigh
 
Spring boot introduction
Spring boot introduction
Rasheed Waraich
 
Testing Angular
Testing Angular
Lilia Sfaxi
 
Spring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
Adam Klein
 
Story about module management with angular.js
Story about module management with angular.js
David Amend
 

More Related Content

What's hot (20)

Unit and integration Testing
Unit and integration Testing
David Berliner
 
Junit
Junit
FAROOK Samath
 
What Is Cucumber?
What Is Cucumber?
QATestLab
 
JavaScript Tutorial
JavaScript Tutorial
Bui Kiet
 
BDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVA
Srinivas Katakam
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
Advanced Javascript
Advanced Javascript
Adieu
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Test Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and Cucumber
Rhoynar Software Consulting
 
Automation testing
Automation testing
Biswajit Pratihari
 
Saving Time By Testing With Jest
Saving Time By Testing With Jest
Ben McCormick
 
The New JavaScript: ES6
The New JavaScript: ES6
Rob Eisenberg
 
Automated Testing vs Manual Testing
Automated Testing vs Manual Testing
Directi Group
 
Jest
Jest
Lucas Lira Gomes
 
Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 
Mutation Testing: Testing your tests
Mutation Testing: Testing your tests
Stephen Leigh
 
Spring boot introduction
Spring boot introduction
Rasheed Waraich
 
Testing Angular
Testing Angular
Lilia Sfaxi
 
Spring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Unit and integration Testing
Unit and integration Testing
David Berliner
 
What Is Cucumber?
What Is Cucumber?
QATestLab
 
JavaScript Tutorial
JavaScript Tutorial
Bui Kiet
 
BDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVA
Srinivas Katakam
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
Advanced Javascript
Advanced Javascript
Adieu
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Saving Time By Testing With Jest
Saving Time By Testing With Jest
Ben McCormick
 
The New JavaScript: ES6
The New JavaScript: ES6
Rob Eisenberg
 
Automated Testing vs Manual Testing
Automated Testing vs Manual Testing
Directi Group
 
Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 
Mutation Testing: Testing your tests
Mutation Testing: Testing your tests
Stephen Leigh
 
Spring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 

Viewers also liked (20)

Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
Adam Klein
 
Story about module management with angular.js
Story about module management with angular.js
David Amend
 
TDD, unit testing and java script testing frameworks workshop
TDD, unit testing and java script testing frameworks workshop
Sikandar Ahmed
 
Angular testing
Angular testing
Raissa Ferreira
 
Angular 2 - What's new and what's different
Angular 2 - What's new and what's different
Priscila Negreiros
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Testing Javascript with Jasmine
Testing Javascript with Jasmine
Tim Tyrrell
 
JavaScript Unit Testing
JavaScript Unit Testing
L&T Technology Services Limited
 
Practical AngularJS
Practical AngularJS
Wei Ru
 
AngularJS for Beginners
AngularJS for Beginners
Edureka!
 
EasyTest Test Automation Tool Introduction
EasyTest Test Automation Tool Introduction
Zhu Zhong
 
AngularJS Testing Strategies
AngularJS Testing Strategies
njpst8
 
Testing angular js
Testing angular js
galan83
 
Slaven tomac unit testing in angular js
Slaven tomac unit testing in angular js
Slaven Tomac
 
Angularjs practical project experiences with javascript development in a bank
Angularjs practical project experiences with javascript development in a bank
David Amend
 
Jasmine - A BDD test framework for JavaScript
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
Test-Driven Development with TypeScript+Jasmine+AngularJS
Test-Driven Development with TypeScript+Jasmine+AngularJS
SmartOrg
 
AngularJs Crash Course
AngularJs Crash Course
Keith Bloomfield
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
AngularJS Testing
AngularJS Testing
Ahmed Elmehri
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
Adam Klein
 
Story about module management with angular.js
Story about module management with angular.js
David Amend
 
TDD, unit testing and java script testing frameworks workshop
TDD, unit testing and java script testing frameworks workshop
Sikandar Ahmed
 
Angular 2 - What's new and what's different
Angular 2 - What's new and what's different
Priscila Negreiros
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Testing Javascript with Jasmine
Testing Javascript with Jasmine
Tim Tyrrell
 
Practical AngularJS
Practical AngularJS
Wei Ru
 
AngularJS for Beginners
AngularJS for Beginners
Edureka!
 
EasyTest Test Automation Tool Introduction
EasyTest Test Automation Tool Introduction
Zhu Zhong
 
AngularJS Testing Strategies
AngularJS Testing Strategies
njpst8
 
Testing angular js
Testing angular js
galan83
 
Slaven tomac unit testing in angular js
Slaven tomac unit testing in angular js
Slaven Tomac
 
Angularjs practical project experiences with javascript development in a bank
Angularjs practical project experiences with javascript development in a bank
David Amend
 
Jasmine - A BDD test framework for JavaScript
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
Test-Driven Development with TypeScript+Jasmine+AngularJS
Test-Driven Development with TypeScript+Jasmine+AngularJS
SmartOrg
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
Ad

Similar to Unit testing JavaScript: Jasmine & karma intro (20)

Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
Angular Unit Testing
Angular Unit Testing
Avi Engelshtein
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Testing in AngularJS
Testing in AngularJS
Peter Drinnan
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
AngularJS Unit Test
AngularJS Unit Test
Chiew Carol
 
Angular Testing
Angular Testing
Kourosh Sajjadi
 
What the HTML? - The Holy Grail
What the HTML? - The Holy Grail
James Ford
 
Angular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
Unit Testing in AngularJS - CC FE & UX
Unit Testing in AngularJS - CC FE & UX
JWORKS powered by Ordina
 
UI Testing Best Practices - An Expected Journey
UI Testing Best Practices - An Expected Journey
Oren Farhi
 
Configure jasmine and karma for code coverage
Configure jasmine and karma for code coverage
Deepak More
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
Gil Fink
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
An Introduction to AngularJs Unittesting
An Introduction to AngularJs Unittesting
Inthra onsap
 
Front end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
Javascript tdd byandreapaciolla
Javascript tdd byandreapaciolla
Andrea Paciolla
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
Testing in AngularJS
Testing in AngularJS
Peter Drinnan
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
AngularJS Unit Test
AngularJS Unit Test
Chiew Carol
 
What the HTML? - The Holy Grail
What the HTML? - The Holy Grail
James Ford
 
Angular Unit testing.pptx
Angular Unit testing.pptx
RiyaBangera
 
UI Testing Best Practices - An Expected Journey
UI Testing Best Practices - An Expected Journey
Oren Farhi
 
Configure jasmine and karma for code coverage
Configure jasmine and karma for code coverage
Deepak More
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
Gil Fink
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
An Introduction to AngularJs Unittesting
An Introduction to AngularJs Unittesting
Inthra onsap
 
Front end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
Javascript tdd byandreapaciolla
Javascript tdd byandreapaciolla
Andrea Paciolla
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
Ad

More from Maurice De Beijer [MVP] (20)

Production-ready Next.js App with Cursor AI
Production-ready Next.js App with Cursor AI
Maurice De Beijer [MVP]
 
Building Robust Web Applications with Test-Driven Development and Playwright:...
Building Robust Web Applications with Test-Driven Development and Playwright:...
Maurice De Beijer [MVP]
 
Mastering React Server Components and Server Actions in React 19
Mastering React Server Components and Server Actions in React 19
Maurice De Beijer [MVP]
 
Practice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components App
Maurice De Beijer [MVP]
 
A foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software Project
Maurice De Beijer [MVP]
 
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Maurice De Beijer [MVP]
 
Build reliable Svelte applications using Cypress
Build reliable Svelte applications using Cypress
Maurice De Beijer [MVP]
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
Maurice De Beijer [MVP]
 
Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18
Maurice De Beijer [MVP]
 
Building reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and Azure
Maurice De Beijer [MVP]
 
Building large and scalable mission critical applications with React
Building large and scalable mission critical applications with React
Maurice De Beijer [MVP]
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
Maurice De Beijer [MVP]
 
Why I am hooked on the future of React
Why I am hooked on the future of React
Maurice De Beijer [MVP]
 
Building reliable web applications using Cypress
Building reliable web applications using Cypress
Maurice De Beijer [MVP]
 
Getting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent rendering
Maurice De Beijer [MVP]
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred Hitchcock
Maurice De Beijer [MVP]
 
From zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScript
Maurice De Beijer [MVP]
 
Why I am hooked on the future of React
Why I am hooked on the future of React
Maurice De Beijer [MVP]
 
The new React
The new React
Maurice De Beijer [MVP]
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
Maurice De Beijer [MVP]
 
Production-ready Next.js App with Cursor AI
Production-ready Next.js App with Cursor AI
Maurice De Beijer [MVP]
 
Building Robust Web Applications with Test-Driven Development and Playwright:...
Building Robust Web Applications with Test-Driven Development and Playwright:...
Maurice De Beijer [MVP]
 
Mastering React Server Components and Server Actions in React 19
Mastering React Server Components and Server Actions in React 19
Maurice De Beijer [MVP]
 
Practice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components App
Maurice De Beijer [MVP]
 
A foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software Project
Maurice De Beijer [MVP]
 
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Maurice De Beijer [MVP]
 
Build reliable Svelte applications using Cypress
Build reliable Svelte applications using Cypress
Maurice De Beijer [MVP]
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
Maurice De Beijer [MVP]
 
Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18
Maurice De Beijer [MVP]
 
Building reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and Azure
Maurice De Beijer [MVP]
 
Building large and scalable mission critical applications with React
Building large and scalable mission critical applications with React
Maurice De Beijer [MVP]
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
Maurice De Beijer [MVP]
 
Building reliable web applications using Cypress
Building reliable web applications using Cypress
Maurice De Beijer [MVP]
 
Getting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent rendering
Maurice De Beijer [MVP]
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred Hitchcock
Maurice De Beijer [MVP]
 
From zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScript
Maurice De Beijer [MVP]
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
Maurice De Beijer [MVP]
 

Recently uploaded (20)

Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Reimagining Software Development and DevOps with Agentic AI
Reimagining Software Development and DevOps with Agentic AI
Maxim Salnikov
 
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Reimagining Software Development and DevOps with Agentic AI
Reimagining Software Development and DevOps with Agentic AI
Maxim Salnikov
 
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Simplify Task, Team, and Project Management with Orangescrum Work
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
Azure AI Foundry: The AI app and agent factory
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 

Unit testing JavaScript: Jasmine & karma intro

  • 1. JASMINE & KARMA Unit testing your JavaScript code Maurice de Beijer
  • 2. Goals • Understand how to write Jasmine BDD specs • Run Jasmine specs using Karma • Test AngularJS code with Jasmine and Karma
  • 4. Jasmine • Test suites describe the functionality • Using a describe() function • Can be nested • Specs test the functionality • Using an it() function • Contain one or more expectations
  • 6. Running Jasmine tests • We need a test runner to execute tests • The browser • Visual Studio with R# • Karma
  • 7. Successful test in the Browser
  • 9. Matchers • Used to verify expectations • toBe() • toEqual() • toMatch() • toBeDefined() • toBeTruthy() • toContain() • toBeGreaterThan() • toBeCloseTo() • And more • Can create custom matchers if needed • You can also throw an Error to fail a test
  • 10. Jasmine spies • Can intercept function calls • Verify if the function was called • Optionally with specific parameters • Can be configured to: • Call original function • Return a predefined result • Call a fake function • Throw an Error
  • 12. Karma Setup • Karma is a NodeJS application • So first install Node.js • Install Karma using NPM • NPM Install Karma-Cli – g • NPM Install Karma –save-dev • Karma requires a configuration file • Create using karma init • Requires a browser to run tests • PhantomJS is an invisible browser • Can use multiple browser at the same time • Either in the config file • Or by navigating to http://localhost:9876/
  • 15. Testing AngularJS code • Include angular-mocks.js as the last AngularJS script • Changes some of the normal behavior • Create Jasmine test as normal • Add beforeEach() to load module under test • Use the inject() function to trigger Dependency Injection • The $controller service is the factory to create controllers • Usually requires at least a $scope • Other dependencies can be provided • If not the normal DI lookup happens.
  • 16. $httpBackend • Fake implementation of the XmlHttpRequest • Expected requests can be configured • whenGET, whenPOST etc • expectGET, expectPOST etc • When... functions are soft requirement • Do not fail a test • Will resolve multiple time • Expect… functions are assertions • Fail a test is not satisfied • Appears to the calling code as asynchronous • Use flush() to resolve requests
  • 17. Questions? • Source code on GitHub • https://github.com/mauricedb/Jasmine-Karma-Intro