SlideShare a Scribd company logo
Getting started with
Testing in Javascript
@Spyr1014
Motivation!
• Why should I test?
• How should I test?
• Testing as motivation or HATE DRIVEN
DEVELOPMENT
Why?
But I’ve just
started…
Test driven programming beginner quick start
Get the number 6 from
pressing the button 6
‘Title Case’ FCC Problem
• Return the provided string with the first letter of each
word capitalised.
• Make sure the rest of the word is in lower case.
https://www.freecodecamp.com/challenges/title-case-a-sentence
Starting code
function titleCase(str) {
return str;
}
titleCase("I'm a little tea pot");
// Result we want: "I'm A Little Tea Pot"
FCC test cases
We can set this up
ourselves!
Testing Helper
Functions!
Test driven programming beginner quick start
Test driven programming beginner quick start
Test driven programming beginner quick start
What do I need to
know to test?
Mocha
• Testing framework
• Does lots of work for you
Using Mocha
• ‘describe(string, function)’
• Group tests and creates heading
• `it(string, function)`
• Your ‘individual test’
describe("Heading 1", function(){
describe("Heading 2", function(){
it("A test", function(){
});
});
});
Assertion Library?
• Gives you an easy way to check if things are behaving as
expected.
• Basically it’s the same as == or ===
I imagine it as ================
Chai - Assertion Library
// From http://chaijs.com/
var expect = chai.expect;
expect(name).to.be.a('string');
expect(name).to.equal('Andrew');
expect(foo).to.have.lengthOf(3);
expect(tea).to.have.property('flavours')
.with.lengthOf(3);
expect(titleCase("I'm a little tea pot")).to.be.a("string");
expect(titleCase("I'm a little tea pot"))
.to.equal("I'm A Little Tea Pot");
expect(titleCase("sHoRt AnD sToUt"))
.to.equal("Short And Stout");
expect(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"))
.to.equal("Here Is My Handle Here Is My Spout");
Add `describe`
and `it`
Test driven programming beginner quick start
The Mocha
webpage
Head
• Import any JS dependencies
• Mocha style sheet (mocha.css)
• Import Chai
Body
• Div with id = “mocha”
• Setup Mocha
• Your code, and testing code.
• Run Mocha
<!DOCTYPE html>
<html>
<head>
<title>Mocha</title>
<script src="vendor/chai.js"></script>
<link rel="stylesheet" href="mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="mocha.js"></script>
<script>mocha.setup('bdd');</script>
<!--Put your code here-->
<script src="problem.js"></script>
<script src="problem.test.js"></script>
<!--End of your code.-->
<script>
mocha.run();
</script>
</body>
</html>
Files set up for
you!
https://github.com/SpyR1014/
Simple-Mocha-Testing-
Boilerplate
http://blog.sewmucheasier.com/wp-content/uploads/2013/12/surprised-puppy.jpg
RED - GREEN - REFACTOR
• while(problem !== ‘solved’) {
• 1.Write a failing test
• 2. Fix the failing test
• 3. Make the code nice
• }
–Kent Beck (Author of Test Driven Development)
“I'm not a great programmer; I'm just a good
programmer with great habits.”

More Related Content

KEY
Zen: Building Maintainable Catalyst Applications
PDF
Databases and MySQL
PDF
PHP and Databases
KEY
10 Catalyst Tips
PDF
Beginning Jquery In Drupal Theming
KEY
PHPerのためのPerl入門@ Kansai.pm#12
PDF
The Top Skills That Can Get You Hired in 2017
PDF
Testing most things in JavaScript - LeedsJS 31/05/2017
Zen: Building Maintainable Catalyst Applications
Databases and MySQL
PHP and Databases
10 Catalyst Tips
Beginning Jquery In Drupal Theming
PHPerのためのPerl入門@ Kansai.pm#12
The Top Skills That Can Get You Hired in 2017
Testing most things in JavaScript - LeedsJS 31/05/2017

Similar to Test driven programming beginner quick start (20)

PDF
Test driven node.js
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
PDF
JavaScript development methodology
PDF
An Introduction to the World of Testing for Front-End Developers
PDF
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
PDF
TDD for Javascript developers
PDF
mocha sinon chai Dc jquery 4-24
PPTX
Zero to Testing in JavaScript
PPT
JavaScript Testing: Mocha + Chai
PDF
Developers Testing - Girl Code at bloomon
PPTX
MidwestJS Zero to Testing
PDF
Intro to JavaScript Testing
PDF
Testing with Express, Mocha & Chai
PDF
Unit Testing JavaScript Applications
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
PDF
Unit testing with mocha
PDF
Surviving javascript.pptx
PPTX
How do I write Testable Javascript so I can Test my CF API on Server and Client
PDF
ES3-2020-06 Test Driven Development (TDD)
PPTX
Introduction to bdd
Test driven node.js
Intro To JavaScript Unit Testing - Ran Mizrahi
JavaScript development methodology
An Introduction to the World of Testing for Front-End Developers
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
TDD for Javascript developers
mocha sinon chai Dc jquery 4-24
Zero to Testing in JavaScript
JavaScript Testing: Mocha + Chai
Developers Testing - Girl Code at bloomon
MidwestJS Zero to Testing
Intro to JavaScript Testing
Testing with Express, Mocha & Chai
Unit Testing JavaScript Applications
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
Unit testing with mocha
Surviving javascript.pptx
How do I write Testable Javascript so I can Test my CF API on Server and Client
ES3-2020-06 Test Driven Development (TDD)
Introduction to bdd
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation theory and applications.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
DOCX
The AUB Centre for AI in Media Proposal.docx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation theory and applications.pdf
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
The AUB Centre for AI in Media Proposal.docx
Ad

Test driven programming beginner quick start