SlideShare a Scribd company logo
1 of 32
Unit testing
Rich UI brownbags
www.slideshare.net/bmarchal/unit-test-in-javascript
US Software Population
0
10
20
30
40
50
60
70
80
90
100
1950 1965 1980 1995 2010
Development personnel Maintenance personnel
2The economics of software maintenance in the XXI century, Capers Jones, 2006
Benoît Marchal
http://marchal.com
3
Unit tests
Proven, well-known technique
to delay legacy comes
to JavaScript
4
Share RoMa frontend experience
With unit tests
5
Unit tests in JavaScript
Jasmine helps us write formal, executable
specifications. It integrates transparently with
our development methodologies while supporting
us with a set of tools.
6
Find them early, fix them cheaply
0
10
20
30
40
50
60
70
80
90
Introduction of defects
7
Cost to fix
FORMAL SPECIFICATIONS
Delay legacy
How/when/why to use these?
• function union(gsA, gsB, geography) {
// …
}
• this.detach = function() {
// …
}
• function plantATree(inputNodes) {
// …
}
• {
assume: function(uri, id, type) {
// …
}
}
9
Show me an example
• it("fused municipalities", function() {
var seeds = [
item("/antwerpen"), item("/arlon"), item("/brugge"),
item("/gent"), item("/hasselt"), item("/leuven"), item("/liege"),
item("/mons"), item("/namur"), item("/wavre")
],
harvest = {
"/antwerpen": { include: true, children: {} },
"/arlon": { include: true, children: {} },
"/brugge": { include: true, children: {} },
"/gent": { include: true, children: {} },
"/hasselt": { include: true, children: {} },
"/leuven": { include: true, children: {} },
"/liege": { include: true, children: {} },
"/mons": { include: true, children: {} },
"/namur": { include: true, children: {} },
"/wavre": { include: true, children: {} }
};
expect(plantATree(seeds)).toEqual(harvest);
});
10
Formal Specifications
• Formal component specifications
– What is the component supposed to do?
– Executable so always up-to-date
– Specifications “by example”
• Testing is a tool, not a goal
– Alternatives
• Design by contract
• Assertions
• Legacy code: code without tests
– Michael Feathers “Working effectively with legacy code”
– Premature legacy
• Oftentimes easier to rewrite than to understand
• Unit tests protect components
11
Written when…
• Understanding the problem or solution
– Describe what you expect, include edge cases
– Test that you implemented your understanding… for free
– Akin to rubber duck debugging 
– By nature, UI work is iterative
• Teamwork
– Encourage reuse: code samples for now or later
– Ease maintenance: pinpoint source of problems
– Team growth or change
• Refactoring
– Enforce the existing API
– Or highlight where it’s broken
12
Formal specifications
• Find problems early
• Facilitates change
• Simplifies integration
• Documentation
• Design
13
TRANSPARENT
Write it in plain JavaScript
Magically self-healing code
Sadly it’s a myth
15
Documentation exposes equally
The good
• Brilliant solution
• Efficient component
• Stable API
• Beautiful design
• You see it,
– You have confidence
And the not so good
• Defect
• Slow component
• Difficult API
• Odd design
• You see it,
– You can improve it
16
Generic development cycle
Design/
refactor
CodeTest
17
Test cycle
Design/
refactor
Write
tests
Code
Run
(write)
tests
18
SOLID is easier to specify/test*
• Single responsible principle
– A component has a single responsibility
• Open/closed principle
– Open for extension, closed for modification
• Liskov substitution principle
– Objects replaceable with instances of their subtypes
• Interface segregation principle
– Client-specific interfaces over general-purpose ones
• Dependency inversion principle
– Depend upon abstractions, not concretions
• * = see separation of concerns
19
Transparent
• No silver bullet
• Highlights equally strengths and flaws
– So you can see them
• Testable code is more likely to be SOLID
• Fit it in your coding habits/methodologies
20
TOOLS
Making it fit
Jasmine and Karma
22
WebStorm or Grunt/PhantomJS
23
: separation of concerns
Factory
Service
Controller
Filter
Template
Directive
24
model
interaction
view
integration
templates
• it("responds to a click (direct compilation)",
function() {
$rootScope.$digest();
spyOn(scope, "tickle");
browserTrigger(element.children(), "click");
expect(scope.tickle).toHaveBeenCalled();
});
25
jargon
• describe() : specification
• it()
• beforeEach() & afterEach()
• xit() & xdescribe()
• iit() & ddescribe()
• createSpy() & createSpyObj()
• expect()
• jasmine.any()
• matchers
• dependency injection
26
Demo
27
Tools
28
templates
UNIT TESTS IN JAVASCRIPT
Share RoMa experience
Unit tests in JavaScript
Jasmine helps us write formal, executable
specifications. It integrates transparently with
our development methodologies while supporting
us with a set of tools.
30
Check these out
• Guidelines and JavaScript Templates
– http://wiki.netpost/display/ROMA/JS+unit+testing+and+specifications
– http://svn.netpost:10080/svn/rom/branches/DEV_V0003.10.00/
• components/2020/ui/rom-ui-app/test/examples
• Writing testable code
– http://wiki.netpost/display/ROMA/Testing+the+Domain+Model
• Angular guidelines
– https://docs.angularjs.org/guide/unit-testing
• Tools
– http://jasmine.github.io/
– http://karma-runner.github.io/
– http://www.jetbrains.com/webstorm/
– http://gruntjs.com/
– http://phantomjs.org/
31
Delay legacy
Specifications in plain JavaScript
32

More Related Content

What's hot

What's hot (11)

Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking Frameworks
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
Design for Testability
Design for Testability Design for Testability
Design for Testability
 
Creating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsCreating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance Tests
 
BDD with SpecFlow and Selenium
BDD with SpecFlow and SeleniumBDD with SpecFlow and Selenium
BDD with SpecFlow and Selenium
 
Software testing ... who’s responsible is it?
Software testing ... who’s responsible is it?Software testing ... who’s responsible is it?
Software testing ... who’s responsible is it?
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
 
How Spotify Does Test Automation - Kristian Karl
How Spotify Does Test Automation - Kristian KarlHow Spotify Does Test Automation - Kristian Karl
How Spotify Does Test Automation - Kristian Karl
 
Codeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansaiCodeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansai
 
Codeception
CodeceptionCodeception
Codeception
 

Similar to Unit test in JavaScript

Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
Billie Berzinskas
 
Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012
cobyst
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
Bill Buchan
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 

Similar to Unit test in JavaScript (20)

Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
Apex Enterprise Patterns Galore - Boston, MA dev group meeting 062719
Apex Enterprise Patterns Galore - Boston, MA dev group meeting 062719Apex Enterprise Patterns Galore - Boston, MA dev group meeting 062719
Apex Enterprise Patterns Galore - Boston, MA dev group meeting 062719
 
Improving the Design of Existing Software
Improving the Design of Existing SoftwareImproving the Design of Existing Software
Improving the Design of Existing Software
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012
 
AgileDC15 I'm Using Chef So I'm DevOps Right?
AgileDC15 I'm Using Chef So I'm DevOps Right?AgileDC15 I'm Using Chef So I'm DevOps Right?
AgileDC15 I'm Using Chef So I'm DevOps Right?
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondEntity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and Beyond
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Topic production code
Topic production codeTopic production code
Topic production code
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID Principles
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Tips for Developing and Testing IBM HATS Applications
Tips for Developing and Testing IBM HATS ApplicationsTips for Developing and Testing IBM HATS Applications
Tips for Developing and Testing IBM HATS Applications
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practices
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
AD208 - End to End Quality Processes for Top Notch XPages Apps
AD208 - End to End Quality Processes for Top Notch XPages AppsAD208 - End to End Quality Processes for Top Notch XPages Apps
AD208 - End to End Quality Processes for Top Notch XPages Apps
 
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons LearnedITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
ITARC15 Workshop - Architecting a Large Software Project - Lessons Learned
 
The Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance TuningThe Diabolical Developers Guide to Performance Tuning
The Diabolical Developers Guide to Performance Tuning
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Unit test in JavaScript

  • 1. Unit testing Rich UI brownbags www.slideshare.net/bmarchal/unit-test-in-javascript
  • 2. US Software Population 0 10 20 30 40 50 60 70 80 90 100 1950 1965 1980 1995 2010 Development personnel Maintenance personnel 2The economics of software maintenance in the XXI century, Capers Jones, 2006
  • 4. Unit tests Proven, well-known technique to delay legacy comes to JavaScript 4
  • 5. Share RoMa frontend experience With unit tests 5
  • 6. Unit tests in JavaScript Jasmine helps us write formal, executable specifications. It integrates transparently with our development methodologies while supporting us with a set of tools. 6
  • 7. Find them early, fix them cheaply 0 10 20 30 40 50 60 70 80 90 Introduction of defects 7 Cost to fix
  • 9. How/when/why to use these? • function union(gsA, gsB, geography) { // … } • this.detach = function() { // … } • function plantATree(inputNodes) { // … } • { assume: function(uri, id, type) { // … } } 9
  • 10. Show me an example • it("fused municipalities", function() { var seeds = [ item("/antwerpen"), item("/arlon"), item("/brugge"), item("/gent"), item("/hasselt"), item("/leuven"), item("/liege"), item("/mons"), item("/namur"), item("/wavre") ], harvest = { "/antwerpen": { include: true, children: {} }, "/arlon": { include: true, children: {} }, "/brugge": { include: true, children: {} }, "/gent": { include: true, children: {} }, "/hasselt": { include: true, children: {} }, "/leuven": { include: true, children: {} }, "/liege": { include: true, children: {} }, "/mons": { include: true, children: {} }, "/namur": { include: true, children: {} }, "/wavre": { include: true, children: {} } }; expect(plantATree(seeds)).toEqual(harvest); }); 10
  • 11. Formal Specifications • Formal component specifications – What is the component supposed to do? – Executable so always up-to-date – Specifications “by example” • Testing is a tool, not a goal – Alternatives • Design by contract • Assertions • Legacy code: code without tests – Michael Feathers “Working effectively with legacy code” – Premature legacy • Oftentimes easier to rewrite than to understand • Unit tests protect components 11
  • 12. Written when… • Understanding the problem or solution – Describe what you expect, include edge cases – Test that you implemented your understanding… for free – Akin to rubber duck debugging  – By nature, UI work is iterative • Teamwork – Encourage reuse: code samples for now or later – Ease maintenance: pinpoint source of problems – Team growth or change • Refactoring – Enforce the existing API – Or highlight where it’s broken 12
  • 13. Formal specifications • Find problems early • Facilitates change • Simplifies integration • Documentation • Design 13
  • 14. TRANSPARENT Write it in plain JavaScript
  • 16. Documentation exposes equally The good • Brilliant solution • Efficient component • Stable API • Beautiful design • You see it, – You have confidence And the not so good • Defect • Slow component • Difficult API • Odd design • You see it, – You can improve it 16
  • 19. SOLID is easier to specify/test* • Single responsible principle – A component has a single responsibility • Open/closed principle – Open for extension, closed for modification • Liskov substitution principle – Objects replaceable with instances of their subtypes • Interface segregation principle – Client-specific interfaces over general-purpose ones • Dependency inversion principle – Depend upon abstractions, not concretions • * = see separation of concerns 19
  • 20. Transparent • No silver bullet • Highlights equally strengths and flaws – So you can see them • Testable code is more likely to be SOLID • Fit it in your coding habits/methodologies 20
  • 24. : separation of concerns Factory Service Controller Filter Template Directive 24 model interaction view integration
  • 25. templates • it("responds to a click (direct compilation)", function() { $rootScope.$digest(); spyOn(scope, "tickle"); browserTrigger(element.children(), "click"); expect(scope.tickle).toHaveBeenCalled(); }); 25
  • 26. jargon • describe() : specification • it() • beforeEach() & afterEach() • xit() & xdescribe() • iit() & ddescribe() • createSpy() & createSpyObj() • expect() • jasmine.any() • matchers • dependency injection 26
  • 29. UNIT TESTS IN JAVASCRIPT Share RoMa experience
  • 30. Unit tests in JavaScript Jasmine helps us write formal, executable specifications. It integrates transparently with our development methodologies while supporting us with a set of tools. 30
  • 31. Check these out • Guidelines and JavaScript Templates – http://wiki.netpost/display/ROMA/JS+unit+testing+and+specifications – http://svn.netpost:10080/svn/rom/branches/DEV_V0003.10.00/ • components/2020/ui/rom-ui-app/test/examples • Writing testable code – http://wiki.netpost/display/ROMA/Testing+the+Domain+Model • Angular guidelines – https://docs.angularjs.org/guide/unit-testing • Tools – http://jasmine.github.io/ – http://karma-runner.github.io/ – http://www.jetbrains.com/webstorm/ – http://gruntjs.com/ – http://phantomjs.org/ 31
  • 32. Delay legacy Specifications in plain JavaScript 32