SlideShare a Scribd company logo
Testing!
AngularJS
Jacopo Nardiello
Padawan Programmer
Presentation code on Github:
github.com/jnardiello/angularjsday-testing-angular
Why?
Fail in
Production
Fail in dev
Thanks to testing
we…
fail fast
No Debugging
Machines handles
bug hunting
Tests are a tool to handle
complexity
Angular is
no exception
Rock solid code
Benefits
Fast Dev Cycle
Benefits
Rock solid code
Relaxed Team
Benefits
Fast Dev Cycle
Rock solid code
Relaxed Team
Benefits
Fast Dev Cycle
Rock solid code
Relaxed PM
Testing Javascript
Testing Javascript
describe(“…”, function() {
it(“should do something", function() {
expect(true).toBe(true);
});
});
Testing Javascript
describe(“…”, function() {
it(“should do something", function() {
expect(true).toBe(true);
});
});
Types of tests
Unit tests
• Small portions of code
• Code is isolated
• Quick and easy
Integration tests
Interaction between elements
• From the product
owner point of view
• They are
(computationally)
expensive and slow
Acceptance tests
Angular is special
Misko Hevery
“Agile Coach at Google where he is
responsible for coaching Googlers to
maintain the high level of automated
testing culture”
- misko.hevery.com/about/
Misko Hevery
+
“Angular is written with
testability in mind”
- Angular Doc
Why is Angular easily
testable?
Dependency
Injection
DI
As a Pattern Framework
DI as Pattern
function Car() {
var wheel = new Wheel();
var engine = Engine.getInstance();
var door = app.get(‘Door’);
!
this.move = function() {
engine.on();
wheel.rotate();
door.open();
}
}
DI as Pattern
function Car() {
var wheel = new Wheel();
var engine = Engine.getInstance();
var door = app.get(‘Door’);
!
this.move = function() {
engine.on();
wheel.rotate();
door.open();
}
}
DI as Pattern
function Car(wheel, engine, door) {
this.move = function() {
engine.on();
wheel.rotate();
door.open();
}
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
The problem
function main() {
var fuel = new Fuel();
var electricity = new Electricity();
var engine = new Engine(fuel);
var door = new Door(Electricity);
var wheel = new Wheel();
var car = new Car(wheel, engine, door);
car.move();
}
DI as framework
function main() {
var injector = new Injector(….);
var car = injector.get(Car);
car.move();
}
DI as framework
function main() {
var injector = new Injector(….);
var car = injector.get(Car);
car.move();
}
Car.$inject = [‘wheel’, ‘engine’, ‘door’];
function Car(wheel, engine, door) {
this.move = function() {
…
}
}
DI as framework
function main() {
var injector = new Injector(….);
var car = injector.get(Car);
car.move();
}
Car.$inject = [‘wheel’, ‘engine’, ‘door’];
function Car(wheel, engine, door) {
this.move = function() {
…
}
}
Angular testability
is super-heroic!
but…
“you still have to do the right thing.”
- Angular Doc
Testability
1. Don’t use new
2. Don’t use globals
The Angular
Way
Solid structured
code
Testing components
Controller
function RocketCtrl($scope) {
$scope.maxFuel = 100;
$scope.finalCheck = function() {
if ($scope.currentFuel < $scope.maxFuel) {
$scope.check = ‘ko’;
} else {
$scope.check = 'ok';
}
};
}
Controller
function RocketCtrl($scope) {
$scope.maxFuel = 100;
$scope.finalCheck = function() {
if ($scope.currentFuel < $scope.maxFuel) {
$scope.check = ‘ko’;
} else {
$scope.check = 'ok';
}
};
}
var $scope = {};
var rc = $controller(
'RocketCtrl',
{ $scope: $scope }
);
!
$scope.currentFuel = 80;
$scope.finalCheck();
expect($scope.check).toEqual('ko');
Controller Test
Directive
app.directive('rocketLaunchPad', function () {
return {
restrict: 'AE',
replace: true,
template:
‘<rocket-launch-pad>
Rocket here
<rocket-launch-pad>’
};
});
Directive Test
it(‘Check launchpad was installed', function() {
var element = $compile(“<rocket-launch-pad></
rocket-launch-pad>”)($rootScope);
!
expect(element.html()).toContain("Rocket here");
});
Filter Test
.filter('i18n', function() {
return function (str) {
return translations.hasOwnProperty(str)
&& translations[str]
|| str;
}
})
var length = $filter('i18n');
expect(i18n(‘ciao’)).toEqual(‘hi’);
expect(length(‘abc’)).toEqual(‘abc');
Tools
Testing AngularJS
Karma
Protractor
Test Runner
Karma
Run tests against real browsers
Karma
Run tests against real browsers
Unit tests
Config file
> karma init
Config file
> karma init
- frameworks: [‘jasmine’]
Config file
> karma init
- frameworks: [‘jasmine’]
- autoWatch: true
Config file
> karma init
- frameworks: [‘jasmine’]
- files: [
‘../tests/controllerSpec.js’
],
- autoWatch: true
Config file
> karma init
- frameworks: [‘jasmine’]
- files: [
‘../tests/controllerSpec.js’
],
- autoWatch: true
- browsers: ['Chrome']
Using Karma
> karma start config.js
Using Karma
> karma start config.js
• From the product
owner point of view
• They are
(computationally)
expensive and slow
Acceptance tests
• They can be very
slow
• Hard to write
• Hard to keep
updated
Acceptance tests
Protractor
E2E Angular Testing
Angular wrapper for WebDriver
Anatomy of a E2E test
describe(‘…’, function() {
it(‘…’, function() {
browser.get(‘…’);
element(by.model(‘…’)).sendKeys(..);
!
var calculate = element(by.binding(‘…’));
!
expect(some.method()).toEqual(..);
});
});
Global Variables
describe(‘…’, function() {
it(‘…’, function() {
browser.get(‘…’);
element(by.model(‘…’)).sendKeys(..);
!
var calculate = element(by.binding(‘…’));
!
expect(some.method()).toEqual(..);
});
});
Global Variables
describe(‘…’, function() {
it(‘…’, function() {
browser.get(‘…’);
element(by.model(‘…’)).sendKeys(..);
!
var calculate = element(by.binding(‘…’));
!
expect(some.method()).toEqual(..);
});
});
Super-Happy Panda!
Page Objects
Protractor provides
Page Objects
Protractor provides
Debugging with superpowers
Page Objects
Protractor provides
Debugging with superpowers
Angular-specific functions
Testing AngularJS
Get your hands dirty!
https://github.com/jnardiello/angularjsday-testing-angular
Tools in action - http://vimeo.com/86816782
Jacopo Nardiello
Twitter: @jnardiello
Say hi!
Jacopo Nardiello
Twitter: @jnardiello
Questions?

More Related Content

What's hot (20)

PDF
Unit tests in node.js
Rotem Tamir
 
PPTX
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
OPITZ CONSULTING Deutschland
 
PDF
Angular Testing
Priscila Negreiros
 
PDF
Redux Sagas - React Alicante
Ignacio Martín
 
PDF
Nativescript angular
Christoffer Noring
 
PDF
React lecture
Christoffer Noring
 
PDF
Evan Schultz - Angular Camp - ng2-redux
Evan Schultz
 
PDF
Describe's Full of It's
Jim Lynch
 
PDF
Sane Sharding with Akka Cluster
miciek
 
PDF
Integrating React.js with PHP projects
Ignacio Martín
 
PDF
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
PDF
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
PDF
Testing React hooks with the new act function
Daniel Irvine
 
PDF
Redux saga: managing your side effects. Also: generators in es6
Ignacio Martín
 
PPTX
Firebase ng2 zurich
Christoffer Noring
 
PDF
Using React, Redux and Saga with Lottoland APIs
Mihail Gaberov
 
PDF
Angular for Java Enterprise Developers: Oracle Code One 2018
Loiane Groner
 
PPTX
Qunit Java script Un
akanksha arora
 
PPTX
Full Stack Unit Testing
GlobalLogic Ukraine
 
PDF
Async JavaScript Unit Testing
Mihail Gaberov
 
Unit tests in node.js
Rotem Tamir
 
Test-driven JavaScript Development - OPITZ CONSULTING - Tobias Bosch - Stefa...
OPITZ CONSULTING Deutschland
 
Angular Testing
Priscila Negreiros
 
Redux Sagas - React Alicante
Ignacio Martín
 
Nativescript angular
Christoffer Noring
 
React lecture
Christoffer Noring
 
Evan Schultz - Angular Camp - ng2-redux
Evan Schultz
 
Describe's Full of It's
Jim Lynch
 
Sane Sharding with Akka Cluster
miciek
 
Integrating React.js with PHP projects
Ignacio Martín
 
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
Testing React hooks with the new act function
Daniel Irvine
 
Redux saga: managing your side effects. Also: generators in es6
Ignacio Martín
 
Firebase ng2 zurich
Christoffer Noring
 
Using React, Redux and Saga with Lottoland APIs
Mihail Gaberov
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Loiane Groner
 
Qunit Java script Un
akanksha arora
 
Full Stack Unit Testing
GlobalLogic Ukraine
 
Async JavaScript Unit Testing
Mihail Gaberov
 

Similar to Testing AngularJS (20)

PDF
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
PDF
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Robot Media
 
PDF
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
PDF
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
PDF
JavaScript Functions
Colin DeCarlo
 
PPTX
Decoding Kotlin - Your guide to solving the mysterious in Kotlin - JNation2025
João Esperancinha
 
PPTX
Build Lightweight Web Module
Morgan Cheng
 
ODP
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
PDF
JavaScript Core
François Sarradin
 
KEY
Unit testing en iOS @ MobileCon Galicia
Robot Media
 
PPT
Automated javascript unit testing
ryan_chambers
 
PDF
Construire une application JavaFX 8 avec gradle
Thierry Wasylczenko
 
PDF
Secrets of JavaScript Libraries
jeresig
 
PDF
Functional Javascript
guest4d57e6
 
PDF
Understanding JavaScript Testing
jeresig
 
PDF
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
PPT
AngularJS Testing Strategies
njpst8
 
PDF
Javascript tdd byandreapaciolla
Andrea Paciolla
 
PPTX
Slaven tomac unit testing in angular js
Slaven Tomac
 
ZIP
Javascript Everywhere
Pascal Rettig
 
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Robot Media
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
JavaScript Functions
Colin DeCarlo
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin - JNation2025
João Esperancinha
 
Build Lightweight Web Module
Morgan Cheng
 
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
JavaScript Core
François Sarradin
 
Unit testing en iOS @ MobileCon Galicia
Robot Media
 
Automated javascript unit testing
ryan_chambers
 
Construire une application JavaFX 8 avec gradle
Thierry Wasylczenko
 
Secrets of JavaScript Libraries
jeresig
 
Functional Javascript
guest4d57e6
 
Understanding JavaScript Testing
jeresig
 
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
AngularJS Testing Strategies
njpst8
 
Javascript tdd byandreapaciolla
Andrea Paciolla
 
Slaven tomac unit testing in angular js
Slaven Tomac
 
Javascript Everywhere
Pascal Rettig
 
Ad

More from Jacopo Nardiello (7)

PDF
The Art of Cloud Native Defense on Kubernetes
Jacopo Nardiello
 
PDF
Tales of the mythical cloud-native platform - Container day 2022
Jacopo Nardiello
 
PDF
Breaking the monolith
Jacopo Nardiello
 
PDF
Monitoring Cloud Native Applications with Prometheus
Jacopo Nardiello
 
PDF
Kubernetes 101
Jacopo Nardiello
 
PDF
Becoming a developer
Jacopo Nardiello
 
PDF
Eventsourcing with PHP and MongoDB
Jacopo Nardiello
 
The Art of Cloud Native Defense on Kubernetes
Jacopo Nardiello
 
Tales of the mythical cloud-native platform - Container day 2022
Jacopo Nardiello
 
Breaking the monolith
Jacopo Nardiello
 
Monitoring Cloud Native Applications with Prometheus
Jacopo Nardiello
 
Kubernetes 101
Jacopo Nardiello
 
Becoming a developer
Jacopo Nardiello
 
Eventsourcing with PHP and MongoDB
Jacopo Nardiello
 
Ad

Recently uploaded (20)

PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 

Testing AngularJS