SlideShare a Scribd company logo
Javascript
Test Driven Development
by
A n d r ea P a c i o l l a
15 Feb 2016
1. What does testing mean?
2. AngularJS Environment: Karma & Jasmine
3. Testing Angular - TDD in practice
4. Protractor (preview)
TABLE OF CONTENTS
WHAT DOES TESTING MEAN?
“I will just launch the app and check if everything works.
We have never had any problem this way. ”
“I will just launch the app and check if everything works.
We have never had any problem this way. ”
-- No One Ever
Unit testing improves code’s orthogonality.
Unit testing VS E2E testing
Test with simple javascript:
custom test framework with no DOM dependencies
Simple javascript test with Q-Unit
( functional qunit & DOM manipulation qunit-fixture )
Test with nodeJS
Test with nodeJS
- Asserts
Test with nodeJS
- Asserts
- Testing Anything Protocol (TAP) & tape module
(Fake) TDD
Keep fixing code until you cannot see any bugs by using:
- Console.log
- Dump
- Alert
- Debugger
(REAL) TDD
Write test while developing, test by using dump(), breakpoint,
autotest once the code has been updated, keep track of bug
fixing, test against as many browsers as you can, wait for your
tests to tell you all has gone.
AngularJS Environment: Karma & Jasmine
Karma Task Runner
Setting up
Conf File
Use by CLI & use by IDE
Jasmine Behaviour-Driven Development
beforeEach & afterEach – set up pre & post conditions and will run
before(after) every test in its block.
Describe – wraps a block of related tests. Descriptive name.
It – meant to be read as a sentence
Expect - allows to make assertions (1 assertion per it block)
spies (toHaveBeenCalled)
toBe & toContain
Testing Angular - TDD in practice
setting up
bower install angular angular-mocks
angular mocks: contains useful mocking tools to easily test angularJS
modules. Just include the angular-mocks.js file into your karma-conf.js
Test controllers
Hold an istance of the ctrl, init a scope & test expectations
// get the module that contain the controller
beforeEach(module('todolist'));
// inject the $controller and the rootScope
beforeEach(inject(function ($rootScope, $controller) {
// create a fresh new scope for the controller
scope = $rootScope.$new();
// create a controller with this scope
ctrl = $controller('TodoListController',{$scope: scope});
}));
Test services
use $injector to get an instance of the service
// get the module that contain the service
beforeEach(module('todolist'));
// inject the $injector
beforeEach(inject(function ($injector) {
// use the $injector to get a hold on the service
service = $injector.get(‘ServiceName’);
}));
test directive
Create an element which will host the directive and compile it
with a scope. Trigg the digest too.
// get the module that contain the service
beforeEach(module('todolist'));
// inject the $compile service and the $rootScope
beforeEach(inject(function ($compile, $rootScope) {
// use the $rootScope to create a scope for the directive
scope = $rootScope;
// create an angular element from a HTML string
element = angular.element(‘<div my-directive ></div>’);
// compile the element with the scope
$compile(element)(scope);
scope.$apply()
}));
Test http requests
$httpBackend as fake backend implementation
Given some particular state of my app
set up state, mock or spy functions if necessary
When I call some method
call the method you’re testing
Then that method behaves in a certain way
verify the method did the right thing
Test http requests
// inject the $httpBackend service and the $rootScope
beforeEach(inject(function ($httpBackend) {
// use the $rootScope to create a scope for the directive
httpBackend = $httpBackend;
it("something that make a request", function() {
// expect a request
httpBackend.expectGET(‘api’).respond(200);
// code that make a request
httpBackend.flush(); // do`nt forget to flush..
});
}));
Testing with Yeoman
Testing with Gulp
What about
Mocha, Chai & Sinon?
Mocha (https://mochajs.org ) - is a test framework. Mocha sets
up and describes test suites
Chai (http://chaijs.com ) - is an expectation framework. It
provides convenient helpers to perform all kinds of assertions
against your JavaScript code.
Sinon (http://sinonjs.org ) - is a great JavaScript library for
stubbing and mocking such external dependencies and to keep
control on side effects against them. It helps us to create a
sandbox environment.
Protractor
E2E tests with protractor
Each test is known as integration test, tested directly against
active app
Thank you

More Related Content

What's hot (20)

PDF
Test-Driven Development of AngularJS Applications
FITC
 
PDF
AngularJS Unit Test
Chiew Carol
 
ODP
Jquery- One slide completing all JQuery
Knoldus Inc.
 
PDF
Angularjs - Unit testing introduction
Nir Kaufman
 
PPTX
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
PDF
Unit tests in node.js
Rotem Tamir
 
PPTX
Angular Unit Testing
Avi Engelshtein
 
PDF
Intro to testing Javascript with jasmine
Timothy Oxley
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
PPTX
Angular Unit Testing
Alessandro Giorgetti
 
PDF
Painless JavaScript Testing with Jest
Michał Pierzchała
 
PDF
Unit-testing and E2E testing in JS
Michael Haberman
 
PPTX
Test-Driven JavaScript Development (JavaZone 2010)
Christian Johansen
 
PPTX
Unit testing on mobile apps
Buşra Deniz, CSM
 
PDF
Isomorphic React Apps Testing
Mikhail Larchanka
 
PPTX
AngularJS Unit Testing
Prince Norin
 
PPTX
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
PPTX
Qunit Java script Un
akanksha arora
 
PDF
Automated Testing in Angular Slides
Jim Lynch
 
Test-Driven Development of AngularJS Applications
FITC
 
AngularJS Unit Test
Chiew Carol
 
Jquery- One slide completing all JQuery
Knoldus Inc.
 
Angularjs - Unit testing introduction
Nir Kaufman
 
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Unit tests in node.js
Rotem Tamir
 
Angular Unit Testing
Avi Engelshtein
 
Intro to testing Javascript with jasmine
Timothy Oxley
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Angular Unit Testing
Alessandro Giorgetti
 
Painless JavaScript Testing with Jest
Michał Pierzchała
 
Unit-testing and E2E testing in JS
Michael Haberman
 
Test-Driven JavaScript Development (JavaZone 2010)
Christian Johansen
 
Unit testing on mobile apps
Buşra Deniz, CSM
 
Isomorphic React Apps Testing
Mikhail Larchanka
 
AngularJS Unit Testing
Prince Norin
 
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Qunit Java script Un
akanksha arora
 
Automated Testing in Angular Slides
Jim Lynch
 

Similar to Javascript tdd byandreapaciolla (20)

PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
PPTX
Slaven tomac unit testing in angular js
Slaven Tomac
 
PDF
Angular Application Testing
Troy Miles
 
PDF
Workshop 14: AngularJS Parte III
Visual Engineering
 
PPTX
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
murtazahaveliwala
 
PPTX
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
PDF
Angular Intermediate
LinkMe Srl
 
PDF
Automated testing for client-side - Adam Klein, 500 Tech
Codemotion Tel Aviv
 
PDF
Client side unit tests - using jasmine & karma
Adam Klein
 
PDF
Describe's Full of It's
Jim Lynch
 
PDF
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
PDF
Testing Angular apps_ A complete guide for developers.pdf
Peerbits
 
PDF
Angularjs Testdriven Development Tim Chaplin
alphonzomckinstryew
 
PDF
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
PDF
AngularJS Beginner Day One
Troy Miles
 
PPTX
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
PDF
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
PDF
Unit Testing in Angular
Knoldus Inc.
 
PDF
Testing AngularJS
Jacopo Nardiello
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
Slaven tomac unit testing in angular js
Slaven Tomac
 
Angular Application Testing
Troy Miles
 
Workshop 14: AngularJS Parte III
Visual Engineering
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
murtazahaveliwala
 
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
Angular Intermediate
LinkMe Srl
 
Automated testing for client-side - Adam Klein, 500 Tech
Codemotion Tel Aviv
 
Client side unit tests - using jasmine & karma
Adam Klein
 
Describe's Full of It's
Jim Lynch
 
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
Testing Angular apps_ A complete guide for developers.pdf
Peerbits
 
Angularjs Testdriven Development Tim Chaplin
alphonzomckinstryew
 
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
AngularJS Beginner Day One
Troy Miles
 
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
Unit Testing in Angular
Knoldus Inc.
 
Testing AngularJS
Jacopo Nardiello
 
Ad

Recently uploaded (20)

PPTX
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
Day2 B2 Best.pptx
helenjenefa1
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Hashing Introduction , hash functions and techniques
sailajam21
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Ad

Javascript tdd byandreapaciolla

  • 1. Javascript Test Driven Development by A n d r ea P a c i o l l a 15 Feb 2016
  • 2. 1. What does testing mean? 2. AngularJS Environment: Karma & Jasmine 3. Testing Angular - TDD in practice 4. Protractor (preview) TABLE OF CONTENTS
  • 4. “I will just launch the app and check if everything works. We have never had any problem this way. ”
  • 5. “I will just launch the app and check if everything works. We have never had any problem this way. ” -- No One Ever
  • 6. Unit testing improves code’s orthogonality.
  • 7. Unit testing VS E2E testing
  • 8. Test with simple javascript: custom test framework with no DOM dependencies
  • 9. Simple javascript test with Q-Unit ( functional qunit & DOM manipulation qunit-fixture )
  • 12. Test with nodeJS - Asserts - Testing Anything Protocol (TAP) & tape module
  • 13. (Fake) TDD Keep fixing code until you cannot see any bugs by using: - Console.log - Dump - Alert - Debugger
  • 14. (REAL) TDD Write test while developing, test by using dump(), breakpoint, autotest once the code has been updated, keep track of bug fixing, test against as many browsers as you can, wait for your tests to tell you all has gone.
  • 16. Karma Task Runner Setting up Conf File Use by CLI & use by IDE
  • 17. Jasmine Behaviour-Driven Development beforeEach & afterEach – set up pre & post conditions and will run before(after) every test in its block. Describe – wraps a block of related tests. Descriptive name. It – meant to be read as a sentence Expect - allows to make assertions (1 assertion per it block) spies (toHaveBeenCalled) toBe & toContain
  • 18. Testing Angular - TDD in practice
  • 19. setting up bower install angular angular-mocks angular mocks: contains useful mocking tools to easily test angularJS modules. Just include the angular-mocks.js file into your karma-conf.js
  • 20. Test controllers Hold an istance of the ctrl, init a scope & test expectations // get the module that contain the controller beforeEach(module('todolist')); // inject the $controller and the rootScope beforeEach(inject(function ($rootScope, $controller) { // create a fresh new scope for the controller scope = $rootScope.$new(); // create a controller with this scope ctrl = $controller('TodoListController',{$scope: scope}); }));
  • 21. Test services use $injector to get an instance of the service // get the module that contain the service beforeEach(module('todolist')); // inject the $injector beforeEach(inject(function ($injector) { // use the $injector to get a hold on the service service = $injector.get(‘ServiceName’); }));
  • 22. test directive Create an element which will host the directive and compile it with a scope. Trigg the digest too. // get the module that contain the service beforeEach(module('todolist')); // inject the $compile service and the $rootScope beforeEach(inject(function ($compile, $rootScope) { // use the $rootScope to create a scope for the directive scope = $rootScope; // create an angular element from a HTML string element = angular.element(‘<div my-directive ></div>’); // compile the element with the scope $compile(element)(scope); scope.$apply() }));
  • 23. Test http requests $httpBackend as fake backend implementation Given some particular state of my app set up state, mock or spy functions if necessary When I call some method call the method you’re testing Then that method behaves in a certain way verify the method did the right thing
  • 24. Test http requests // inject the $httpBackend service and the $rootScope beforeEach(inject(function ($httpBackend) { // use the $rootScope to create a scope for the directive httpBackend = $httpBackend; it("something that make a request", function() { // expect a request httpBackend.expectGET(‘api’).respond(200); // code that make a request httpBackend.flush(); // do`nt forget to flush.. }); }));
  • 28. Mocha (https://mochajs.org ) - is a test framework. Mocha sets up and describes test suites Chai (http://chaijs.com ) - is an expectation framework. It provides convenient helpers to perform all kinds of assertions against your JavaScript code. Sinon (http://sinonjs.org ) - is a great JavaScript library for stubbing and mocking such external dependencies and to keep control on side effects against them. It helps us to create a sandbox environment.
  • 30. E2E tests with protractor Each test is known as integration test, tested directly against active app