SlideShare uma empresa Scribd logo
1 de 35
AGENDA
Unit Testing Concept
Why Unit Testing?
Test Driven Development (a.k.a. TDD)
Basic Terms & Structure
Tools & Libraries
Unit Testing Specifics in JavaScript
Best Practices
UNIT TESTING CONCEPT
Unit testing is a method by which individual units of source code
are tested to determine if they are fit for use.
Unit is the smallest testable piece of
code.
Unit tests are created by programmers.
COMMON SENSE OR WHY UNIT TESTING?
Unit tests find problems early in the development cycle (TDD
& BDD)

Refactoring

Documentation
design

Integration

Better
IS UNIT TESTING A GOOD INVESTMENT?
Might slow down the development
process.
The tests may share the same blind
spots with the code.
Proving that components X and Y both
work independently doesn’t prove
that they’re compatible with one
another or configured correctly.
TEST DRIVEN DEVELOPMENT(TDD)
Test-driven development is
related to the test-first
programming concepts of
extreme programming (XP).
TDD is a software development
process that relies on the
repetition of short development
cycle shown on the screen to the
left.
Behavior-driven
development(BDD) based on
TDD…
ALRIGHT, SO WHAT IS BDD YOU ASK?
Behavior-driven development (BDD) isn't anything new or
revolutionary. It's just TDD with any test-related
terminology replaced by examples-of-behavior-related
terminology:
TDD term

BDD term

Test

Example

Assertion

Expectation

assert

should, expect

Unit

Behavior

Verification

Specification
ASSERTION
In simple words, the goal of assertion is to forcefully define
if the test fails or passes.
Example #1:
Statement

Passes(True)

Fails(False)

x = 1;

assert (x > 0)

assert (x < 0)

x++;

assert (x > 1)

assert (x < 1)
ASSERTION: EXAMPLE
Example(Chai) #2:
Given the function initialize():
function initialize() {
//… Some code goes here …
// The initialization went successfully.
return true;
}

Check that function initialize() returns true when called.
var isInitialized = initialize();

TDD syntax

BDD syntax

assert.isTrue(isInitialized)

expect(isInitialized).to.be.true
FIXTURE
A test fixture is a fixed state of the software under test used as a
baseline for running tests.
In JavaScript: simulate AJAX responses; loading known set of
data, such as html objects.
Example(Jasmine):
Require the piece of markup stored in myfixturemarkup.html file before each
test:
beforeEach(function() {
loadFixtures('myfixturemarkup.html');
});
STUB
Method stubs are functions with pre-programmed behavior.
Example (Sinon):
Forcing a method to throw an error in order to test error handling.
var fn = foo.stub().throws(Error);
expect(fn).to.throw(Error);
SPY
A test spy is a function that records arguments, return value, the
value of this and exception thrown (if any) for all its calls.
Example(Sinon):

Test that a function cursor.hide() has been only called once, and only
once.

sinon.spy(cursor, "hide");
TDD

sinon.assert.calledOnce(cursor.hide)

BDD

expect(cursor.hide.calledOnce).to.be.true
MOCK
 Mocks are fake objects with pre-programmed behavior (like
stubs) and pre-programmed expectations. They are like both
stubs and spies – in one.
 Mocks isolate the imitate the dependency and thus isolate the
unit test.
MOCK: EXAMPLE
Example(Sinon):
Create an expectation that jQuery.each is called once, and only once,
and also instructs the mock to behave as we pre-define.
var mock = sinon.mock(jQuery);

#1 – which method?(“jQuery.each”)
#2 – how many times it is called?(only once)
#3 – what are the arguments when the method called? (1, {})
#4 – what the method returns? (empty object)
BASIC STRUCTURE
#1. Setup/BeforeEach/Before
#2. Prepare an input
#3. Call a method
#4. Check an output
#5. Tear down/AfterEach/After
BASIC STRUCTURE EXPLAINED
#1. Setup/BeforeEach/Before.
before(function(done) {
// Create a basic document.
document = jsdom.jsdom();
window
done();
});

= document.parentWindow;
BASIC STRUCTURE EXPLAINED
before(function() { console.log(‘before test’); });
test(‘first test', function() { console.log(‘first test’); });

test(‘second test', function() { console.log(‘second test’); });
afterEach(function() { console.log(‘after each test’); });

Result:
before test
first test
after each test
second test
after each test
BASIC STRUCTURE EXPLAINED
it('should not initialize cursor if zoom level <= minimum zoom level.',
function(done) {

#2. Prepare an input and predicted result.
var zoomLevel = 1;
var expectedCursor = {‘color’:

‘white’, ‘height’:

#3. Call a method.
var actualCursor = cursor.init(zoomLevel);

#4. Check an output.
expect(actualCursor).to.deep.equal(expectedCursor);
done();
});

‘32px’, etc…};
BASIC STRUCTURE EXPLAINED
#5. Tear down/AfterEach/After.
after(function(done) {
// Remove global objects document.
document = null;
window
done();
});

= null;
OUTPUT: SUCCESS
<testsuite name="Macchiato Tests" tests="13" failures="0"
errors="0" skipped="0" timestamp="Mon, 02 Dec 2013
11:08:09 GMT" time="0.114">
<testcase classname=‚cursor #init ()" name="should not
initialize cursor if zoom level &lt; minimum

zoom level.‛
time="0.004"/>
</testsuite>
OUTPUT: FAILURE
<failure classname="cursor #init()" name="should not
initialize cursor if zoom level &lt; minimum zoom level."
time="0" message="Cannot read property 'show' of
undefined"><![CDATA[TypeError: Cannot read property 'show'
of undefined
// ..... Exception Stack Trace .....
</failure>
TOOLS
No framework
Known frameworks:
 qUnit(TDD)
 Jasmine(BDD)
 Mocha+ Chai(TDD & BDD)+ Sinon
 Etc…
TOOLS
What we use:
 Run UT: Mocha
 Run UT in parallel: Macchiato
 Assert/Expect: Chai
 W3C DOM in JavaScript: Jsdom
 Mock, spy, stub: Sinon
 Code coverage tool: None
UNIT TESTING SPECIFICS IN JAVASCRIPT
Stubbing jQuery plugin functions(mock jQuery.fn)
Testing Ajax requests

Stub jQuery.ajax
Fake XMLHttpRequest(XMLHTTP ActiveXObject)
Fake server

Mocking DOM elements
Load fake data via “fixtures”
Use W3C Javascript implementation Jsdom
BEST PRACTICES
Fast
Isolated

Consistent
Responsibility
Self-descriptive
No exception Handling
Use assertions when needed
Senior Frontend Developer at GlobalLogic
Co-founder of IT company DA-14
Contributor of HoganJs,
Backbone.Validations

http://da-14.com/
akhabibullina
_khabibullina
ua.linkedin.com/pub/anna-khabibullina/38/566/463/

Mais conteúdo relacionado

Mais procurados

Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript TestingKissy Team
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkArulalan T
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Kiki Ahmadi
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Andrea Francia
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Unit Test Your Database
Unit Test Your DatabaseUnit Test Your Database
Unit Test Your DatabaseDavid Wheeler
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using JasmineQuick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using JasmineGil Fink
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With PytestEddy Reyes
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG Greg.Helton
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesNarendra Pathai
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesHarry Potter
 
JDD 2016 - Sebastian Malaca - You Dont Need Unit Tests
JDD 2016 - Sebastian Malaca - You Dont Need Unit TestsJDD 2016 - Sebastian Malaca - You Dont Need Unit Tests
JDD 2016 - Sebastian Malaca - You Dont Need Unit TestsPROIDEA
 

Mais procurados (20)

Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Presentation Unit Testing process
Presentation Unit Testing processPresentation Unit Testing process
Presentation Unit Testing process
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Unit testing
Unit testingUnit testing
Unit testing
 
Auto testing!
Auto testing!Auto testing!
Auto testing!
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 
Python testing
Python  testingPython  testing
Python testing
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Unit Test Your Database
Unit Test Your DatabaseUnit Test Your Database
Unit Test Your Database
 
Quick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using JasmineQuick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
JDD 2016 - Sebastian Malaca - You Dont Need Unit Tests
JDD 2016 - Sebastian Malaca - You Dont Need Unit TestsJDD 2016 - Sebastian Malaca - You Dont Need Unit Tests
JDD 2016 - Sebastian Malaca - You Dont Need Unit Tests
 

Semelhante a In search of JavaScript code quality: unit testing

Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Anna Khabibullina
 
JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014DA-14
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmineGil Fink
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingVisual Engineering
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSKnoldus Inc.
 
Javascript tdd byandreapaciolla
Javascript tdd byandreapaciollaJavascript tdd byandreapaciolla
Javascript tdd byandreapaciollaAndrea Paciolla
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And DrupalPeter Arato
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptdavejohnson
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e bigAndy Peterson
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockRobot Media
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineGil Fink
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialAnup Singh
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It'sJim Lynch
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
Android testing
Android testingAndroid testing
Android testingSean Tsai
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Howsatesgoral
 

Semelhante a In search of JavaScript code quality: unit testing (20)

Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)
 
JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014JS Frameworks Day April,26 of 2014
JS Frameworks Day April,26 of 2014
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmine
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
TDD & BDD
TDD & BDDTDD & BDD
TDD & BDD
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
 
Javascript tdd byandreapaciolla
Javascript tdd byandreapaciollaJavascript tdd byandreapaciolla
Javascript tdd byandreapaciolla
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Android testing
Android testingAndroid testing
Android testing
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Hows
 

Mais de Anna Khabibullina

Key Challenges of Smart Home
Key Challenges of Smart HomeKey Challenges of Smart Home
Key Challenges of Smart HomeAnna Khabibullina
 
Influence without authority annakh@ slideshare
Influence without authority   annakh@ slideshareInfluence without authority   annakh@ slideshare
Influence without authority annakh@ slideshareAnna Khabibullina
 
Don’t forget to add doctype
Don’t forget to add doctypeDon’t forget to add doctype
Don’t forget to add doctypeAnna Khabibullina
 
Web Accessibility: You can and You should
Web Accessibility: You can and You shouldWeb Accessibility: You can and You should
Web Accessibility: You can and You shouldAnna Khabibullina
 
Typescript: A Story Of A Project
Typescript: A Story Of A ProjectTypescript: A Story Of A Project
Typescript: A Story Of A ProjectAnna Khabibullina
 

Mais de Anna Khabibullina (6)

Key Challenges of Smart Home
Key Challenges of Smart HomeKey Challenges of Smart Home
Key Challenges of Smart Home
 
Influence without authority annakh@ slideshare
Influence without authority   annakh@ slideshareInfluence without authority   annakh@ slideshare
Influence without authority annakh@ slideshare
 
Don’t forget to add doctype
Don’t forget to add doctypeDon’t forget to add doctype
Don’t forget to add doctype
 
Web Accessibility: You can and You should
Web Accessibility: You can and You shouldWeb Accessibility: You can and You should
Web Accessibility: You can and You should
 
SVG Accessibility
SVG AccessibilitySVG Accessibility
SVG Accessibility
 
Typescript: A Story Of A Project
Typescript: A Story Of A ProjectTypescript: A Story Of A Project
Typescript: A Story Of A Project
 

In search of JavaScript code quality: unit testing

  • 1.
  • 2. AGENDA Unit Testing Concept Why Unit Testing? Test Driven Development (a.k.a. TDD) Basic Terms & Structure Tools & Libraries Unit Testing Specifics in JavaScript Best Practices
  • 3.
  • 4. UNIT TESTING CONCEPT Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. Unit is the smallest testable piece of code. Unit tests are created by programmers.
  • 5.
  • 6. COMMON SENSE OR WHY UNIT TESTING? Unit tests find problems early in the development cycle (TDD & BDD) Refactoring Documentation design Integration Better
  • 7. IS UNIT TESTING A GOOD INVESTMENT? Might slow down the development process. The tests may share the same blind spots with the code. Proving that components X and Y both work independently doesn’t prove that they’re compatible with one another or configured correctly.
  • 8.
  • 9. TEST DRIVEN DEVELOPMENT(TDD) Test-driven development is related to the test-first programming concepts of extreme programming (XP). TDD is a software development process that relies on the repetition of short development cycle shown on the screen to the left. Behavior-driven development(BDD) based on TDD…
  • 10. ALRIGHT, SO WHAT IS BDD YOU ASK? Behavior-driven development (BDD) isn't anything new or revolutionary. It's just TDD with any test-related terminology replaced by examples-of-behavior-related terminology: TDD term BDD term Test Example Assertion Expectation assert should, expect Unit Behavior Verification Specification
  • 11.
  • 12. ASSERTION In simple words, the goal of assertion is to forcefully define if the test fails or passes. Example #1: Statement Passes(True) Fails(False) x = 1; assert (x > 0) assert (x < 0) x++; assert (x > 1) assert (x < 1)
  • 13. ASSERTION: EXAMPLE Example(Chai) #2: Given the function initialize(): function initialize() { //… Some code goes here … // The initialization went successfully. return true; } Check that function initialize() returns true when called. var isInitialized = initialize(); TDD syntax BDD syntax assert.isTrue(isInitialized) expect(isInitialized).to.be.true
  • 14. FIXTURE A test fixture is a fixed state of the software under test used as a baseline for running tests. In JavaScript: simulate AJAX responses; loading known set of data, such as html objects. Example(Jasmine): Require the piece of markup stored in myfixturemarkup.html file before each test: beforeEach(function() { loadFixtures('myfixturemarkup.html'); });
  • 15. STUB Method stubs are functions with pre-programmed behavior. Example (Sinon): Forcing a method to throw an error in order to test error handling. var fn = foo.stub().throws(Error); expect(fn).to.throw(Error);
  • 16. SPY A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls. Example(Sinon): Test that a function cursor.hide() has been only called once, and only once. sinon.spy(cursor, "hide"); TDD sinon.assert.calledOnce(cursor.hide) BDD expect(cursor.hide.calledOnce).to.be.true
  • 17. MOCK  Mocks are fake objects with pre-programmed behavior (like stubs) and pre-programmed expectations. They are like both stubs and spies – in one.  Mocks isolate the imitate the dependency and thus isolate the unit test.
  • 18. MOCK: EXAMPLE Example(Sinon): Create an expectation that jQuery.each is called once, and only once, and also instructs the mock to behave as we pre-define. var mock = sinon.mock(jQuery); #1 – which method?(“jQuery.each”) #2 – how many times it is called?(only once) #3 – what are the arguments when the method called? (1, {}) #4 – what the method returns? (empty object)
  • 19.
  • 20. BASIC STRUCTURE #1. Setup/BeforeEach/Before #2. Prepare an input #3. Call a method #4. Check an output #5. Tear down/AfterEach/After
  • 21. BASIC STRUCTURE EXPLAINED #1. Setup/BeforeEach/Before. before(function(done) { // Create a basic document. document = jsdom.jsdom(); window done(); }); = document.parentWindow;
  • 22. BASIC STRUCTURE EXPLAINED before(function() { console.log(‘before test’); }); test(‘first test', function() { console.log(‘first test’); }); test(‘second test', function() { console.log(‘second test’); }); afterEach(function() { console.log(‘after each test’); }); Result: before test first test after each test second test after each test
  • 23. BASIC STRUCTURE EXPLAINED it('should not initialize cursor if zoom level <= minimum zoom level.', function(done) { #2. Prepare an input and predicted result. var zoomLevel = 1; var expectedCursor = {‘color’: ‘white’, ‘height’: #3. Call a method. var actualCursor = cursor.init(zoomLevel); #4. Check an output. expect(actualCursor).to.deep.equal(expectedCursor); done(); }); ‘32px’, etc…};
  • 24. BASIC STRUCTURE EXPLAINED #5. Tear down/AfterEach/After. after(function(done) { // Remove global objects document. document = null; window done(); }); = null;
  • 25.
  • 26. OUTPUT: SUCCESS <testsuite name="Macchiato Tests" tests="13" failures="0" errors="0" skipped="0" timestamp="Mon, 02 Dec 2013 11:08:09 GMT" time="0.114"> <testcase classname=‚cursor #init ()" name="should not initialize cursor if zoom level &lt; minimum zoom level.‛ time="0.004"/> </testsuite>
  • 27. OUTPUT: FAILURE <failure classname="cursor #init()" name="should not initialize cursor if zoom level &lt; minimum zoom level." time="0" message="Cannot read property 'show' of undefined"><![CDATA[TypeError: Cannot read property 'show' of undefined // ..... Exception Stack Trace ..... </failure>
  • 28.
  • 29. TOOLS No framework Known frameworks:  qUnit(TDD)  Jasmine(BDD)  Mocha+ Chai(TDD & BDD)+ Sinon  Etc…
  • 30. TOOLS What we use:  Run UT: Mocha  Run UT in parallel: Macchiato  Assert/Expect: Chai  W3C DOM in JavaScript: Jsdom  Mock, spy, stub: Sinon  Code coverage tool: None
  • 31.
  • 32. UNIT TESTING SPECIFICS IN JAVASCRIPT Stubbing jQuery plugin functions(mock jQuery.fn) Testing Ajax requests Stub jQuery.ajax Fake XMLHttpRequest(XMLHTTP ActiveXObject) Fake server Mocking DOM elements Load fake data via “fixtures” Use W3C Javascript implementation Jsdom
  • 33.
  • 35. Senior Frontend Developer at GlobalLogic Co-founder of IT company DA-14 Contributor of HoganJs, Backbone.Validations http://da-14.com/ akhabibullina _khabibullina ua.linkedin.com/pub/anna-khabibullina/38/566/463/