SlideShare a Scribd company logo
Unit Testing JavaScript
Keir Bowden
CTO, BrightGen
@bob_buzzard
Unit Tests – They’re not just for Apex
• Introduction
• Tools
• Visualforce
• Lightning Components
• Q & A
What we’ll cover
Introduction
Why write unit tests
• Give confidence
• Reduce cost of bugs
• Produce testable code
• Promote refactoring
Introduction
No test context for client
• Changes aren’t rolled back
Side effects
• DOM manipulation
• Server calls
Challenges
JavaScript on Salesforce
Unobtrusive JavaScript
Avoid anonymous functions
Functional decomposition
• Function to decide on action
• Function to take the action
Writing Testable Code
function redirectToURL(url) {
if (“#”===url.charAt(0)) {
window.location.hash=url;
} else if (“/”===url.charAt(0))
window.location.pathname=url;
}
....
Github: bobbuzz.me.uk/JSUTSF
Public Site
• Jobs:
• bobbuzz.me.uk/1Ad25OG
• Tests:
• bobbuzz.me.uk/1PvhII3
Demo App
Tools
Pure JavaScript Unit Test Framework
Executes in-browser
From the developers of jQuery
QUnit
<apex:includeScript
value="{!$Resource.qunitJS}"/>
<apex:stylesheet
value="{!$Resource.qunitCSS}"/>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
Test markup container
Module – group of tests
Test – single test
Assert – confirm behaviour
Concepts
QUnit
QUnit.module("JOBLIST", {
beforeEach: function(assert) {
// setup code
},
afterEach : function(assert) {
// teardown code
}
});
QUnit.test("Show Progress", function( assert ) {
jobCtrl.showProgress();
});
assert.equal(jobCtrl.progressCount, 1,
"Progress count is one");
Pure JavaScript
Works with any test framework
Three key concepts
• Spies – records execution, parameters
• Stubs – Spies + replace functions
• Mocks – Stubs + expected behaviour
Sinon.JS
Sinon Stub
this.searchStub = sinon.stub(jobCtrl,
"doSearch");
var jobCtrl={
doSearch : function(ev) {
// remote call to server
}
};
jobCtrl.doSearch(event);
doSearch : function() {
// sinon stub code
}
assert.equal(this.searchStub.callCount,1,
"search called once");
this.searchStub.restore();
doSearch : function(ev) {
// remote call to server
}
$('#searchbtn').on("click",
jobCtrl.doSearch);
Code coverage!
Pure JavaScript
Works with QUnit, Mocha and Jasmine
JavaScript must be external
Blanket.JS
<apex:includeScript value="{!$Resource.blanketJS}"/>
<script src="{!$Resource.JobsJS}" data-cover="true"></script>
Blanket.JS
Visualforce
Dedicated test page
Replicate markup as required
Include JavaScript to be tested
Can capture coverage
Demo
Visualforce
Lightning Components
Components are isolated
JavaScript in controller + helper
Cannot capture coverage
Markup is fixed
How to trigger tests?
Challenges
Lightning Components
Qunit tests in helper function
• Controller method to execute helper function
Lightning test application (page equivalent)
Tests executed via event
• Fired by application
• Handled by component under test
Demo
My Solution!
Lightning Components
Unobtrusive JavaScript
Functional decomposition
Avoid anonymous functions
No client-side test context
Stubs are your best friend
Takeaways
Q & A
Keir Bowden
CTO, BrightGen
@bob_buzzard

More Related Content

PPTX
Responsive App Design with the Salesforce Lightning Design System
PPTX
JavaScript Unit Testing
PDF
Agile Testing
PDF
Better Page Object Handling with Loadable Component Pattern - SQA Days 20, Be...
PDF
Behavior-Driven Development and Automation Testing Using Cucumber Framework W...
PDF
JIRA Performance Testing in Pictures - Edward Bukoski Michael March
PDF
Patterns and practices for building enterprise-scale HTML5 apps
PDF
Lean Quality & Engineering
Responsive App Design with the Salesforce Lightning Design System
JavaScript Unit Testing
Agile Testing
Better Page Object Handling with Loadable Component Pattern - SQA Days 20, Be...
Behavior-Driven Development and Automation Testing Using Cucumber Framework W...
JIRA Performance Testing in Pictures - Edward Bukoski Michael March
Patterns and practices for building enterprise-scale HTML5 apps
Lean Quality & Engineering

What's hot (20)

PPTX
Web based automation testing on Node.js environment
PPT
Test Automation With Cucumber JVM, Selenium, and Mocha
PPTX
ASP.NET & Unit Testing
PPTX
ASP.NET 5 & Unit Testing
PDF
Code stock
PDF
Cucumber ppt
PPTX
QA Automation testing online training
PPTX
Lap Around Visual Studio 2010 Ultimate And TFS 2010
PPTX
SenchaCon 2016: Developing and Delivering Quality Code, Frequently - Neil Manvar
PDF
Web Application Testing with Selenium
PPTX
Get responsive with Galen
PPTX
Writing better tests for your java script app
PPTX
QAorHighway2016
PPT
Automated Testing With Jasmine, PhantomJS and Jenkins
PPTX
Visual AI Enhanced Testing on Dynamic Websites
PPTX
Load testing with Visual Studio and Azure - Andrew Siemer
PPTX
Udvid din test portefølje med coded ui test og cloud load test
PPTX
Visual Studio 2010 Testing for Developers
PDF
Patterns of a "Good" Test Automation Framework, Locators & Data
PDF
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
Web based automation testing on Node.js environment
Test Automation With Cucumber JVM, Selenium, and Mocha
ASP.NET & Unit Testing
ASP.NET 5 & Unit Testing
Code stock
Cucumber ppt
QA Automation testing online training
Lap Around Visual Studio 2010 Ultimate And TFS 2010
SenchaCon 2016: Developing and Delivering Quality Code, Frequently - Neil Manvar
Web Application Testing with Selenium
Get responsive with Galen
Writing better tests for your java script app
QAorHighway2016
Automated Testing With Jasmine, PhantomJS and Jenkins
Visual AI Enhanced Testing on Dynamic Websites
Load testing with Visual Studio and Azure - Andrew Siemer
Udvid din test portefølje med coded ui test og cloud load test
Visual Studio 2010 Testing for Developers
Patterns of a "Good" Test Automation Framework, Locators & Data
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
Ad

Viewers also liked (20)

PPTX
Lean Startup for AaltoES Summer of Startups
PPT
Slide 04 - Otaciso, Digelvânia e Silvana
PDF
딥 시큐리티
PPTX
Brightspace Spring Release 2016
 
ODT
8281 Gefahr der Überheblichkeit ....
PPT
Spring 2011 Current Projects
PDF
CA concepts, principles and practices Kitui
PDF
Инновации в рекламном бизнесе
PDF
Open Government Data - Zwei Beispiele der Community-Einbindung
PPTX
Ana maria araujo calderon trabajo social redvolucion leidis
PDF
3 ways to value your business
PDF
GBI 2016 Asia - Hong Kong Launch Presentation 270916
PDF
Golden rules from online community facilitators
PDF
Enterprise Mobility Transforming Public Service and Citizen Engagement
PDF
Strata Singapore: Gearpump Real time DAG-Processing with Akka at Scale
PPTX
20151205japanr
PPS
Iconic AP images of the 20th century
PPTX
Android app development – what you should not
PPTX
How Reebok Uses Employee Generated Content to Amplify Advocacy
PPTX
Market Opportunity
Lean Startup for AaltoES Summer of Startups
Slide 04 - Otaciso, Digelvânia e Silvana
딥 시큐리티
Brightspace Spring Release 2016
 
8281 Gefahr der Überheblichkeit ....
Spring 2011 Current Projects
CA concepts, principles and practices Kitui
Инновации в рекламном бизнесе
Open Government Data - Zwei Beispiele der Community-Einbindung
Ana maria araujo calderon trabajo social redvolucion leidis
3 ways to value your business
GBI 2016 Asia - Hong Kong Launch Presentation 270916
Golden rules from online community facilitators
Enterprise Mobility Transforming Public Service and Citizen Engagement
Strata Singapore: Gearpump Real time DAG-Processing with Akka at Scale
20151205japanr
Iconic AP images of the 20th century
Android app development – what you should not
How Reebok Uses Employee Generated Content to Amplify Advocacy
Market Opportunity
Ad

Similar to JavaScript Unit Testing (20)

PPT
Js unit testing
PDF
Javascript-heavy Salesforce Applications
PPTX
Episode 5 - Writing unit tests in Salesforce
PPTX
JavaScript Unit Testing
PDF
Unit Testing in JavaScript
PPTX
Test-Driven Development
PPT
Javascript unit testing framework
PPT
8 - Javascript unit testing framework
PDF
Efficient JavaScript Unit Testing, JavaOne China 2013
PDF
JUnit with_mocking
PDF
Quick tour to front end unit testing using jasmine
PDF
Qunit tutorial
PDF
Efficient JavaScript Unit Testing, May 2012
PPTX
An Introduction to Unit Testing
PDF
Unit testing
PDF
Introduction to test automation in java and php
PDF
Efficient JavaScript Unit Testing, March 2013
PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
PDF
Unit testing (Exploring the other side as a tester)
PPTX
Level Up Your Salesforce Unit Testing
Js unit testing
Javascript-heavy Salesforce Applications
Episode 5 - Writing unit tests in Salesforce
JavaScript Unit Testing
Unit Testing in JavaScript
Test-Driven Development
Javascript unit testing framework
8 - Javascript unit testing framework
Efficient JavaScript Unit Testing, JavaOne China 2013
JUnit with_mocking
Quick tour to front end unit testing using jasmine
Qunit tutorial
Efficient JavaScript Unit Testing, May 2012
An Introduction to Unit Testing
Unit testing
Introduction to test automation in java and php
Efficient JavaScript Unit Testing, March 2013
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Unit testing (Exploring the other side as a tester)
Level Up Your Salesforce Unit Testing

More from Keir Bowden (20)

PDF
LC 2022 - Second Generation Packaging
PDF
Mutation Testing.pdf
PDF
London Non-Profit Spring 22 Salesforce Release
PDF
London Salesforce Developer January 2022
PDF
Winter 22 for Developers
PDF
Londons Calling 2021
PPTX
London Salesforce Developers TDX 20 Global Gathering
PDF
Helsinki developer group march 2020
PDF
London's calling 2020 Documentor Plug-In
PDF
Lightning User Interface Testing with Selenium and Node JS
PDF
Salesforce CLI Cheat Sheet
PDF
Salesforce Spring 19 release top 10 Features
PDF
UI Testing with Selenium and Node - London's Calling 2019
PPTX
Salesforce Winter 19 release top 10 features
PDF
Quickstart Templates with the Salesforce CLI
PDF
TrailheaDX Global Gathering London 2018
PDF
Getting started with public speaking
PDF
Salesforce CLI
PPTX
London's Calling 2018 - No Connection, No Problem
PPTX
No Connection? No Problem!
LC 2022 - Second Generation Packaging
Mutation Testing.pdf
London Non-Profit Spring 22 Salesforce Release
London Salesforce Developer January 2022
Winter 22 for Developers
Londons Calling 2021
London Salesforce Developers TDX 20 Global Gathering
Helsinki developer group march 2020
London's calling 2020 Documentor Plug-In
Lightning User Interface Testing with Selenium and Node JS
Salesforce CLI Cheat Sheet
Salesforce Spring 19 release top 10 Features
UI Testing with Selenium and Node - London's Calling 2019
Salesforce Winter 19 release top 10 features
Quickstart Templates with the Salesforce CLI
TrailheaDX Global Gathering London 2018
Getting started with public speaking
Salesforce CLI
London's Calling 2018 - No Connection, No Problem
No Connection? No Problem!

Recently uploaded (20)

PPTX
Funds Management Learning Material for Beg
PPTX
Digital Literacy And Online Safety on internet
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
newyork.pptxirantrafgshenepalchinachinane
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
DOC
Rose毕业证学历认证,利物浦约翰摩尔斯大学毕业证国外本科毕业证
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPT
Ethics in Information System - Management Information System
PDF
Introduction to the IoT system, how the IoT system works
PPTX
Database Information System - Management Information System
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Introduction to cybersecurity and digital nettiquette
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
Funds Management Learning Material for Beg
Digital Literacy And Online Safety on internet
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Slides PPTX World Game (s) Eco Economic Epochs.pptx
newyork.pptxirantrafgshenepalchinachinane
SAP Ariba Sourcing PPT for learning material
Unit-1 introduction to cyber security discuss about how to secure a system
Rose毕业证学历认证,利物浦约翰摩尔斯大学毕业证国外本科毕业证
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
presentation_pfe-universite-molay-seltan.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
Design_with_Watersergyerge45hrbgre4top (1).ppt
Ethics in Information System - Management Information System
Introduction to the IoT system, how the IoT system works
Database Information System - Management Information System
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Introduction to cybersecurity and digital nettiquette
SASE Traffic Flow - ZTNA Connector-1.pdf

JavaScript Unit Testing

  • 1. Unit Testing JavaScript Keir Bowden CTO, BrightGen @bob_buzzard Unit Tests – They’re not just for Apex
  • 2. • Introduction • Tools • Visualforce • Lightning Components • Q & A What we’ll cover
  • 4. Why write unit tests • Give confidence • Reduce cost of bugs • Produce testable code • Promote refactoring Introduction
  • 5. No test context for client • Changes aren’t rolled back Side effects • DOM manipulation • Server calls Challenges JavaScript on Salesforce
  • 6. Unobtrusive JavaScript Avoid anonymous functions Functional decomposition • Function to decide on action • Function to take the action Writing Testable Code function redirectToURL(url) { if (“#”===url.charAt(0)) { window.location.hash=url; } else if (“/”===url.charAt(0)) window.location.pathname=url; } ....
  • 7. Github: bobbuzz.me.uk/JSUTSF Public Site • Jobs: • bobbuzz.me.uk/1Ad25OG • Tests: • bobbuzz.me.uk/1PvhII3 Demo App
  • 9. Pure JavaScript Unit Test Framework Executes in-browser From the developers of jQuery QUnit <apex:includeScript value="{!$Resource.qunitJS}"/> <apex:stylesheet value="{!$Resource.qunitCSS}"/> <div id="qunit"></div> <div id="qunit-fixture"></div> Test markup container
  • 10. Module – group of tests Test – single test Assert – confirm behaviour Concepts QUnit QUnit.module("JOBLIST", { beforeEach: function(assert) { // setup code }, afterEach : function(assert) { // teardown code } }); QUnit.test("Show Progress", function( assert ) { jobCtrl.showProgress(); }); assert.equal(jobCtrl.progressCount, 1, "Progress count is one");
  • 11. Pure JavaScript Works with any test framework Three key concepts • Spies – records execution, parameters • Stubs – Spies + replace functions • Mocks – Stubs + expected behaviour Sinon.JS
  • 12. Sinon Stub this.searchStub = sinon.stub(jobCtrl, "doSearch"); var jobCtrl={ doSearch : function(ev) { // remote call to server } }; jobCtrl.doSearch(event); doSearch : function() { // sinon stub code } assert.equal(this.searchStub.callCount,1, "search called once"); this.searchStub.restore(); doSearch : function(ev) { // remote call to server } $('#searchbtn').on("click", jobCtrl.doSearch);
  • 13. Code coverage! Pure JavaScript Works with QUnit, Mocha and Jasmine JavaScript must be external Blanket.JS <apex:includeScript value="{!$Resource.blanketJS}"/> <script src="{!$Resource.JobsJS}" data-cover="true"></script>
  • 16. Dedicated test page Replicate markup as required Include JavaScript to be tested Can capture coverage Demo Visualforce
  • 18. Components are isolated JavaScript in controller + helper Cannot capture coverage Markup is fixed How to trigger tests? Challenges Lightning Components
  • 19. Qunit tests in helper function • Controller method to execute helper function Lightning test application (page equivalent) Tests executed via event • Fired by application • Handled by component under test Demo My Solution! Lightning Components
  • 20. Unobtrusive JavaScript Functional decomposition Avoid anonymous functions No client-side test context Stubs are your best friend Takeaways
  • 21. Q & A Keir Bowden CTO, BrightGen @bob_buzzard