SlideShare a Scribd company logo
TESTING JAVASCRIPT APPLICATIONS
by Alexander Gerasimov and Dmitrey Gerasimov
¿POR QUÉ?
¡PORQUE!
refactoring
organization, modularization, extensibility
documentation
defect prevention
collaboration
"Any feature without a test doesn’t exist"
Steve Loughran HP Laboratories
TDD & BDD
TESTS FIRST
RED/GREEN/REFACTOR
Red: Write a failing test
RED/GREEN/REFACTOR
Green: Make it pass
RED/GREEN/REFACTOR
Refactor: Eliminate redundancy
STRUCTURE
Setup: Put the Unit Under Test (UUT) or the overall test system in the state needed
to run the test.
Execution: Trigger/drive the UUT to perform the target behavior and capture all
output, such as return values and output parameters.
Validation: Ensure the results of the test are correct.
Cleanup: Restore the UUT or the overall test system to the pre-test state.
http://en.wikipedia.org/wiki/Test-driven_development#Test_structure
TDD ASSERTIONS
var assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'favors');
assert.lengthOf(tea.flavors, 3);
BDD
Behavior-driven development
GIVEN-WHEN-THEN
Story: Returns go to stock
In order to keep track of stock
As a store owner
I want to add items back to stock when they're returned
Scenario 1: Refunded items should be returned to stock
Given a customer previously bought a black sweater from me
And I currently have three black sweaters left in stock
When he returns the sweater for a refund
Then I should have four black sweaters in stock
CUCUMBER
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two number
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
CUCUMBER
Given /I have entered (.*) into the calculator do
calculator = new Calculator();
calculator.push(n);
end
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.have.length(3);
expect(beverages).to.have.property("tea").with.length(3);
SHOULD
var should = require("chai").should()
, foo = "bar"
, beverages = { tea: [ "chai", "matcha", "oolong" ] };
foo.should.be.a("string");
foo.should.equal("bar");
foo.should.have.length(3);
beverages.should.have.property("tea").with.length(3);
FUNCTIONAL TESTING
UX/BEHAVIOR VERIFICATION
Unit tests just prove that your code doesn't work
AUTOMATION & CONTROL
↓
METRICS & PROFILING
Execution time
Loading, rendering, painting
CPU & Memory
Google Chrome Metrics
HELPS QA TESTERS
Why not let QA guys concentrate on quality rather than
routine?
TOOLS & EXAMPLES
generators
frameworks
[assertion] libraries
plugins
stat tools
complex solutions + Grunt
TECHNIQUES
DUMMIES
STUBS
MOCKS
SPIES
FIXTURES
JASMINE
What is it?..
JASMINE
Suites & specs
describe("A suite is just a function", function() {
var a;
it("and so is a spec", function() {
a = true;
expect(a).toBe(true);
});
});
JASMINE
Matchers
expect(x).toEqual(y);
expect(x).toBe(y);
expect(x).toMatch(pattern);
expect(x).toBeDefined();
expect(x).toBeUndefined();
expect(x).toBeNull();
expect(x).toBeTruthy();
expect(x).toBeFalsy();
expect(x).toContain(y);
expect(x).toBeLessThan(y);
expect(x).toBeGreaterThan(y);
expect(function(){fn();}).toThrow(e);
JASMINE
Spies
SPIES
Tracks
Functions calls
Arguments
Number of calls
SPIES
Access
All calls of function
Every argument of every call
SPIES
Can
Track and delegate
Substitute returning values
Call faked functions
Create mock objects
JASMINE
Any
describe("jasmine.any", function() {
it("matches any value", function() {
expect({}).toEqual(jasmine.any(Object));
expect(12).toEqual(jasmine.any(Number));
});
});
JASMINE
Clock
beforeEach(function() {
timerCallback = jasmine.createSpy("timerCallback"); //create spy
jasmine.Clock.useMock(); //use wrepper of system timer
});
it("causes a timeout to be called synchronously", function() {
setTimeout(function() {
timerCallback();
}, 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.Clock.tick(101); //make time to go
expect(timerCallback).toHaveBeenCalled();
});
JASMINE
Async
It exists, but...
JASMINE
Reporter
describe("Jasmine", function() {
it("makes testing JavaScript awesome!", function() {
expect (yourCode).toBeLotsBetter();
});
});
MOCHA
['mɔkə]
MOCHA
Supports TDD assertions and BDD should/expect
Reporting & CI integration
JavaScript API
Browser Test Runner
MOCHA
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
})
})
})
MOCHA
describe('User', function(){
describe('#save()', function(){
it('should save without error', function(done){
var user = new User('Luna');
user.save(function(err){
if (err) throw err;
done();
});
})
})
})
MOCHA
Hooks: before(), after(), beforeEach(), afterEach()
beforeEach(function(done){
db.clear(function(err){
if (err) return done(err);
db.save([tobi, loki, jane], done);
});
})
MOCHA
MOCHA
Console reporter
MOCHA
HTML reporter
MOCHA
Nyan reporter
CHAI
CHAI
Assert, expect/should
chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.length(3);
tea.should.have.property('flavors')
.with.length(3);
QUESTION TIME!
How would you test an RNG?
CHAI
Plugins
here
CASPERJS
PhantomJS
API
SlimerJS
SpookyJS
CASPERJS
var casper = require('casper').create();
casper.start('http://domain.tld/page.html', function() {
if (this.exists('h1.page-title')) {
this.echo('the heading exists');
}
});
casper.run();
QUESTION TIME!
What happens if not everyone on the team adopts TDD/BDD?
CODE COVERAGE
INSTRUMENTATION
INSTRUMENTATION
Testing JavaScript Applications
ISTANBUL
Statement, branch, and function coverage
Test running tools
HTML & LCOV reporting
esprima-based
TESTING + CI = ❤
fail builds
statistics & reporting
Github is integration paradise
GITHUB IS INTEGRATION PARADISE
IRL, 100% COVERAGE IS A LIE
legacy & untestable code
permissive tests
not applicable to functional testing
MUTATION TESTING
Who tests tests?
Coverage is paramount! (it isn't)
Mutations: remove lines, alter operators, rename identifiers
The Future of Unit Testing
QUESTION TIME!
What do you do when you're offerred a project without TDD?
YOU RUN LIKE HELL
THE END

More Related Content

What's hot (20)

PPT
Testing Javascript with Jasmine
Tim Tyrrell
 
KEY
Testing JS with Jasmine
Evgeny Gurin
 
PDF
PL/SQL Unit Testing Can Be Fun!
Raimonds Simanovskis
 
PDF
Understanding JavaScript Testing
jeresig
 
PDF
Painless JavaScript Testing with Jest
Michał Pierzchała
 
PPTX
Good karma: UX Patterns and Unit Testing in Angular with Karma
ExoLeaders.com
 
PDF
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
PDF
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
PDF
Testing javascript in the frontend
Frederic CABASSUT
 
PDF
Intro to testing Javascript with jasmine
Timothy Oxley
 
PDF
Test-Driven Development of AngularJS Applications
FITC
 
PPTX
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
PDF
Unit Testing JavaScript Applications
Ynon Perek
 
PPTX
Understanding JavaScript Testing
Kissy Team
 
PPTX
Full Stack Unit Testing
GlobalLogic Ukraine
 
PDF
JavaScript Unit Testing with Jasmine
Raimonds Simanovskis
 
PDF
Introduction to Protractor
Jie-Wei Wu
 
PDF
Testing your javascript code with jasmine
Rubyc Slides
 
PDF
Workshop 5: JavaScript testing
Visual Engineering
 
Testing Javascript with Jasmine
Tim Tyrrell
 
Testing JS with Jasmine
Evgeny Gurin
 
PL/SQL Unit Testing Can Be Fun!
Raimonds Simanovskis
 
Understanding JavaScript Testing
jeresig
 
Painless JavaScript Testing with Jest
Michał Pierzchała
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
ExoLeaders.com
 
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
Testing javascript in the frontend
Frederic CABASSUT
 
Intro to testing Javascript with jasmine
Timothy Oxley
 
Test-Driven Development of AngularJS Applications
FITC
 
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Unit Testing JavaScript Applications
Ynon Perek
 
Understanding JavaScript Testing
Kissy Team
 
Full Stack Unit Testing
GlobalLogic Ukraine
 
JavaScript Unit Testing with Jasmine
Raimonds Simanovskis
 
Introduction to Protractor
Jie-Wei Wu
 
Testing your javascript code with jasmine
Rubyc Slides
 
Workshop 5: JavaScript testing
Visual Engineering
 

Viewers also liked (20)

PDF
Graceful degradation & Progressive enhancement
The Rolling Scopes
 
PDF
D3 workshop
The Rolling Scopes
 
PDF
Learn Frontend Testing
Ryan Roemer
 
PDF
Node.js in Production
Ryan Roemer
 
PDF
CascadiaJS 2014 - Making JavaScript Tests Fast, Easy & Friendly
Ryan Roemer
 
PPTX
Small is Beautiful- Fully Automate your Test Case Design
Georgina Tilby
 
ODP
Garbage collection
Mudit Gupta
 
PPTX
Automation using Javascript
khanhdang1214
 
PDF
5 Coding Hacks to Reduce GC Overhead
Takipi
 
PPTX
An efficient memory system for java Garbage Collection
Rohit Deshpande
 
PDF
Angular js vs. Facebook react
Keyup
 
PPT
JavaScript Testing: Mocha + Chai
James Cryer
 
PDF
Garbage Collection in Java
Keyup
 
PPTX
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
mlilley
 
PDF
How to do open and front end innovation. 5 Principles. Insights & experience...
Martin Malthe Borch
 
PDF
Testing with Express, Mocha & Chai
Joerg Henning
 
PPTX
Cypress/VSAC Presentation at HIMSS13
Saul Kravitz
 
PDF
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Prasid Pathak
 
PDF
Wrangling Large Scale Frontend Web Applications
Ryan Roemer
 
PDF
Automated UI Testing Frameworks
TestObject - Mobile Testing
 
Graceful degradation & Progressive enhancement
The Rolling Scopes
 
D3 workshop
The Rolling Scopes
 
Learn Frontend Testing
Ryan Roemer
 
Node.js in Production
Ryan Roemer
 
CascadiaJS 2014 - Making JavaScript Tests Fast, Easy & Friendly
Ryan Roemer
 
Small is Beautiful- Fully Automate your Test Case Design
Georgina Tilby
 
Garbage collection
Mudit Gupta
 
Automation using Javascript
khanhdang1214
 
5 Coding Hacks to Reduce GC Overhead
Takipi
 
An efficient memory system for java Garbage Collection
Rohit Deshpande
 
Angular js vs. Facebook react
Keyup
 
JavaScript Testing: Mocha + Chai
James Cryer
 
Garbage Collection in Java
Keyup
 
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
mlilley
 
How to do open and front end innovation. 5 Principles. Insights & experience...
Martin Malthe Borch
 
Testing with Express, Mocha & Chai
Joerg Henning
 
Cypress/VSAC Presentation at HIMSS13
Saul Kravitz
 
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Prasid Pathak
 
Wrangling Large Scale Frontend Web Applications
Ryan Roemer
 
Automated UI Testing Frameworks
TestObject - Mobile Testing
 
Ad

Similar to Testing JavaScript Applications (20)

PPTX
Zero to Testing in JavaScript
pamselle
 
PPTX
Jasmine
Alok Guha
 
PPTX
Java Script Isn\'t a Toy Anymore
Alexis Williams
 
PDF
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
PPTX
MidwestJS Zero to Testing
pamselle
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
PDF
Front end unit testing using jasmine
Gil Fink
 
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
PDF
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
PPTX
JS Frameworks Day April,26 of 2014
DA-14
 
PPTX
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
PPTX
Tests in Javascript using Jasmine and Testacular
Paulo Cesar Ortins Brito
 
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
PPTX
jasmine
Asanka Indrajith
 
PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
PPTX
JavaScript Unit Testing
L&T Technology Services Limited
 
PDF
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
Ortus Solutions, Corp
 
Zero to Testing in JavaScript
pamselle
 
Jasmine
Alok Guha
 
Java Script Isn\'t a Toy Anymore
Alexis Williams
 
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
MidwestJS Zero to Testing
pamselle
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
Front end unit testing using jasmine
Gil Fink
 
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
JS Frameworks Day April,26 of 2014
DA-14
 
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
Tests in Javascript using Jasmine and Testacular
Paulo Cesar Ortins Brito
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
JavaScript Unit Testing
L&T Technology Services Limited
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
Ortus Solutions, Corp
 
Ad

Recently uploaded (20)

PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Human Resources Information System (HRIS)
Amity University, Patna
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

Testing JavaScript Applications