SlideShare a Scribd company logo
Keir Bowden
CTO, BrightGen
@bob_buzzard
Unit Testing Lightning Components
With Jasmine
Keir Bowden
@bob_buzzard
CTO, BrightGen
PLACE COMPANY LOGO
HERE
Introduction
● Give confidence
● Reduce cost of bugs
● Produce Testable Code
● Promote refactoring
Why Write Unit Tests
JavaScript Testing on Salesforce
● No test context for client
○ Changes aren't rolled back
● Side effects
○ DOM manipulation
○ Server side actions
● You don't have to!
Challenges
Techniques
● Use unobtrusive JavaScript
● Avoid anonymous functions
○ Can't test in isolation
○ Have to execute action
Write Testable Code
Techniques
● Decompose functions
○ Function to decide on action
○ Function to execute the action
Write Testable Code
Demo App
Github repository
http://bobbuzz.me.uk/UTLCJSM
Job Board
Jasmine
Jasmine
● Pure JavaScript testing framework
● Executes in-browser
○ Execute tests from anywhere
○ Lightning Component JavaScript only executable from browser
● Open source : http://bobbuzz.me.uk/2bVjTEr
● Minor changes for locker service
Overview
Jasmine
● Behaviour-driven development testing framework
○ Given the job page is open
When the user searches for jobs
Then the search method should be invoked
● Also known as Setup (Given) - Exercise (When) - Verify (Then)
Overview
Jasmine
● Suite
○ Collection of tests
○ describe function
● Spec
○ Single test
○ it function
● Expectation
○ Verifies behaviour (equivalent of assert in Apex)
○ expect function
○ Chained with matcher function
Features
Jasmine
● Setup/Teardown
○ beforeAll - called once, before first spec
○ afterAll - called once, after last spec
○ beforeEach - called before each spec
○ afterEach - called after each spec
● Spies
○ Stubs function
○ Tracks calls
○ Tracks arguments
Features
Jasmine
Spies
Reset when spec/suite completes
doSearch function
calls getJobs
getJobs executes
server side action
spy on the getJobs function
Jasmine spy code replaces getJobs
implementation - no server side action
interrogate spy to confirm behaviour
Jasmine
● Object with functions to handle Jasmine events
○ e.g. JasmineStarted, specStarted, specDone
● HTML reporter included
○ Disabled in the github repository
○ Replaced with SLDS version
● Console reporter available
○ But deprecated!
Reporter
Testing Lightning
Components
Locker Service
● Delete from Jasmine JavaScript:
● Can't spy framework methods
How to Trigger Tests?
Test
Application
Reporter
Component
Jasmine
Testable
Component
1 create
2 Initialise
5 queue
tests
6 queue tests
4 Initialised
7 tests
queued
8 execute
9 notify
10 Complete
3 register
Lightning component
Jasmine
Lightning event
Attribute change event
JavaScript function
Key
Triggering Tests
1. The test application creates the Jasmine instance
The Jasmine instance is visible to all components in the same namespace.
This includes all testable components.
2. The application fires an event asking the reporters to initialize
This is an application event that can be received by any component with an
appropriate handler
3. Each reporter registers an object to receive notifications with Jasmine
The object can take action on each event, but my samples simply capture the
information as the tests progress and then take action once all are complete.
4. Each reporter fires an event to indicate it has completed initialization
5. Once all reporters have initialised, the application fires a queue tests event
The application knows how many responses to expect.
Triggering Tests
6. Each testable component receives the event and queues their tests with Jasmine
The tests are not run at this point!
7. Each testable component fires an event to indicate it has queued its tests
The test application knows how many responses to expect.
8. The test application invokes the Jasmine execute method
This executes the queued tests
9. As tests execute, they notify the reporter(s) of the outcomes
The sample reporters record this information but take no action
10. The application notifies the reporter(s) that all tests are complete
The sample reporters take appropriate action once all tests are complete.
Posting to chatter, for example.
Key Takeaways
● Front end code deserves unit tests
● Decompose functions
● Isolate framework calls
● It's probably not the locker service!
Useful Links
Jasmine source
https://github.com/jasmine/jasmine
Jasmine introduction
http://jasmine.github.io/2.4/introduction.html
Jasmine JavaScript Testing (Book)
https://www.packtpub.com/web-development/jasmine-javascript-testing-second-edition
Jasmine Reporters
https://github.com/larrymyers/jasmine-reporters
Thank Y u

More Related Content

What's hot (20)

PPTX
Angular Unit Testing
Alessandro Giorgetti
 
PDF
[FullStack NYC 2019] Effective Unit Tests for JavaScript
Hazem Saleh
 
KEY
Jellyfish, JSCONF 2011
Adam Christian
 
PPTX
Angular Unit Testing
Avi Engelshtein
 
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
PDF
Unit-testing and E2E testing in JS
Michael Haberman
 
PDF
Javascript tdd byandreapaciolla
Andrea Paciolla
 
PPTX
Protractor for angularJS
Krishna Kumar
 
PPTX
JavaScript Metaprogramming with ES 2015 Proxy
Alexandr Skachkov
 
PDF
AngularJS and Protractor
Filipe Falcão
 
PDF
Continuous testing and deployment in Perl (London.pm Technical Meeting Octobe...
Alex Balhatchet
 
PPTX
Test-Driven JavaScript Development (JavaZone 2010)
Christian Johansen
 
PDF
Ava unit test
Mohamed Ahmed
 
PPTX
Automating Perl deployments with Hudson
nachbaur
 
PPTX
Running a Test Harness in Parallel using Jenkins
Joshua Shapiro
 
PDF
Angular Unit Testing from the Trenches
Justin James
 
PDF
Building software by feature with immutable infrastructures on AWS
Nicolas Mas
 
PDF
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
 
PPTX
Java do-while Loop
Rhythm Suiwal
 
PDF
Introduction to Venus.js
Seth McLaughlin
 
Angular Unit Testing
Alessandro Giorgetti
 
[FullStack NYC 2019] Effective Unit Tests for JavaScript
Hazem Saleh
 
Jellyfish, JSCONF 2011
Adam Christian
 
Angular Unit Testing
Avi Engelshtein
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
Unit-testing and E2E testing in JS
Michael Haberman
 
Javascript tdd byandreapaciolla
Andrea Paciolla
 
Protractor for angularJS
Krishna Kumar
 
JavaScript Metaprogramming with ES 2015 Proxy
Alexandr Skachkov
 
AngularJS and Protractor
Filipe Falcão
 
Continuous testing and deployment in Perl (London.pm Technical Meeting Octobe...
Alex Balhatchet
 
Test-Driven JavaScript Development (JavaZone 2010)
Christian Johansen
 
Ava unit test
Mohamed Ahmed
 
Automating Perl deployments with Hudson
nachbaur
 
Running a Test Harness in Parallel using Jenkins
Joshua Shapiro
 
Angular Unit Testing from the Trenches
Justin James
 
Building software by feature with immutable infrastructures on AWS
Nicolas Mas
 
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
 
Java do-while Loop
Rhythm Suiwal
 
Introduction to Venus.js
Seth McLaughlin
 

Viewers also liked (14)

PPTX
JavaScript Unit Testing
Keir Bowden
 
PDF
Developer Experience to Testing
Mozaic Works
 
PDF
The Developer Experience
Atlassian
 
PDF
Testing javascript in the frontend
Frederic CABASSUT
 
PDF
Introducing Sencha Touch 2
Sencha
 
PDF
Painless JavaScript Testing with Jest
Michał Pierzchała
 
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
PDF
Javascript testing: tools of the trade
Juanma Orta
 
PDF
Javascript Unit Testing Tools
PixelCrayons
 
PDF
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
PPTX
SenchaCon 2016: The Changing Landscape of JavaScript Testing - Joel Watson an...
Sencha
 
PPTX
jasmine
Asanka Indrajith
 
PPTX
Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"
Fwdays
 
JavaScript Unit Testing
Keir Bowden
 
Developer Experience to Testing
Mozaic Works
 
The Developer Experience
Atlassian
 
Testing javascript in the frontend
Frederic CABASSUT
 
Introducing Sencha Touch 2
Sencha
 
Painless JavaScript Testing with Jest
Michał Pierzchała
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
Javascript testing: tools of the trade
Juanma Orta
 
Javascript Unit Testing Tools
PixelCrayons
 
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
SenchaCon 2016: The Changing Landscape of JavaScript Testing - Joel Watson an...
Sencha
 
Михаил Боднарчук "Acceptance Testing in NodeJS: Tools & Approaches"
Fwdays
 
Ad

Similar to Unit Testing Lightning Components with Jasmine (20)

PPTX
Introduction to Protractor - Habilelabs
HabileLabs
 
PDF
Unit testing legacy code
Lars Thorup
 
PDF
Si fa presto a dire serverless
Alessio Coser
 
PDF
Testing Angular
Lilia Sfaxi
 
PPTX
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
PDF
Unit Testing in Angular
Knoldus Inc.
 
PPT
Qtp training session V
Aisha Mazhar
 
PDF
Test Automation
Rodrigo Paiva
 
PPTX
Test automation engineer
Sadaaki Emura
 
PDF
Aikau testing tech talk live 83 20150204
David Webster
 
PDF
Unit tests in node.js
Rotem Tamir
 
PPTX
Test automation expert days
Oren Rubin
 
PDF
How to kill test flake in appium
Gaurav Singh
 
PPTX
Automation Zaman Now
Ibnu Fajar Yunardi
 
PDF
Performance testing with jmeter
Knoldus Inc.
 
PPTX
Unit Testing in Android
Md Shamsul Arafin Mahtab
 
PDF
Machine learning pipeline
Vadym Kuzmenko
 
PPTX
Cloudstack Continuous Delivery
buildacloud
 
PDF
Automation for developers
Dharshana Kasun Warusavitharana
 
PPTX
Joomla! JavaScript Testing
Ruchiranga Wickramasinghe
 
Introduction to Protractor - Habilelabs
HabileLabs
 
Unit testing legacy code
Lars Thorup
 
Si fa presto a dire serverless
Alessio Coser
 
Testing Angular
Lilia Sfaxi
 
Dive into Angular, part 5: Experience
Oleksii Prohonnyi
 
Unit Testing in Angular
Knoldus Inc.
 
Qtp training session V
Aisha Mazhar
 
Test Automation
Rodrigo Paiva
 
Test automation engineer
Sadaaki Emura
 
Aikau testing tech talk live 83 20150204
David Webster
 
Unit tests in node.js
Rotem Tamir
 
Test automation expert days
Oren Rubin
 
How to kill test flake in appium
Gaurav Singh
 
Automation Zaman Now
Ibnu Fajar Yunardi
 
Performance testing with jmeter
Knoldus Inc.
 
Unit Testing in Android
Md Shamsul Arafin Mahtab
 
Machine learning pipeline
Vadym Kuzmenko
 
Cloudstack Continuous Delivery
buildacloud
 
Automation for developers
Dharshana Kasun Warusavitharana
 
Joomla! JavaScript Testing
Ruchiranga Wickramasinghe
 
Ad

More from Keir Bowden (20)

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

Recently uploaded (20)

PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
July Patch Tuesday
Ivanti
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 

Unit Testing Lightning Components with Jasmine

  • 1. Keir Bowden CTO, BrightGen @bob_buzzard Unit Testing Lightning Components With Jasmine
  • 3. Introduction ● Give confidence ● Reduce cost of bugs ● Produce Testable Code ● Promote refactoring Why Write Unit Tests
  • 4. JavaScript Testing on Salesforce ● No test context for client ○ Changes aren't rolled back ● Side effects ○ DOM manipulation ○ Server side actions ● You don't have to! Challenges
  • 5. Techniques ● Use unobtrusive JavaScript ● Avoid anonymous functions ○ Can't test in isolation ○ Have to execute action Write Testable Code
  • 6. Techniques ● Decompose functions ○ Function to decide on action ○ Function to execute the action Write Testable Code
  • 9. Jasmine ● Pure JavaScript testing framework ● Executes in-browser ○ Execute tests from anywhere ○ Lightning Component JavaScript only executable from browser ● Open source : http://bobbuzz.me.uk/2bVjTEr ● Minor changes for locker service Overview
  • 10. Jasmine ● Behaviour-driven development testing framework ○ Given the job page is open When the user searches for jobs Then the search method should be invoked ● Also known as Setup (Given) - Exercise (When) - Verify (Then) Overview
  • 11. Jasmine ● Suite ○ Collection of tests ○ describe function ● Spec ○ Single test ○ it function ● Expectation ○ Verifies behaviour (equivalent of assert in Apex) ○ expect function ○ Chained with matcher function Features
  • 12. Jasmine ● Setup/Teardown ○ beforeAll - called once, before first spec ○ afterAll - called once, after last spec ○ beforeEach - called before each spec ○ afterEach - called after each spec ● Spies ○ Stubs function ○ Tracks calls ○ Tracks arguments Features
  • 13. Jasmine Spies Reset when spec/suite completes doSearch function calls getJobs getJobs executes server side action spy on the getJobs function Jasmine spy code replaces getJobs implementation - no server side action interrogate spy to confirm behaviour
  • 14. Jasmine ● Object with functions to handle Jasmine events ○ e.g. JasmineStarted, specStarted, specDone ● HTML reporter included ○ Disabled in the github repository ○ Replaced with SLDS version ● Console reporter available ○ But deprecated! Reporter
  • 16. Locker Service ● Delete from Jasmine JavaScript: ● Can't spy framework methods
  • 17. How to Trigger Tests? Test Application Reporter Component Jasmine Testable Component 1 create 2 Initialise 5 queue tests 6 queue tests 4 Initialised 7 tests queued 8 execute 9 notify 10 Complete 3 register Lightning component Jasmine Lightning event Attribute change event JavaScript function Key
  • 18. Triggering Tests 1. The test application creates the Jasmine instance The Jasmine instance is visible to all components in the same namespace. This includes all testable components. 2. The application fires an event asking the reporters to initialize This is an application event that can be received by any component with an appropriate handler 3. Each reporter registers an object to receive notifications with Jasmine The object can take action on each event, but my samples simply capture the information as the tests progress and then take action once all are complete. 4. Each reporter fires an event to indicate it has completed initialization 5. Once all reporters have initialised, the application fires a queue tests event The application knows how many responses to expect.
  • 19. Triggering Tests 6. Each testable component receives the event and queues their tests with Jasmine The tests are not run at this point! 7. Each testable component fires an event to indicate it has queued its tests The test application knows how many responses to expect. 8. The test application invokes the Jasmine execute method This executes the queued tests 9. As tests execute, they notify the reporter(s) of the outcomes The sample reporters record this information but take no action 10. The application notifies the reporter(s) that all tests are complete The sample reporters take appropriate action once all tests are complete. Posting to chatter, for example.
  • 20. Key Takeaways ● Front end code deserves unit tests ● Decompose functions ● Isolate framework calls ● It's probably not the locker service!
  • 21. Useful Links Jasmine source https://github.com/jasmine/jasmine Jasmine introduction http://jasmine.github.io/2.4/introduction.html Jasmine JavaScript Testing (Book) https://www.packtpub.com/web-development/jasmine-javascript-testing-second-edition Jasmine Reporters https://github.com/larrymyers/jasmine-reporters