SlideShare a Scribd company logo
Testing!
Web Applications
presented by Seth McLaughlin @sethmc
E N G I N E E R I N G
www.shapesecurity.com
testing is a BIG topic.
how to effectively test JavaScript code
in your web applications?
Workflow.
Start Commit Code Staging QA Sign-off In ProdCode Review
DEV
Right away!
PM
Build me
feature X ASAP!
Start Commit Code Staging QA Sign-off In ProdCode Review
DEV
working for
the man…
Start Commit Code Staging QA Sign-off In ProdCode Review
every night
and day!
DEV
Start Commit Code Staging QA Sign-off In ProdCode Review
DEV
everyone’s
a critic
Start Commit Code Staging QA Sign-off In ProdCode Review
DEV
hallelujah!
Start Commit Code Staging QA Sign-off In ProdCode Review
DEV
my work here
is done.
Start Commit Code Staging QA Sign-off In ProdCode Review
DEV
my work here
is done.
QA
we’ll see
about that!
Start Commit Code Staging QA Sign-off In ProdCode Review
QA
ship it!
Start Commit Code Staging QA Sign-off In ProdCode Review
CUSTOMER
feature X is
awesome!
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 0 days
start coding!
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 7 days
start code review
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 9 days
commit code
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 10 days
deploy code to staging
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 12 days
QA finds a bug in staging
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 13 days
fix bug
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 15 days
code review fix
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 17 days
commit fixed code
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 17 days
deploy (again) to staging
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 19 days
QA is happy
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 20 days
shipped to production
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 6 days
Dev finds the bug while coding
Start Commit Code Staging QA Sign-off In ProdCode Review
elapsed time: 15 days
could have saved over 5 days
by catching bug sooner
alternatively…
find bugs as early
as possible.
Static Analysis
Start Commit Code Staging QA Sign-off In ProdCode Review
var config = {!
name: this.name,!
style: 'solo',!
};
blows up in IE7
Static Analysis
Start Commit Code Staging QA Sign-off In ProdCode Review
function getObjs(paths) {
return paths.map(function (p) {
var obj = null;
!
try {
obj = require(p);
} catch (e) {
console.error('Cannot load path', p);
}
}).filter(function (obj) {
return obj !== null;
});
}
Static Analysis
Start Commit Code Staging QA Sign-off In ProdCode Review
ESLint
• Find common errors and enforce coding style
• Run every time you save a file! (automatically)
• Fast!
www.jshint.com www.eslint.org
JS Hint
DEV
eslint ftw!
Unit Tests
Start Commit Code Staging QA Sign-off In ProdCode Review
• Test code at a granular level
• Test as you develop (TDD)
• Great traceability
• Fast! Fun!
DEV
I ♥ unit tests
visionmedia.github.io/mocha
simple, flexible, fun
jasmine.github.io
www.qunitjs.com
mocha
Continuous Integration
Start Commit Code Staging QA Sign-off In ProdCode Review
Pre-Commit
Post-Commit
example: run eslint and mocha tests
example: run selenium tests
hudson-ci.org
Hudson
jenkins-ci.org
Jenkins
travis-ci.org circleci.com
atlassian.com
Bamboo
jetbrains.com
TeamCity
End to End Tests
Start Commit Code Staging QA Sign-off In ProdCode Review
• Scenario driven
• Tests client and server code
• More “realistic” vs. Unit Tests
• Less traceability vs. Unit Tests
• Slower to execute vs. Unit Tests
I ♥ selenium
QAseleniumhq.org
Selenium
saucelabs.com browserstack.com
Techniques.
End to End tests with Selenium
Selenium
Client API
Test Script
Selenium
WebDriver
Web
Browser
JavaScript, Java, C#, Python, …
Converts commands to HTTP requests
Communicates with web browser
...
End to End tests with Selenium
Selenium
Client API
Test Script Selenium Grid
VM #1
VM #2
VM #5
IE 7
VM #4
IE 8
VM #6
IE 9
End to End tests with Selenium
module.exports = {
'Get free VMs': function (browser) {
browser
.url('http://www.modern.ie')
.assert.title('Modern.IE')
.end();
}
};
Nightwatch.js
Unit Testing with Mocha (node.js)
test_file.js
Node.js
test_file.js
code_to_test.js
mocha
test results
Unit Testing with Mocha (node.js)
module.exports = {
name: function (title, first, last) {
return [title, first, last].join();
}
};
formatter.js
Unit Testing with Mocha (node.js)
var fmt = require('../../formatter');
var assert = require('assert');
!
describe('format', function () {
it('should return full name', function () {
var actual = fmt.name('Mr.', 'Bob', 'Rogers');
var expected = 'Mr. Bob Rogers';
!
assert.equal(actual, expected);
});
});
formatter.spec.js
Unit Testing with Venus & Mocha (browser)
test_file.js
Venus
Node.js Browser
test_file.js
code_to_test.js
mocha
test results
Unit Testing with Venus & Mocha (browser)
function formatName(title, first, last) {
return [title, first, last].join();
}
!
formatter.js
Unit Testing with Venus & Mocha (browser)
/**
* @venus-library mocha
* @venus-code formatter.js
*/
describe('format', function () {
var actual = formatName('Mr.', 'Bob', 'Rogers');
var expected = 'Mr. Bob Rogers';
!
it('should return full name', function () {
expect(actual).to.be(expected);
});
});
!
formatter.spec.js
Unit Testing with Venus & Mocha (browser)
<!DOCTYPE html>
<html>
<head>
<title>Test for Formatter</title>
<script type="text/javascript" src=“mocha.js”></script>
<script type="text/javascript" src=“venus_client.js”></script>
<script type="text/javascript" src="formatter.js"></script>
<script type="text/javascript" src=“specs/formatter.spec.js”></script>
<script type="text/javascript">
testLibrary.run();
</script>
</head>
<body>
<div id="results"></div>
</body>
</html>
test_harness.html
Example: Spy
var callback = sinon.spy();
!
PubSub.subscribe('message', callback);
PubSub.publishSync('message');
!
assert(callback.called === true);
!
A function that records information about how it is called.
sinonjs.org
Example: Mock
XMLHttpRequestmyLib jQuery Web Server
Example: Mock
Mock XMLHttpRequestmyLib jQuery
A fake implementation of a code dependency.
Sinon.js XHR Mock
var xhr = sinon.useFakeXMLHttpRequest();
var requests = [];
var callback = sinon.spy();
!
xhr.onCreate = function (xhr) { requests.push(xhr); };
!
myLib.getCommentsFor("/some/article", callback);
assertEquals(1, requests.length);
!
requests[0].respond(200,
{ "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]'
);
!
assert(callback.calledWith([{ id: 12, comment: "Hey there" }]));
Code Samples
End-to-End test with Selenium
Unit Test node.js code
Unit Test browser code
1
2
3
http://sethmcl.com/testing-web-apps/
Learn More.
Introduction to writing testable JavaScript!
LinkedIn Tech Talk
Smashing Magazine
!
Unit Testing with Venus.js !
LinkedIn Tech Talk
Venusjs.org
!
End to End testing with Selenium!
The Selenium Project
Selenium Architecture
Nightwatchjs.org
!
Free Windows VMs and other testing resources!
modern.ie
http://sethmcl.com/testing-web-apps/

More Related Content

What's hot (20)

PDF
Testing nightwatch, by David Torroija
David Torroija
 
PDF
20160905 - BrisJS - nightwatch testing
Vladimir Roudakov
 
PDF
Node.js and Selenium Webdriver, a journey from the Java side
Mek Srunyu Stittri
 
PPTX
Automate testing with behat, selenium, phantom js and nightwatch.js (5)
Faichi Solutions
 
PDF
Front-end Automated Testing
Ruben Teijeiro
 
PDF
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Cogapp
 
PDF
Acceptance & Functional Testing with Codeception - Devspace 2015
Joe Ferguson
 
PDF
Codeception introduction and use in Yii
IlPeach
 
PPTX
Protractor Tutorial Quality in Agile 2015
Andrew Eisenberg
 
PPTX
Nightwatch JS for End to End Tests
Sriram Angajala
 
PDF
Efficient JavaScript Unit Testing, May 2012
Hazem Saleh
 
PPTX
Automated Testing using JavaScript
Simon Guest
 
PDF
PHP Unit Testing in Yii
IlPeach
 
PDF
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Codemotion
 
PDF
High Performance JavaScript 2011
Nicholas Zakas
 
PDF
Testing with Codeception (Webelement #30)
Adam Štipák
 
PDF
Real World Selenium Testing
Mary Jo Sminkey
 
PDF
Introducing Playwright's New Test Runner
Applitools
 
PDF
Codeception presentation
Andrei Burian
 
PPTX
Automated Smoke Tests with Protractor
🌱 Dale Spoonemore
 
Testing nightwatch, by David Torroija
David Torroija
 
20160905 - BrisJS - nightwatch testing
Vladimir Roudakov
 
Node.js and Selenium Webdriver, a journey from the Java side
Mek Srunyu Stittri
 
Automate testing with behat, selenium, phantom js and nightwatch.js (5)
Faichi Solutions
 
Front-end Automated Testing
Ruben Teijeiro
 
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Cogapp
 
Acceptance & Functional Testing with Codeception - Devspace 2015
Joe Ferguson
 
Codeception introduction and use in Yii
IlPeach
 
Protractor Tutorial Quality in Agile 2015
Andrew Eisenberg
 
Nightwatch JS for End to End Tests
Sriram Angajala
 
Efficient JavaScript Unit Testing, May 2012
Hazem Saleh
 
Automated Testing using JavaScript
Simon Guest
 
PHP Unit Testing in Yii
IlPeach
 
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Codemotion
 
High Performance JavaScript 2011
Nicholas Zakas
 
Testing with Codeception (Webelement #30)
Adam Štipák
 
Real World Selenium Testing
Mary Jo Sminkey
 
Introducing Playwright's New Test Runner
Applitools
 
Codeception presentation
Andrei Burian
 
Automated Smoke Tests with Protractor
🌱 Dale Spoonemore
 

Viewers also liked (20)

PPTX
Testing web application
jayashreesaravanan
 
PPTX
Web Application Testing
Richa Goel
 
PDF
Web application security & Testing
Deepu S Nath
 
DOCX
Selenium Testing Project report
Kapil Rajpurohit
 
PPS
A perspective on web testing.ppt
sivaprasanth rentala
 
PDF
Introduction To Web Application Testing
Ynon Perek
 
PDF
Web App Testing - A Practical Approach
Walter Mamed
 
PDF
Unit 09: Web Application Testing
DSBW 2011/2002 - Carles Farré - Barcelona Tech
 
PPTX
Web Application Software Testing
Andrew Kandels
 
PPTX
Software testing live project training
TOPS Technologies
 
PDF
Software testing methods, levels and types
Confiz
 
PPT
Software Testing Fundamentals
Chankey Pathak
 
PPTX
Software testing ppt
Heritage Institute Of Tech,India
 
PDF
Acunetix technical presentation v7 setembro2011
Wlad1m1r
 
PPT
TESTING Checklist
Febin Chacko
 
PPTX
Locking and Race Conditions in Web Applications
Andrew Kandels
 
PPTX
PAVE
Mike Wilhelm
 
PDF
Testing Web Application Security
Ted Husted
 
PDF
Penetration testing web application web application (in) security
Nahidul Kibria
 
PDF
Intro to IronWASP
n|u - The Open Security Community
 
Testing web application
jayashreesaravanan
 
Web Application Testing
Richa Goel
 
Web application security & Testing
Deepu S Nath
 
Selenium Testing Project report
Kapil Rajpurohit
 
A perspective on web testing.ppt
sivaprasanth rentala
 
Introduction To Web Application Testing
Ynon Perek
 
Web App Testing - A Practical Approach
Walter Mamed
 
Unit 09: Web Application Testing
DSBW 2011/2002 - Carles Farré - Barcelona Tech
 
Web Application Software Testing
Andrew Kandels
 
Software testing live project training
TOPS Technologies
 
Software testing methods, levels and types
Confiz
 
Software Testing Fundamentals
Chankey Pathak
 
Software testing ppt
Heritage Institute Of Tech,India
 
Acunetix technical presentation v7 setembro2011
Wlad1m1r
 
TESTING Checklist
Febin Chacko
 
Locking and Race Conditions in Web Applications
Andrew Kandels
 
Testing Web Application Security
Ted Husted
 
Penetration testing web application web application (in) security
Nahidul Kibria
 
Ad

Similar to Testing Web Applications (20)

PDF
JavaScript development methodology
Aleksander Fabijan
 
PDF
Testing most things in JavaScript - LeedsJS 31/05/2017
Colin Oakley
 
PDF
Intro to JavaScript Testing
Ran Mizrahi
 
PDF
mocha sinon chai Dc jquery 4-24
Carson Banov
 
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
PDF
Software Testing
Andrew Wang
 
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
PDF
Unit Testing JavaScript Applications
Ynon Perek
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
PPT
WE-06-Testing.ppt
javed281701
 
PPTX
Testing JavaScript Applications
Muhammad Samu
 
PPT
Testing And Drupal
Peter Arato
 
PPTX
Zero to Testing in JavaScript
pamselle
 
PPTX
Java script unit testing
Mats Bryntse
 
ODP
Js unit testingpresentation
Jonathan Gregory
 
PPTX
MidwestJS Zero to Testing
pamselle
 
ODP
Dot Net Notts Js Unit Testing at Microlise
Jonathan Gregory
 
PPTX
Testing Ext JS and Sencha Touch
Mats Bryntse
 
PDF
How to write Testable Javascript
ColdFusionConference
 
JavaScript development methodology
Aleksander Fabijan
 
Testing most things in JavaScript - LeedsJS 31/05/2017
Colin Oakley
 
Intro to JavaScript Testing
Ran Mizrahi
 
mocha sinon chai Dc jquery 4-24
Carson Banov
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
Software Testing
Andrew Wang
 
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
Unit Testing JavaScript Applications
Ynon Perek
 
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
WE-06-Testing.ppt
javed281701
 
Testing JavaScript Applications
Muhammad Samu
 
Testing And Drupal
Peter Arato
 
Zero to Testing in JavaScript
pamselle
 
Java script unit testing
Mats Bryntse
 
Js unit testingpresentation
Jonathan Gregory
 
MidwestJS Zero to Testing
pamselle
 
Dot Net Notts Js Unit Testing at Microlise
Jonathan Gregory
 
Testing Ext JS and Sencha Touch
Mats Bryntse
 
How to write Testable Javascript
ColdFusionConference
 
Ad

More from Seth McLaughlin (8)

PDF
Building testable chrome extensions
Seth McLaughlin
 
PDF
Chapter 2: What's your type?
Seth McLaughlin
 
PDF
Chapter 1: Communicating with Your Computer
Seth McLaughlin
 
PDF
Are we there yet?
Seth McLaughlin
 
PDF
JavaScript State of Mind
Seth McLaughlin
 
PDF
Get Moving! (with HTML5 canvas)
Seth McLaughlin
 
PDF
Hello, Canvas.
Seth McLaughlin
 
PDF
Introduction to Venus.js
Seth McLaughlin
 
Building testable chrome extensions
Seth McLaughlin
 
Chapter 2: What's your type?
Seth McLaughlin
 
Chapter 1: Communicating with Your Computer
Seth McLaughlin
 
Are we there yet?
Seth McLaughlin
 
JavaScript State of Mind
Seth McLaughlin
 
Get Moving! (with HTML5 canvas)
Seth McLaughlin
 
Hello, Canvas.
Seth McLaughlin
 
Introduction to Venus.js
Seth McLaughlin
 

Recently uploaded (20)

PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 

Testing Web Applications

  • 1. Testing! Web Applications presented by Seth McLaughlin @sethmc
  • 2. E N G I N E E R I N G www.shapesecurity.com
  • 3. testing is a BIG topic.
  • 4. how to effectively test JavaScript code in your web applications?
  • 6. Start Commit Code Staging QA Sign-off In ProdCode Review DEV Right away! PM Build me feature X ASAP!
  • 7. Start Commit Code Staging QA Sign-off In ProdCode Review DEV working for the man…
  • 8. Start Commit Code Staging QA Sign-off In ProdCode Review every night and day! DEV
  • 9. Start Commit Code Staging QA Sign-off In ProdCode Review DEV everyone’s a critic
  • 10. Start Commit Code Staging QA Sign-off In ProdCode Review DEV hallelujah!
  • 11. Start Commit Code Staging QA Sign-off In ProdCode Review DEV my work here is done.
  • 12. Start Commit Code Staging QA Sign-off In ProdCode Review DEV my work here is done. QA we’ll see about that!
  • 13. Start Commit Code Staging QA Sign-off In ProdCode Review QA ship it!
  • 14. Start Commit Code Staging QA Sign-off In ProdCode Review CUSTOMER feature X is awesome!
  • 15. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 0 days start coding!
  • 16. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 7 days start code review
  • 17. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 9 days commit code
  • 18. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 10 days deploy code to staging
  • 19. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 12 days QA finds a bug in staging
  • 20. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 13 days fix bug
  • 21. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 15 days code review fix
  • 22. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 17 days commit fixed code
  • 23. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 17 days deploy (again) to staging
  • 24. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 19 days QA is happy
  • 25. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 20 days shipped to production
  • 26. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 6 days Dev finds the bug while coding
  • 27. Start Commit Code Staging QA Sign-off In ProdCode Review elapsed time: 15 days could have saved over 5 days by catching bug sooner alternatively…
  • 28. find bugs as early as possible.
  • 29. Static Analysis Start Commit Code Staging QA Sign-off In ProdCode Review var config = {! name: this.name,! style: 'solo',! }; blows up in IE7
  • 30. Static Analysis Start Commit Code Staging QA Sign-off In ProdCode Review function getObjs(paths) { return paths.map(function (p) { var obj = null; ! try { obj = require(p); } catch (e) { console.error('Cannot load path', p); } }).filter(function (obj) { return obj !== null; }); }
  • 31. Static Analysis Start Commit Code Staging QA Sign-off In ProdCode Review ESLint • Find common errors and enforce coding style • Run every time you save a file! (automatically) • Fast! www.jshint.com www.eslint.org JS Hint DEV eslint ftw!
  • 32. Unit Tests Start Commit Code Staging QA Sign-off In ProdCode Review • Test code at a granular level • Test as you develop (TDD) • Great traceability • Fast! Fun! DEV I ♥ unit tests visionmedia.github.io/mocha simple, flexible, fun jasmine.github.io www.qunitjs.com mocha
  • 33. Continuous Integration Start Commit Code Staging QA Sign-off In ProdCode Review Pre-Commit Post-Commit example: run eslint and mocha tests example: run selenium tests hudson-ci.org Hudson jenkins-ci.org Jenkins travis-ci.org circleci.com atlassian.com Bamboo jetbrains.com TeamCity
  • 34. End to End Tests Start Commit Code Staging QA Sign-off In ProdCode Review • Scenario driven • Tests client and server code • More “realistic” vs. Unit Tests • Less traceability vs. Unit Tests • Slower to execute vs. Unit Tests I ♥ selenium QAseleniumhq.org Selenium saucelabs.com browserstack.com
  • 36. End to End tests with Selenium Selenium Client API Test Script Selenium WebDriver Web Browser JavaScript, Java, C#, Python, … Converts commands to HTTP requests Communicates with web browser ...
  • 37. End to End tests with Selenium Selenium Client API Test Script Selenium Grid VM #1 VM #2 VM #5 IE 7 VM #4 IE 8 VM #6 IE 9
  • 38. End to End tests with Selenium module.exports = { 'Get free VMs': function (browser) { browser .url('http://www.modern.ie') .assert.title('Modern.IE') .end(); } }; Nightwatch.js
  • 39. Unit Testing with Mocha (node.js) test_file.js Node.js test_file.js code_to_test.js mocha test results
  • 40. Unit Testing with Mocha (node.js) module.exports = { name: function (title, first, last) { return [title, first, last].join(); } }; formatter.js
  • 41. Unit Testing with Mocha (node.js) var fmt = require('../../formatter'); var assert = require('assert'); ! describe('format', function () { it('should return full name', function () { var actual = fmt.name('Mr.', 'Bob', 'Rogers'); var expected = 'Mr. Bob Rogers'; ! assert.equal(actual, expected); }); }); formatter.spec.js
  • 42. Unit Testing with Venus & Mocha (browser) test_file.js Venus Node.js Browser test_file.js code_to_test.js mocha test results
  • 43. Unit Testing with Venus & Mocha (browser) function formatName(title, first, last) { return [title, first, last].join(); } ! formatter.js
  • 44. Unit Testing with Venus & Mocha (browser) /** * @venus-library mocha * @venus-code formatter.js */ describe('format', function () { var actual = formatName('Mr.', 'Bob', 'Rogers'); var expected = 'Mr. Bob Rogers'; ! it('should return full name', function () { expect(actual).to.be(expected); }); }); ! formatter.spec.js
  • 45. Unit Testing with Venus & Mocha (browser) <!DOCTYPE html> <html> <head> <title>Test for Formatter</title> <script type="text/javascript" src=“mocha.js”></script> <script type="text/javascript" src=“venus_client.js”></script> <script type="text/javascript" src="formatter.js"></script> <script type="text/javascript" src=“specs/formatter.spec.js”></script> <script type="text/javascript"> testLibrary.run(); </script> </head> <body> <div id="results"></div> </body> </html> test_harness.html
  • 46. Example: Spy var callback = sinon.spy(); ! PubSub.subscribe('message', callback); PubSub.publishSync('message'); ! assert(callback.called === true); ! A function that records information about how it is called. sinonjs.org
  • 49. A fake implementation of a code dependency. Sinon.js XHR Mock var xhr = sinon.useFakeXMLHttpRequest(); var requests = []; var callback = sinon.spy(); ! xhr.onCreate = function (xhr) { requests.push(xhr); }; ! myLib.getCommentsFor("/some/article", callback); assertEquals(1, requests.length); ! requests[0].respond(200, { "Content-Type": "application/json" }, '[{ "id": 12, "comment": "Hey there" }]' ); ! assert(callback.calledWith([{ id: 12, comment: "Hey there" }]));
  • 50. Code Samples End-to-End test with Selenium Unit Test node.js code Unit Test browser code 1 2 3 http://sethmcl.com/testing-web-apps/
  • 51. Learn More. Introduction to writing testable JavaScript! LinkedIn Tech Talk Smashing Magazine ! Unit Testing with Venus.js ! LinkedIn Tech Talk Venusjs.org ! End to End testing with Selenium! The Selenium Project Selenium Architecture Nightwatchjs.org ! Free Windows VMs and other testing resources! modern.ie http://sethmcl.com/testing-web-apps/