zero to testing in javascript

36
Zero to Testing in JavaScript Basics of testing in JS

Upload: pamselle

Post on 07-May-2015

318 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Zero to Testing in JavaScript

Zero to Testing in JavaScriptBasics of testing in JS

Page 2: Zero to Testing in JavaScript

About me

• Software developer for Xfinity.com

• Online: thewebivore.com, @pamasaur

• Co-organizer of Philadelphia JavaScript Developers

• Testing fanatic

Page 3: Zero to Testing in JavaScript

Agenda

• My testing story

• Writing your first JavaScript test

• Testing frameworks

• Working testing into your workflow

Page 4: Zero to Testing in JavaScript

My testing story

Page 5: Zero to Testing in JavaScript

My Testing Story

• Code Retreat• TDD

• Pairing

• Throw-away code

• Testing at work

• Now: test coverage drops break the build

Page 6: Zero to Testing in JavaScript

What’s the deal with testing?

• When you break it, it breaks!

• Secondary line of documentation defense

• Design tool

Page 7: Zero to Testing in JavaScript

What do you call code without tests?

Legacy code.

Page 8: Zero to Testing in JavaScript

Testing front-end code

• The debt in your /js folder

• Front-end code has logic too!

If you have logic, you need tests

Page 9: Zero to Testing in JavaScript

BDD/TDD

• Behavior Driven Development

• Test-Driven Development

“Write tests to inform how you write your code”

• BDD: Describe, it, before, after, beforeEach, afterEach

• TDD: Suite, test, setup, teardown

Page 10: Zero to Testing in JavaScript

Test Ever vs. Test Never

Page 11: Zero to Testing in JavaScript

Writing your first JavaScript test

Page 12: Zero to Testing in JavaScript

Writing your first JavaScript test

• Setting up your base

• Writing the test*

• Making it pass*

* The order of these is debatable

Page 13: Zero to Testing in JavaScript

Jasmine

• jasmine.github.io

• Download a standalone version on GitHub from pivotal/jasmine

• Or use RubyGems for a Ruby environment

Page 14: Zero to Testing in JavaScript

Mocha

• visionmedia.github.io/mocha

• Node!

• npm install –g mocha

Page 15: Zero to Testing in JavaScript

Anatomy of a test

Describe [thing you’re testing]

It [does something you expect it to do]

Rinse and repeat.

Page 16: Zero to Testing in JavaScript

Example test walk-throughwith Mocha

Page 17: Zero to Testing in JavaScript

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);

});

});

Page 18: Zero to Testing in JavaScript

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);

});

});

Page 19: Zero to Testing in JavaScript

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);

});

});

Page 20: Zero to Testing in JavaScript

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);

});

});

Page 21: Zero to Testing in JavaScript

var assert = require('assert');

describe("An area of my application", function() {

it("should know that 2 and 2 is 4", function(){

assert.equal(2+2, 4);

});

});

Page 22: Zero to Testing in JavaScript
Page 23: Zero to Testing in JavaScript

Testing concepts: Unit testing

Test your code

Page 24: Zero to Testing in JavaScript

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.

Page 25: Zero to Testing in JavaScript

Live coding?Write a test with me!

Page 26: Zero to Testing in JavaScript

Testing Tools

Page 29: Zero to Testing in JavaScript

Spies, stubs, and mocks

• Sinon.js

• Jest from Facebook

Page 31: Zero to Testing in JavaScript

Bringing testing into the fold3 tips for making testing a regular part of your world

Page 32: Zero to Testing in JavaScript

#1: Teach testing

• Attend talks like this!

• Practice (ex. Code Retreat)

• Pair programming

Page 33: Zero to Testing in JavaScript

#2: Code coverage

• Istanbul

• Blanket.js

• hrcov (Mozilla)

• JSCover

Page 34: Zero to Testing in JavaScript

#3: Code review

• Quality assurance

• Mentoring

• Don’t accept without tests!

Page 35: Zero to Testing in JavaScript

What’d we learn?

• Writing a JavaScript test

• Tools in JavaScript for testing

• Ways to create a testing culture

Page 36: Zero to Testing in JavaScript

Thank you!

• Find me online at• @pamasaur

• thewebivore.com (blog)

• turing.cool (podcast)

• bleedingedgepress.com (upcoming book on JS frameworks)