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

Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking FrameworksDror Helper
 
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 2013Dror Helper
 
Design for Testability
Design for Testability Design for Testability
Design for Testability Pawel Kalbrun
 
Creating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsCreating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsJez Humble
 
BDD with SpecFlow and Selenium
BDD with SpecFlow and SeleniumBDD with SpecFlow and Selenium
BDD with SpecFlow and SeleniumLiraz Shay
 
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?Manjula03809891
 
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 KarlSmartBear
 
Codeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansaiCodeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansaiFlorent Batard
 

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

Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing SoftwareSteven Smith
 
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 062719BingWang77
 
Improving the Design of Existing Software
Improving the Design of Existing SoftwareImproving the Design of Existing Software
Improving the Design of Existing SoftwareSteven Smith
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing SoftwareSteven Smith
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4Billie Berzinskas
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing SoftwareSteven Smith
 
Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012cobyst
 
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?Rob Brown
 
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 BeyondSteve Westgarth
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing SoftwareSteven Smith
 
Topic production code
Topic production codeTopic production code
Topic production codeKavi Kumar
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesSteven Smith
 
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 HoleNoam Kfir
 
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 ApplicationsStrongback Consulting
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonIneke Scheffers
 
The View - Lotusscript coding best practices
The View - Lotusscript coding best practicesThe View - Lotusscript coding best practices
The View - Lotusscript coding best practicesBill Buchan
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
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 Appsbeglee
 
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 LearnedJoão Pedro Martins
 
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 TuningjClarity
 

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

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

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