SlideShare ist ein Scribd-Unternehmen logo
1 von 60
Zero to Testing in
JavaScript
Basics of testing, in JS
About me
• Blog: thewebivore.com
• Twitter: @pamasaur
• Co-organizer of Philadelphia JavaScript Developers
• Podcasting on Turing Incomplete (@turingcool)
• Testing fanatic
@pamasaur | #midwestjs | thewebivore.com
@pamasaur | #midwestjs | thewebivore.com
Welcome to the testing track?
@pamasaur | #midwestjs | thewebivore.com
*pause* … A TESTING TRACK!
@pamasaur | #midwestjs | thewebivore.com
Let’s kick this off right!
Agenda
• My testing story
• Testing theory
• Basic test walk-through
• Testing frameworks
• Other forms of testing
• Working testing into your workflow
• Minishop (workshop)
@pamasaur | #midwestjs | thewebivore.com
My testing story
When I first started writing tests
Code Retreat
• TDD
• Pairing
• Throw-away code
@pamasaur | #midwestjs | thewebivore.com
@pamasaur | #midwestjs | thewebivore.com
What about tests in my projects?
@pamasaur | #midwestjs | thewebivore.com
previously:
“Tests* are doing the dishes”
* and writing docs and fixing bad code
@pamasaur | #midwestjs | thewebivore.com
aka:
“Tests are work for people you don’t like”
@pamasaur | #midwestjs | thewebivore.com
The other piece that I would add to this is, and I’ve said
this before at talks, that testing isn’t something that you do
because it is good to be testing. It’s not your lettuce.
You do it because it increases your confidence in your
code. And it increases your ability to release quickly and
make changes without worrying that something is going to
break. And so, if you’re paranoid like me, that’s a good
incentive to do testing. And if you have a whole lot of self-
confidence, maybe you’re just a really great coder. But
maybe it’s a little bit more likely that you’re
going to make mistakes and you might pay for it
a little bit later.
Julie Ralph, Protractor
http://javascriptjabber.com/106-jsj-protractor-with-julie-ralph/
@pamasaur | #midwestjs | thewebivore.com
@pamasaur | #midwestjs | thewebivore.com
now:
GOOD developers write tests
@pamasaur | #midwestjs | thewebivore.com
Literally.
@pamasaur | #midwestjs | thewebivore.com
-b for bad coder
What do you call code without tests?
Legacy code.
@pamasaur | #midwestjs | thewebivore.com
Testing basics
1. When you break it, it breaks!
2. Secondary line of documentation defense
3. Design tool
@pamasaur | #midwestjs | thewebivore.com
When you break it, it breaks!
• Testing is required to enable refactoring
• Regularly run tests
• Use test results to make decisions
@pamasaur | #midwestjs | thewebivore.com
Documentation defense
• Tests describe behavior
• Read tests to understand behavior
• Write tests to describe behavior
• Write tests to validate behavior
@pamasaur | #midwestjs | thewebivore.com
Tests as design tool
• Tests describe functionality
• Write functionality description (test)
• Write code to make it work
@pamasaur | #midwestjs | thewebivore.com
BDD/TDD
• Behavior Driven Development
• Test-Driven Development
“Write tests to inform how you write your code”
@pamasaur | #midwestjs | thewebivore.com
BDD
• describe
• it
• before
• after
• beforeEach
• afterEach
@pamasaur | #midwestjs | thewebivore.com
TDD
• suite
• test
• setup
• teardown
@pamasaur | #midwestjs | thewebivore.com
Test Ever vs. Test Never
@pamasaur | #midwestjs | thewebivore.com
Types of testing
• Unit
• Integration
• Functional
• E2E
@pamasaur | #midwestjs | thewebivore.com
Unit
Testing functionality in isolation
“When I call addNumbers, it returns the value of the added
numbers”
@pamasaur | #midwestjs | thewebivore.com
Unit testing takeaway:
Test your code
@pamasaur | #midwestjs | thewebivore.com
How to only test your code?
• Isolate
• Faking
@pamasaur | #midwestjs | thewebivore.com
Fake things: Spies, stubs, and mocks
• Spy: an object that records its interactions
• Stubs: fake objects
• Mocks: fake objects with expected behavior
Generally, you can SPY on a function, STUB an
object, and MOCK a service.
@pamasaur | #midwestjs | thewebivore.com
Integration
• Multiple pieces of code together
“Is the router setting up my models correctly?”
@pamasaur | #midwestjs | thewebivore.com
Functional
• Does your code work as product expects it to? (functional
requirements)
“When I click the login button, it should log me in”
@pamasaur | #midwestjs | thewebivore.com
E2E (end-to-end)
• Functional, but fully integrated with external services, simulate
real-time situations.
“When I click ‘Login with Google,’ it logs me in”
@pamasaur | #midwestjs | thewebivore.com
JavaScript test walk-through
JavaScript test walk-through
• Setting up your base
• Writing the test/expectations*
• Making it pass*
* The order of these is debatable
@pamasaur | #midwestjs | thewebivore.com
Jasmine
• jasmine.github.io
• Download a standalone version on GitHub from pivotal/jasmine
• Node: jasmine-node (fork of karma-jasmine)
@pamasaur | #midwestjs | thewebivore.com
Mocha
• visionmedia.github.io/mocha
• Node!
• npm install –g mocha
@pamasaur | #midwestjs | thewebivore.com
Anatomy of a test
Describe [thing you’re testing]
It [does something you expect it to do]
Rinse and repeat.
@pamasaur | #midwestjs | thewebivore.com
Example test walk-through
with Mocha
var assert = require('assert');
describe("An area of my application", function() {
it("should know that 2 and 2 is 4", function(){
assert.equal(4, 2+2);
});
});
@pamasaur | #midwestjs | thewebivore.com
describe("An area of my application", function() {
it("should know that 2 and 2 is 4", function(){
assert.equal(4, 2+2);
});
});
@pamasaur | #midwestjs | thewebivore.com
var assert = require('assert');
describe( , function() {
it("should know that 2 and 2 is 4", function(){
assert.equal(4, 2+2);
});
});
@pamasaur | #midwestjs | thewebivore.com
var assert = require('assert');
describe("An area of my application", {
it("should know that 2 and 2 is 4", {
assert.equal(4, 2+2);
});
});
@pamasaur | #midwestjs | thewebivore.com
var assert = require('assert');
describe("An area of my application", function() {
it( , function(){
});
});
@pamasaur | #midwestjs | thewebivore.com
@pamasaur | #midwestjs | thewebivore.com
Testing Tools
Test describers
• Jasmine
• Mocha
• QUnit
• node-tap
• YUI Test
@pamasaur | #midwestjs | thewebivore.com
Assertions
• Chai
• should.js
• Expect.js
• better-assert
@pamasaur | #midwestjs | thewebivore.com
Spies, stubs, and mocks
• Sinon.js
• Jest from Facebook
@pamasaur | #midwestjs | thewebivore.com
Test runners
• Karma
• Testem
• YUI Yeti
@pamasaur | #midwestjs | thewebivore.com
Bringing testing into the fold
3 tips for making testing a regular part of your world
#1: Teach testing
• Attend talks like this!
• Practice (ex. Code Retreat)
• Pair programming
@pamasaur | #midwestjs | thewebivore.com
#2: Code coverage
• Istanbul
• Blanket.js
@pamasaur | #midwestjs | thewebivore.com
#3: Code review
• Quality assurance
• Mentoring
• Don’t accept without tests!
@pamasaur | #midwestjs | thewebivore.com
What’d we learn?
• Basics of testing
• Writing a JavaScript test
• Tools in JavaScript for testing
• Ways to create a testing culture
@pamasaur | #midwestjs | thewebivore.com
Minishop
(workshop, playshop …)
TODO
• Set up karma (our test runner)
• With Jasmine (our assertion framework)
@pamasaur | #midwestjs | thewebivore.com
TODO
• Write a failing test
• Correct the failing test
@pamasaur | #midwestjs | thewebivore.com
TODO
• Write another test
• Setup in beforeEach
@pamasaur | #midwestjs | thewebivore.com
Thank you!
• Find me online at
• @pamasaur
• thewebivore.com (blog)
• turing.cool (podcast)
• thewebivore.com/book (book!)
@pamasaur | #midwestjs | thewebivore.com

Weitere ähnliche Inhalte

Was ist angesagt?

Lunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and CapybaraLunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and Capybara
Marc Seeger
 
Objective C 基本介紹
Objective C 基本介紹Objective C 基本介紹
Objective C 基本介紹
Giga Cheng
 

Was ist angesagt? (20)

Introduction to testing in Rails
Introduction to testing in RailsIntroduction to testing in Rails
Introduction to testing in Rails
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
Introduction To Web Application Testing
Introduction To Web Application TestingIntroduction To Web Application Testing
Introduction To Web Application Testing
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 
Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101Javascript Testing with Jasmine 101
Javascript Testing with Jasmine 101
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
Lunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and CapybaraLunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and Capybara
 
RSpec on Rails Tutorial
RSpec on Rails TutorialRSpec on Rails Tutorial
RSpec on Rails Tutorial
 
Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with Cucumber
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
 
Getting Answers to Your Testing Questions
Getting Answers to Your Testing QuestionsGetting Answers to Your Testing Questions
Getting Answers to Your Testing Questions
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram ArnoldEfficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
 
Refactoring Away from Test Hell
Refactoring Away from Test HellRefactoring Away from Test Hell
Refactoring Away from Test Hell
 
Selenium bootcamp slides
Selenium bootcamp slides   Selenium bootcamp slides
Selenium bootcamp slides
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
Objective C 基本介紹
Objective C 基本介紹Objective C 基本介紹
Objective C 基本介紹
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6
 

Andere mochten auch (6)

Displacing Worst Practices in CSS
Displacing Worst Practices in CSSDisplacing Worst Practices in CSS
Displacing Worst Practices in CSS
 
Simple Proxying in Rails
Simple Proxying in RailsSimple Proxying in Rails
Simple Proxying in Rails
 
Selecting a Web Framework
Selecting a Web FrameworkSelecting a Web Framework
Selecting a Web Framework
 
Feminism & Open Source Contribution
Feminism & Open Source ContributionFeminism & Open Source Contribution
Feminism & Open Source Contribution
 
WordPress 101 Sunday Session
WordPress 101 Sunday SessionWordPress 101 Sunday Session
WordPress 101 Sunday Session
 
Sadia Afroz: Detecting Hoaxes, Frauds, and Deception in Writing Style Online
Sadia Afroz: Detecting Hoaxes, Frauds, and Deception in Writing Style Online  Sadia Afroz: Detecting Hoaxes, Frauds, and Deception in Writing Style Online
Sadia Afroz: Detecting Hoaxes, Frauds, and Deception in Writing Style Online
 

Ähnlich wie MidwestJS Zero to Testing

An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
Steven Casey
 

Ähnlich wie MidwestJS Zero to Testing (20)

Apex Testing Deep Dive
Apex Testing Deep DiveApex Testing Deep Dive
Apex Testing Deep Dive
 
Node.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.jsNode.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.js
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To Testing
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRW
 
Data Driven DevOps
Data Driven DevOpsData Driven DevOps
Data Driven DevOps
 
Foundation selenium java
Foundation selenium java Foundation selenium java
Foundation selenium java
 
Anatomy of Test Driven Development
Anatomy of Test Driven DevelopmentAnatomy of Test Driven Development
Anatomy of Test Driven Development
 
The what, why and how of web analytics testing
The what, why and how of web analytics testingThe what, why and how of web analytics testing
The what, why and how of web analytics testing
 
Browser Automated Testing Frameworks - Nightwatch.js
Browser Automated Testing Frameworks - Nightwatch.jsBrowser Automated Testing Frameworks - Nightwatch.js
Browser Automated Testing Frameworks - Nightwatch.js
 
How to not blow up spaceships
How to not blow up spaceshipsHow to not blow up spaceships
How to not blow up spaceships
 
Just Mock It - Mocks and Stubs
Just Mock It - Mocks and StubsJust Mock It - Mocks and Stubs
Just Mock It - Mocks and Stubs
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
My Little Webap - DevOpsSec is Magic
My Little Webap - DevOpsSec is MagicMy Little Webap - DevOpsSec is Magic
My Little Webap - DevOpsSec is Magic
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
Away day
Away dayAway day
Away day
 
Measuring Coverage From E2E Tests
Measuring Coverage From E2E TestsMeasuring Coverage From E2E Tests
Measuring Coverage From E2E Tests
 
Data driven devops as presented at QCon London 2018
Data driven devops as presented at QCon London 2018Data driven devops as presented at QCon London 2018
Data driven devops as presented at QCon London 2018
 
Selenium Tips & Tricks - StarWest 2015
Selenium Tips & Tricks - StarWest 2015Selenium Tips & Tricks - StarWest 2015
Selenium Tips & Tricks - StarWest 2015
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
A Deep Dive Into SEO Tactics For Modern Javascript Frameworks
A Deep Dive Into SEO Tactics For Modern Javascript FrameworksA Deep Dive Into SEO Tactics For Modern Javascript Frameworks
A Deep Dive Into SEO Tactics For Modern Javascript Frameworks
 

Mehr von pamselle (11)

WordPress 101 Saturday Session
WordPress 101 Saturday SessionWordPress 101 Saturday Session
WordPress 101 Saturday Session
 
Power Spriting With Compass
Power Spriting With CompassPower Spriting With Compass
Power Spriting With Compass
 
Aylin Caliskan: Quantifying the Translator Effect: Identifying authors and ma...
Aylin Caliskan: Quantifying the Translator Effect: Identifying authors and ma...Aylin Caliskan: Quantifying the Translator Effect: Identifying authors and ma...
Aylin Caliskan: Quantifying the Translator Effect: Identifying authors and ma...
 
Kamelia Aryafar: Musical Genre Classification Using Sparsity-Eager Support Ve...
Kamelia Aryafar: Musical Genre Classification Using Sparsity-Eager Support Ve...Kamelia Aryafar: Musical Genre Classification Using Sparsity-Eager Support Ve...
Kamelia Aryafar: Musical Genre Classification Using Sparsity-Eager Support Ve...
 
GDI WordPress 4 January 2012 (white)
GDI WordPress 4 January 2012 (white)GDI WordPress 4 January 2012 (white)
GDI WordPress 4 January 2012 (white)
 
GDI WordPress 4 January 2012
GDI WordPress 4 January 2012GDI WordPress 4 January 2012
GDI WordPress 4 January 2012
 
GDI WordPress 3 January 2012 (white background)
GDI WordPress 3 January 2012 (white background)GDI WordPress 3 January 2012 (white background)
GDI WordPress 3 January 2012 (white background)
 
GDI WordPress 3 January 2012
GDI WordPress 3 January 2012GDI WordPress 3 January 2012
GDI WordPress 3 January 2012
 
GDI WordPress 2 January 2012
GDI WordPress 2 January 2012 GDI WordPress 2 January 2012
GDI WordPress 2 January 2012
 
Gdi word press_2
Gdi word press_2Gdi word press_2
Gdi word press_2
 
GDI WordPress 1 January 2012
GDI WordPress 1 January 2012GDI WordPress 1 January 2012
GDI WordPress 1 January 2012
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

MidwestJS Zero to Testing

  • 1. Zero to Testing in JavaScript Basics of testing, in JS
  • 2. About me • Blog: thewebivore.com • Twitter: @pamasaur • Co-organizer of Philadelphia JavaScript Developers • Podcasting on Turing Incomplete (@turingcool) • Testing fanatic @pamasaur | #midwestjs | thewebivore.com
  • 3. @pamasaur | #midwestjs | thewebivore.com Welcome to the testing track?
  • 4. @pamasaur | #midwestjs | thewebivore.com *pause* … A TESTING TRACK!
  • 5. @pamasaur | #midwestjs | thewebivore.com Let’s kick this off right!
  • 6. Agenda • My testing story • Testing theory • Basic test walk-through • Testing frameworks • Other forms of testing • Working testing into your workflow • Minishop (workshop) @pamasaur | #midwestjs | thewebivore.com
  • 8. When I first started writing tests Code Retreat • TDD • Pairing • Throw-away code @pamasaur | #midwestjs | thewebivore.com
  • 9. @pamasaur | #midwestjs | thewebivore.com What about tests in my projects?
  • 10. @pamasaur | #midwestjs | thewebivore.com previously: “Tests* are doing the dishes” * and writing docs and fixing bad code
  • 11. @pamasaur | #midwestjs | thewebivore.com aka: “Tests are work for people you don’t like”
  • 12. @pamasaur | #midwestjs | thewebivore.com The other piece that I would add to this is, and I’ve said this before at talks, that testing isn’t something that you do because it is good to be testing. It’s not your lettuce. You do it because it increases your confidence in your code. And it increases your ability to release quickly and make changes without worrying that something is going to break. And so, if you’re paranoid like me, that’s a good incentive to do testing. And if you have a whole lot of self- confidence, maybe you’re just a really great coder. But maybe it’s a little bit more likely that you’re going to make mistakes and you might pay for it a little bit later. Julie Ralph, Protractor http://javascriptjabber.com/106-jsj-protractor-with-julie-ralph/
  • 13. @pamasaur | #midwestjs | thewebivore.com
  • 14. @pamasaur | #midwestjs | thewebivore.com now: GOOD developers write tests
  • 15. @pamasaur | #midwestjs | thewebivore.com Literally.
  • 16. @pamasaur | #midwestjs | thewebivore.com -b for bad coder
  • 17. What do you call code without tests? Legacy code. @pamasaur | #midwestjs | thewebivore.com
  • 18. Testing basics 1. When you break it, it breaks! 2. Secondary line of documentation defense 3. Design tool @pamasaur | #midwestjs | thewebivore.com
  • 19. When you break it, it breaks! • Testing is required to enable refactoring • Regularly run tests • Use test results to make decisions @pamasaur | #midwestjs | thewebivore.com
  • 20. Documentation defense • Tests describe behavior • Read tests to understand behavior • Write tests to describe behavior • Write tests to validate behavior @pamasaur | #midwestjs | thewebivore.com
  • 21. Tests as design tool • Tests describe functionality • Write functionality description (test) • Write code to make it work @pamasaur | #midwestjs | thewebivore.com
  • 22. BDD/TDD • Behavior Driven Development • Test-Driven Development “Write tests to inform how you write your code” @pamasaur | #midwestjs | thewebivore.com
  • 23. BDD • describe • it • before • after • beforeEach • afterEach @pamasaur | #midwestjs | thewebivore.com
  • 24. TDD • suite • test • setup • teardown @pamasaur | #midwestjs | thewebivore.com
  • 25. Test Ever vs. Test Never @pamasaur | #midwestjs | thewebivore.com
  • 26. Types of testing • Unit • Integration • Functional • E2E @pamasaur | #midwestjs | thewebivore.com
  • 27. Unit Testing functionality in isolation “When I call addNumbers, it returns the value of the added numbers” @pamasaur | #midwestjs | thewebivore.com
  • 28. Unit testing takeaway: Test your code @pamasaur | #midwestjs | thewebivore.com
  • 29. How to only test your code? • Isolate • Faking @pamasaur | #midwestjs | thewebivore.com
  • 30. Fake things: Spies, stubs, and mocks • Spy: an object that records its interactions • Stubs: fake objects • Mocks: fake objects with expected behavior Generally, you can SPY on a function, STUB an object, and MOCK a service. @pamasaur | #midwestjs | thewebivore.com
  • 31. Integration • Multiple pieces of code together “Is the router setting up my models correctly?” @pamasaur | #midwestjs | thewebivore.com
  • 32. Functional • Does your code work as product expects it to? (functional requirements) “When I click the login button, it should log me in” @pamasaur | #midwestjs | thewebivore.com
  • 33. E2E (end-to-end) • Functional, but fully integrated with external services, simulate real-time situations. “When I click ‘Login with Google,’ it logs me in” @pamasaur | #midwestjs | thewebivore.com
  • 35. JavaScript test walk-through • Setting up your base • Writing the test/expectations* • Making it pass* * The order of these is debatable @pamasaur | #midwestjs | thewebivore.com
  • 36. Jasmine • jasmine.github.io • Download a standalone version on GitHub from pivotal/jasmine • Node: jasmine-node (fork of karma-jasmine) @pamasaur | #midwestjs | thewebivore.com
  • 37. Mocha • visionmedia.github.io/mocha • Node! • npm install –g mocha @pamasaur | #midwestjs | thewebivore.com
  • 38. Anatomy of a test Describe [thing you’re testing] It [does something you expect it to do] Rinse and repeat. @pamasaur | #midwestjs | thewebivore.com
  • 40. var assert = require('assert'); describe("An area of my application", function() { it("should know that 2 and 2 is 4", function(){ assert.equal(4, 2+2); }); }); @pamasaur | #midwestjs | thewebivore.com
  • 41. describe("An area of my application", function() { it("should know that 2 and 2 is 4", function(){ assert.equal(4, 2+2); }); }); @pamasaur | #midwestjs | thewebivore.com
  • 42. var assert = require('assert'); describe( , function() { it("should know that 2 and 2 is 4", function(){ assert.equal(4, 2+2); }); }); @pamasaur | #midwestjs | thewebivore.com
  • 43. var assert = require('assert'); describe("An area of my application", { it("should know that 2 and 2 is 4", { assert.equal(4, 2+2); }); }); @pamasaur | #midwestjs | thewebivore.com
  • 44. var assert = require('assert'); describe("An area of my application", function() { it( , function(){ }); }); @pamasaur | #midwestjs | thewebivore.com
  • 45. @pamasaur | #midwestjs | thewebivore.com
  • 47. Test describers • Jasmine • Mocha • QUnit • node-tap • YUI Test @pamasaur | #midwestjs | thewebivore.com
  • 48. Assertions • Chai • should.js • Expect.js • better-assert @pamasaur | #midwestjs | thewebivore.com
  • 49. Spies, stubs, and mocks • Sinon.js • Jest from Facebook @pamasaur | #midwestjs | thewebivore.com
  • 50. Test runners • Karma • Testem • YUI Yeti @pamasaur | #midwestjs | thewebivore.com
  • 51. Bringing testing into the fold 3 tips for making testing a regular part of your world
  • 52. #1: Teach testing • Attend talks like this! • Practice (ex. Code Retreat) • Pair programming @pamasaur | #midwestjs | thewebivore.com
  • 53. #2: Code coverage • Istanbul • Blanket.js @pamasaur | #midwestjs | thewebivore.com
  • 54. #3: Code review • Quality assurance • Mentoring • Don’t accept without tests! @pamasaur | #midwestjs | thewebivore.com
  • 55. What’d we learn? • Basics of testing • Writing a JavaScript test • Tools in JavaScript for testing • Ways to create a testing culture @pamasaur | #midwestjs | thewebivore.com
  • 57. TODO • Set up karma (our test runner) • With Jasmine (our assertion framework) @pamasaur | #midwestjs | thewebivore.com
  • 58. TODO • Write a failing test • Correct the failing test @pamasaur | #midwestjs | thewebivore.com
  • 59. TODO • Write another test • Setup in beforeEach @pamasaur | #midwestjs | thewebivore.com
  • 60. Thank you! • Find me online at • @pamasaur • thewebivore.com (blog) • turing.cool (podcast) • thewebivore.com/book (book!) @pamasaur | #midwestjs | thewebivore.com

Hinweis der Redaktion

  1. Fascinating data, and can help you monitor code quality over time
  2. Code review is great for 1m reasons, and enforcing testing is just one of them