SlideShare a Scribd company logo
JavaScript Unit-Testing
Mihail Irintchev
SiteGround.com
How do you test your JS?
1. Write your JavaScript code
2. See if it works in your favourite browser
3. Change something + [F5]
4. If it doesn't work repeat #3 until you make it work
or you go crazy...
5. In case you made it work, discover few
days/weeks later that it doesn't work in another
browser
I think I'm going crazy...
So what's the first step to sanity?
WRITE TESTABLE CODE
What's wrong with this code?
js_sample_001.js
(inline functions and more inside, ajax
directly hooked to element, etc.)
Anonymous functions, within functions,
within functions...
I'll put functions in your functions...
All your DOM elements are belong to JS!
Server URL coupling
js_sample_001.js
(with highlighted hardcoded url)
Refactoring...
js_sample_002.js
Refactoring...
js_sample_002.js
Now that's better...
js_sample_003.js
(init func and hooked named functions to
page)
Now that's better...
Now that's better...
Now what about testing?
Popular JS Unit-testing frameworks:

QUnit

Jasmine

UnitJS

JsUnit (no longer actively maintained)

Some other – see:
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
What's about QUnit?

Used by the jQuery project to test jQuery, jQuery
UI, jQuery Mobile

Can be used to test any generic JavaScript
code, including itself

Very easy to install – just include JS & CSS
file in your HTML
Minimal setup: The tests.htm file
The tests.js file
...remember this earlier?
… and the results:
More tests: quite a variety
… and the results:
Test modules
... and the results page:
Setup / TearDown methods:
What about asynch methods &
AJAX?
Use an AJAX mocking library, such as jquery-mockjax:
No bullshit
Let me show you...
Other mock JS frameworks:
-ajax
But what about test automation?
Why phing?
1. Because I like it and I am used to it, and...
2. PHING ALL THE THINGS!
But phing runs in the console...
JS needs browser to run in...
How do we run the browser?
Wouldn't it be slow?
Need speed? Ditch the browser!
But what about cross-browser
testing?
Credits
”Browser Eyeballing != JavaScript Testing”, Jordan Kasper
bit.ly/js-testing
”Unit Testing JavaScript With QUnit And Phing”, Robert Price
http://www.robertprice.co.uk/robblog/2012/12/unit-testing-javascript-with-qunit-and-phin
”Qunit Phing Task”, Martin Jonsson
http://github.com/martinj/phing-task-qunit
Questions?
@irintchev
m.irintchev@siteground.com

More Related Content

What's hot (20)

PPTX
Browser Automated Testing Frameworks - Nightwatch.js
Luís Bastião Silva
 
PDF
webpack 101 slides
mattysmith
 
PPTX
An Intro into webpack
Squash Apps Pvt Ltd
 
PPTX
UI Testing Automation - Alex Kalinovsky - CreamTec LLC
Jim Lane
 
PPT
AngularJS for Legacy Apps
Peter Drinnan
 
PDF
Webdriver.io
LinkMe Srl
 
PDF
Module, AMD, RequireJS
偉格 高
 
PDF
Webpack DevTalk
Alessandro Bellini
 
PDF
Service Worker 201 (en)
Chang W. Doh
 
PPTX
High Performance Snippets
Steve Souders
 
PDF
Building a js widget
Tudor Barbu
 
PDF
Night Watch with QA
Carsten Sandtner
 
PDF
JavaScript + Jenkins = Winning!
Eric Wendelin
 
PPTX
Your Script Just Killed My Site
Steve Souders
 
PPTX
Automation testing with Drupal 8
nagpalprachi
 
PDF
Service Worker 101 (en)
Chang W. Doh
 
PDF
Webpack: from 0 to 2
Alessandro Bellini
 
PDF
PHP MVC Tutorial
Yang Bruce
 
PDF
20160905 - BrisJS - nightwatch testing
Vladimir Roudakov
 
PDF
jQuery Proven Performance Tips & Tricks
Addy Osmani
 
Browser Automated Testing Frameworks - Nightwatch.js
Luís Bastião Silva
 
webpack 101 slides
mattysmith
 
An Intro into webpack
Squash Apps Pvt Ltd
 
UI Testing Automation - Alex Kalinovsky - CreamTec LLC
Jim Lane
 
AngularJS for Legacy Apps
Peter Drinnan
 
Webdriver.io
LinkMe Srl
 
Module, AMD, RequireJS
偉格 高
 
Webpack DevTalk
Alessandro Bellini
 
Service Worker 201 (en)
Chang W. Doh
 
High Performance Snippets
Steve Souders
 
Building a js widget
Tudor Barbu
 
Night Watch with QA
Carsten Sandtner
 
JavaScript + Jenkins = Winning!
Eric Wendelin
 
Your Script Just Killed My Site
Steve Souders
 
Automation testing with Drupal 8
nagpalprachi
 
Service Worker 101 (en)
Chang W. Doh
 
Webpack: from 0 to 2
Alessandro Bellini
 
PHP MVC Tutorial
Yang Bruce
 
20160905 - BrisJS - nightwatch testing
Vladimir Roudakov
 
jQuery Proven Performance Tips & Tricks
Addy Osmani
 

Similar to Js unit testing (20)

PDF
Qunit tutorial
HarikaReddy115
 
PPTX
Java script unit testing
Mats Bryntse
 
PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
PPT
JavaScript Unit Testing
Christian Johansen
 
PPT
JsUnit
Alex Chaffee
 
PDF
Automated Web Testing using JavaScript
Simon Guest
 
PDF
Javascript Unit Testing Tools
PixelCrayons
 
KEY
Enterprise Strength Mobile JavaScript
Troy Miles
 
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PPTX
Unit Testing TypeScript
Daniel Jimenez Garcia
 
ODP
Dot Net Notts Js Unit Testing at Microlise
Jonathan Gregory
 
PPT
Jsunit
david_inapps
 
ODP
Js unit testingpresentation
Jonathan Gregory
 
PPTX
Automated Testing using JavaScript
Simon Guest
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PPTX
Testing Ext JS and Sencha Touch
Mats Bryntse
 
PPTX
JavaScript Unit Testing
Keir Bowden
 
PDF
Front end unit testing using jasmine
Gil Fink
 
KEY
JavaScript Testing VIA Selenium
Adam Christian
 
Qunit tutorial
HarikaReddy115
 
Java script unit testing
Mats Bryntse
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
JavaScript Unit Testing
Christian Johansen
 
JsUnit
Alex Chaffee
 
Automated Web Testing using JavaScript
Simon Guest
 
Javascript Unit Testing Tools
PixelCrayons
 
Enterprise Strength Mobile JavaScript
Troy Miles
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Unit Testing TypeScript
Daniel Jimenez Garcia
 
Dot Net Notts Js Unit Testing at Microlise
Jonathan Gregory
 
Jsunit
david_inapps
 
Js unit testingpresentation
Jonathan Gregory
 
Automated Testing using JavaScript
Simon Guest
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Testing Ext JS and Sencha Touch
Mats Bryntse
 
JavaScript Unit Testing
Keir Bowden
 
Front end unit testing using jasmine
Gil Fink
 
JavaScript Testing VIA Selenium
Adam Christian
 
Ad

Recently uploaded (20)

PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Ad

Js unit testing