SlideShare a Scribd company logo
TDD for
JavaScript
developers
What is TDD?
●
●
●
●
●

TODO list (navigation map)
Test first
Assert first
Fail first
Continuous integration
Red > Green > Refactor
What about TDD?
●
●
●
●
●
●
●

Unit tests coverage
Increases assurance of correctness
More complete explorations of requirements
Improved code
Code as documentation
Safe refactoring
No extra code
TDD for Javascript developers
Pair programming
Baby steps
Emergency design
TDD for Javascript developers
TDD for Javascript developers
Broken Window Theory
Build Breakers
How to start with TDD?
●
●
●
●
●
●
●

Trainings
Management support
Skills sharing
Perseverance
Mentors
Metrics
Coding dojo
How to start with TDD?
● Start new project with TDD/BDD practice
● Start new task with unit tests
● Start to fix bugs with unit tests
Where is my test?
● Developers
● CI
● Deployment
Behavior-driven Development
● Given ...
● When ...
● Then ...

● describe ...
● it ...
JavaScript programming
Tools for JavaScript
Runners:

●
●
●
●

jsTestDriver
PhantomJS
Node.js
Browser
Tasks for JavaScript
● gruntjs
● grunt plugins
Tasks for JavaScript
● gulp.js
Bootstrap project
● Yeoman
● Brunch.io
Package managers
● npmjs
● bower.io
● volojs
Tools for JavaScript
IDE:

●
●
●
●
●

WebStorm
Netbeans
Eclipse
Sublime Text
c9.io
Tools for JavaScript
Continuous integration:

●
●
●
●

Jenkins/Hudson
TeamCity
Cruise Control
Travis-ci
Tools for JavaScript
Code analyze:

●
●
●
●

jslint
jshint
JSCoverage
jscpd
Tests
●
●
●
●
●
●
●

mocha
chai
buster.js
nodeunit
qunit
jasmine
sinon.js
Tests
cucumber.js
Mocha - getting started
<div id="mocha"></div>
... include js ...
<script src="mocha.js"></script>
<script>mocha.setup('bdd')</script>
... include tests ...
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run();
</script>
Mocha - suites and cases
describe("Test Suite", function() {
it("test case/spec", function() {
//BDD style
});
});
suite('Test Suite', function(){
test('test case', function(){
//TDD style
});
});
Mocha - setUp and tearDown
beforeEach(function() {
//code here call every time at start of test
});
afterEach(function() {
//code here call every time at after test
});
Mocha - docs

http://visionmedia.github.io/mocha/
Chai - assert
var assert = require('chai').assert
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
assert.typeOf(foo, 'string', 'foo is a string');
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.notEqual(foo, 'barz', 'foo not equal `barz`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types
of tea');
Chai - expect
var expect = require('chai').expect
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.not.equal('barz');
expect(foo).to.have.length(3);
expect(beverages).to.have.property('tea').with.length(3);
Chai - should
var should = require('chai').should() //actually call the the
function
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.not.equal('barz');
foo.should.have.length(3);
beverages.should.have.property('tea').with.length(3);
Chai - docs

http://chaijs.com/api/
Sinon.js - spy, stub
"test should call subscribers on publish": function () {
var callback = sinon.spy();
PubSub.subscribe("message", callback);
PubSub.publishSync("message");
assertTrue(callback.called);
}
Sinon.js - env
var env, $ajax;
beforeEach(function() {
env = sinon.sandbox.create();
//stub all requests to server
$ajax = env.stub($, 'ajax');
});
afterEach(function() {
env.restore();
});
Sinon.js - env

http://sinonjs.
org/docs/
Links & books
http://visionmedia.github.io/mocha/
http://chaijs.com/
http://sinonjs.org/
http://www.testdrivenjs.com/
http://tddjs.com/
Questions?
Lets write code!

More Related Content

PDF
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
PPTX
CodeIgniter Ant Scripting
PPTX
Code igniter unittest-part1
PPT
Google C++ Testing Framework in Visual Studio 2008
PDF
TDD in Python With Pytest
PDF
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
PDF
PDF
Python Testing Fundamentals
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
CodeIgniter Ant Scripting
Code igniter unittest-part1
Google C++ Testing Framework in Visual Studio 2008
TDD in Python With Pytest
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
Python Testing Fundamentals

Similar to TDD for Javascript developers (20)

PPT
JavaScript Testing: Mocha + Chai
PDF
Unit testing with mocha
PPTX
How do I write Testable Javascript so I can Test my CF API on Server and Client
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
PDF
How do I Write Testable Javascript so I can Test my CF API on Server and Client
PDF
Developers Testing - Girl Code at bloomon
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
PDF
Test driven node.js
PPTX
Full Stack Unit Testing
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 ...
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PDF
How to write Testable Javascript
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
PDF
How do I write Testable Javascript?
PDF
How do I write Testable Javascript
PDF
How do I write testable javascript?
PDF
Testing with Express, Mocha & Chai
PPT
Test innode
JavaScript Testing: Mocha + Chai
Unit testing with mocha
How do I write Testable Javascript so I can Test my CF API on Server and Client
Intro To JavaScript Unit Testing - Ran Mizrahi
How do I Write Testable Javascript so I can Test my CF API on Server and Client
Developers Testing - Girl Code at bloomon
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API -
Test driven node.js
Full Stack Unit Testing
An Introduction to the World of Testing for Front-End Developers
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
How to write Testable Javascript
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript?
How do I write Testable Javascript
How do I write testable javascript?
Testing with Express, Mocha & Chai
Test innode
Ad

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The AUB Centre for AI in Media Proposal.docx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Review of recent advances in non-invasive hemoglobin estimation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Ad

TDD for Javascript developers