SlideShare a Scribd company logo
Unittesting JavaScript
   with Evidence
        Tobie Langel
      @tobie on twitter
“Evidence in its broadest
sense includes everything
that is used to determine
or demonstrate the truth
of an assertion.”
               –Wikipedia
http://github.com/tobie/evidence
Yet Another Unit Test
     Framework
DOH           Test.Simple/More
JSUnit        TestCase
QUnit         jsUnitTest
Crosscheck    JSTest
J3Unit        jsUnity
JSNUnit       RhinoUnit
YUI Test      FireUnit
JSSpec        ...
unittest.js
JSpec
screw-unit
Why?
Scratching our own itch
Unittesting JavaScript with Evidence
Unittesting JavaScript with Evidence
A lot of unit tests (> 2000 assertions).
A lot of unit tests (> 2000 assertions).
Complex cases (async, cross-browser).
A lot of unit tests (> 2000 assertions).
Complex cases (async, cross-browser).
Uses a framework originally created in
2005 for script.aculo.us...
A lot of unit tests (> 2000 assertions).
Complex cases (async, cross-browser).
Uses a framework originally created in
2005 for script.aculo.us...
...with a dependency on Prototype.
#1
Framework agnostic
#2
Environment agnostic
#2
Environment agnostic
 (Attempts to run in as many different
      environments as possible.)
#3
Self-contained
#3
   Self-contained
(Doesn’t pollute the global scope.)
#4
Reliable
#5
Built with async in mind
#6
Easier to automate
#7
Promotes better testing
“I recommend that
developers spend
25-50% of their time
developing tests.”
              –Kent Beck
Drawing by Witold Riedel (http://www.witoldriedel.com)
Kent Beck’s original
   white paper


                Drawing by Witold Riedel (http://www.witoldriedel.com)
Kent Beck’s original
   white paper
    http://tr.im/kentbeck



                            Drawing by Witold Riedel (http://www.witoldriedel.com)
JUnit
JUnit
Kent Beck &
Erich Gamma
Test::Unit
MiniTest
unittest / PyUnit
YUI.Test
... and of course, Kent
Beck’s original Smalltalk
      implementation.
(
Evidence
!=
RSpec
BDD
Why?
I don’t like BDD
&
JavaScript’s awful at
    writing DSLs
unless
you’re using it’s bad
       parts!
function callAssertions(fn) {
 var rgxp = /^[^{]*{((.*n*)*)}/m;
 fn = fn.toString();
 fn = fn.match(rgxp)[1];

    eval('with (dsl) { 
     with (context) { 
     with (matchers) { ' +
     fn + ' }}}');
}
Function decompilation
function callAssertions(fn) {
                      (Not part of any standard.)
 var rgxp = /^[^{]*{((.*n*)*)}/m;
 fn = fn.toString();
 fn = fn.match(rgxp)[1];

    eval('with (dsl) { 
     with (context) { 
     with (matchers) { ' +
     fn + ' }}}');
}
function callAssertions(fn) {
 var rgxp = /^[^{]*{((.*n*)*)}/m;
 fn = fn.toString();
 fn = fn.match(rgxp)[1];

    eval('with (dsl) { 
     with (context) { 
     with (matchers) { ' +
     fn + ' }}}'); Nested with statements
}                      (Not ES5 strict compliant.)
function callAssertions(fn) {
 var rgxp = /^[^{]*{((.*n*)*)}/m;
 fn = fn.toString();
                          eval
 fn = fn.match(rgxp)[1];
           (Not supported in some environments.)



    eval('with (dsl) { 
     with (context) { 
     with (matchers) { ' +
     fn + ' }}}');
}
function callAssertions(fn) {
 var rgxp = /^[^{]*{((.*n*)*)}/m;
 fn = fn.toString();
 fn = fn.match(rgxp)[1];

    eval('with (dsl) { 
     with (context) { 
     with (matchers) { ' +
     fn + ' }}}');
}
)
xUnit
xUnit
xUnit
TestCase
xUnit
TestCase
TestSuite
xUnit
TestCase
TestSuite
TestRunner
xUnit
TestCase
TestSuite
TestRunner
TestResult
Anatomy of a TestCase
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },

 testFirst: function() {
   this.assertEqual('foo', _.first(this.array));
   this.assertUndefined(_.first([]));
 },

  testLast: function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
});
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },
                              “Subclass” TestCase
 testFirst: function() {
   this.assertEqual('foo', _.first(this.array));
   this.assertUndefined(_.first([]));
 },

  testLast: function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
});
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },
                                           Give it a name.
 testFirst: function() {
   this.assertEqual('foo', _.first(this.array));
   this.assertUndefined(_.first([]));
 },

  testLast: function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
});
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },

 testFirst: function() {
Add fixtures to your _.first(this.array));
   this.assertEqual('foo',
   setUp method.
   this.assertUndefined(_.first([]));
 },

  testLast: function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
});
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
Prefix setUp:testcases {with
      your function()
        this.array = ['foo', 'bar', 'baz'];
      },    test.
       testFirst: function() {
         this.assertEqual('foo', _.first(this.array));
         this.assertUndefined(_.first([]));
       },

       testLast: function() {
         this.assertEqual('bar', _.last(this.array),
          'Failed to grab the last element of the array.');
         this.assertUndefined(_.last([]));
       }
     });
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },

 testFirst: function() {
   this.assertEqual('foo', _.first(this.array));
   this.assertUndefined(_.first([]));
 },

  testLast: your assertions.
    Write function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
});
var ArrayTest = Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },

 testFirst: function() {
   this.assertEqual('foo', _.first(this.array));
   this.assertUndefined(_.first([]));
 },

  testLast: function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
                             Optionally add
});                         meaningful error
                               messages.
Built-in async handling.
Evidence.TestCase.extend('AjaxTest', {
  testAjaxRequest: function(testcase) {
    testcase.pause();
    new Ajax.Request('/some/url', {
      onComplete: function(response) {
        testcase.resume(function() {
          this.assert(response);
        });
      }
    });
  }
});
Evidence.TestCase.extend('AjaxTest', {
  testAjaxRequest: function(testcase) {
    testcase.pause();
    new Ajax.Request('/some/url', { instance
                              TestCase
      onComplete: function(response) {
                           conveniently passed as a
        testcase.resume(function() {
          this.assert(response); first argument.
        });
      }
    });
  }
});
Evidence.TestCase.extend('AjaxTest', {
    testAjaxRequest: function(testcase) {
No need to bind this or
      testcase.pause();
create your own closure.
      new Ajax.Request('/some/url', {
        onComplete: function(response) {
          testcase.resume(function() {
            this.assert(response);
          });
        }
      });
    }
  });
Evidence.TestCase.extend('AjaxTest', {
  testAjaxRequest: function(testcase) {
    testcase.pause();
    new Ajax.Request('/some/url', {
      onComplete: function(response) {
        testcase.resume(function() {
          this.assert(response);
        });
      Bound to the original
      }
    });      scope so:
  } this === testcase
});
The TestSuite
function getTestCaseNames(testcaseClass) {
 var results = [];

    for (var property in testcaseClass.prototype) {
      if (property.indexOf('test') === 0) {
        results.push(property);
      }
    }

    return results.sort();
}
Grab all the methods
function getTestCaseNames(testcaseClass) {
 var results = [];        starting with test.

    for (var property in testcaseClass.prototype) {
      if (property.indexOf('test') === 0) {
        results.push(property);
      }
    }

    return results.sort();
}
function getTestCaseNames(testcaseClass) {
 var results = [];

    for (var property in testcaseClass.prototype) {
      if (property.indexOf('test') === 0) {
        results.push(property);
      }                              Sort the results.
    }

    return results.sort();
}
function loadTestsFromTestCase(testcaseClass) {
 var suite = new TestSuite(testcaseClass.displayName),
    methodNames = getTestCaseNames(testcaseClass);

    for (var i = 0; i < methodNames.length; i++) {
      suite.push(new testcaseClass(methodNames[i]));
    }

    return suite;
}
Create a new suite.

function loadTestsFromTestCase(testcaseClass) {
 var suite = new TestSuite(testcaseClass.displayName),
    methodNames = getTestCaseNames(testcaseClass);

    for (var i = 0; i < methodNames.length; i++) {
      suite.push(new testcaseClass(methodNames[i]));
    }

    return suite;
}
Give it a name.

function loadTestsFromTestCase(testcaseClass) {
 var suite = new TestSuite(testcaseClass.displayName),
    methodNames = getTestCaseNames(testcaseClass);

    for (var i = 0; i < methodNames.length; i++) {
      suite.push(new testcaseClass(methodNames[i]));
    }

    return suite;
}
function loadTestsFromTestCase(testcaseClass) {
 var suite = new TestSuite(testcaseClass.displayName),
    methodNames = getTestCaseNames(testcaseClass);

    for (var i = 0; i < methodNames.length; i++) {
      suite.push(new testcaseClass(methodNames[i]));
    }
                      Grab the testcase names.

    return suite;
}
function loadTestsFromTestCase(testcaseClass) {
 var suite = new TestSuite(testcaseClass.displayName),
    methodNames = getTestCaseNames(testcaseClass);

    for (var i = 0; i < methodNames.length; i++) {
      suite.push(new testcaseClass(methodNames[i]));
    }

    return suite;   Create a new instance of
}
                       the testcase class.
function loadTestsFromTestCase(testcaseClass) {
  var suite = new TestSuite(testcaseClass.displayName),
     methodNames = getTestCaseNames(testcaseClass);

  for (var i = 0; i < methodNames.length; i++) {
    suite.push(new testcaseClass(methodNames[i]));
  }

  return suite;
Add it to your suite.
 }
Benefits
Custom assertions
Evidence.TestCase.extend('ElementTest', {
 setUp: function() {
   this.element = document.createElement('div');
 },

 testElementExtend: function() {
   var extended = Element.extend(this.element);
   this.assertEqual(this.element, extended);
   this.assertRespondsTo('show', extended);
   // ...
 },

  testElementClone: function() {
    var cloned = Element.clone(this.element);
    this.assert(cloned.show); // is extended
    // ...
  }
});
Evidence.TestCase.extend('ElementTest', {
 setUp: function() {
   this.element = document.createElement('div');
 },
                                   What!?
 testElementExtend: function() {
   var extended = Element.extend(this.element);
   this.assertEqual(this.element, extended);
   this.assertRespondsTo('show', extended);
   // ...
 },

  testElementClone: function() {
    var cloned = Element.clone(this.element);
    this.assert(cloned.show); // is extended
    // ...
  }
});
Evidence.TestCase.extend('ElementTest', {
 setUp: function() {
   this.element = document.createElement('div');
 },

 testElementExtend: function() {
   var extended = Element.extend(this.element);
   this.assertEqual(this.element, extended);
   this.assertRespondsTo('show', extended);
   // ...
 },                           Again?!
  testElementClone: function() {
    var cloned = Element.clone(this.element);
    this.assert(cloned.show); // is extended
    // ...
  }
});
Evidence.TestCase.extend('ElementTest', {
 setUp: function() {
   this.element = document.createElement('div');
 },

 testElementExtend: function() {
   var extended = Element.extend(this.element);
   this.assertEqual(this.element, extended);
   this.assertRespondsTo('show', extended);
   // ...
 },

  testElementClone: function() {
    var cloned = Element.clone(this.element);
    this.assert(cloned.show); // is extended
    // ...
  OK, now I get it.
  }
});
!= DRY
Evidence.TestCase.extend('ElementTest', {
 //...

 assertElementExtended: function(element, message) {
   this._assertExpression(
     typeof element.show === 'function',
     message || 'Element is not extended.',
     'Expected %o to be extended.', element
   );
 },

 testElementExtend: function() {
   var extended = Element.extend(this.element);
   this.assertElementExtended(extended);
 },

 testElementClone: function() {
   var cloned = Element.clone(this.element);
   this.assertElementExtended(cloned);
 }
Evidence.TestCase.extend('ElementTest', {
 //...

 assertElementExtended: function(element, message) {
   this._assertExpression(
     typeof element.show === 'function',
                                      !
     message || 'Element is not extended.',
     'Expected %o to be extended.', element
   );
                         Syntax still subject to
 },                              change!
 testElementExtend: function() {
   var extended = Element.extend(this.element);
   this.assertElementExtended(extended);
 },

 testElementClone: function() {
   var cloned = Element.clone(this.element);
   this.assertElementExtended(cloned);
 }
Instance methods
function getInnerHTML(element) {
  var html = element.innerHTML.toString();
  return html.toLowerCase().gsub(/[rnt]/, '');
}

Evidence.TestCase.extend('ElementTest', {
 // ...

  testElementInsert: function() {
    var html = '<p>a paragraph</p>';
    Element.insert(this.element, html);
    this.assertEqual(html, getInnerHTML(this.element));
  }
});
function getInnerHTML(element) {
  var html = element.innerHTML.toString();
  return html.toLowerCase().gsub(/[rnt]/, '');
}
                                 Global scope pollution.
Evidence.TestCase.extend('ElementTest', {
 // ...

  testElementInsert: function() {
    var html = '<p>a paragraph</p>';
    Element.insert(this.element, html);
    this.assertEqual(html, getInnerHTML(this.element));
  }
});
function getInnerHTML(element) {
  var html = element.innerHTML.toString();
  return html.toLowerCase().gsub(/[rnt]/, '');
}         Obscure syntax.
        What’s wrong with
Evidence.TestCase.extend('ElementTest', {
 //this.element.innerHTML?
    ...

  testElementInsert: function() {
    var html = '<p>a paragraph</p>';
    Element.insert(this.element, html);
    this.assertEqual(html, getInnerHTML(this.element));
  }
});
Evidence.TestCase.extend('ElementTest', {
 // ...

 testElementInsert: function() {
   var html = '<p>a paragraph</p>';
   Element.insert(this.element, html);
   this.assertEqual(html, this.getElementHTML());
 },

  getElementHTML: function() {
    var html = this.element.innerHTML.toString();
    return html.toLowerCase().gsub(/[rnt]/, '');
  }
});
Evidence.TestCase.extend('ElementTest', {
 // ...
              Instance method!
 testElementInsert: function() {
   var html = '<p>a paragraph</p>';
   Element.insert(this.element, html);
   this.assertEqual(html, this.getElementHTML());
 },

  getElementHTML: function() {
    var html = this.element.innerHTML.toString();
    return html.toLowerCase().gsub(/[rnt]/, '');
  }
});
Evidence.TestCase.extend('ElementTest', {
 // ...

 testElementInsert: function() {
   var html = '<p>a paragraph</p>';
   Element.insert(this.element, html);
   this.assertEqual(html, this.getElementHTML());
 },

  getElementHTML: function() {
                 Would benefit from a
    var html = this.element.innerHTML.toString();
    return html.toLowerCase().gsub(/[rnt]/, '');
                custom assertion too.
  }
});
The TestRunner
var suite = loadTestsFromTestCase(ArrayTest);
var runner = new Evidence.Runner();
var results = runner.run(suite);
The TestResult
Logs to the console.




The TestResult
Logs to the console.




The TestResult

          Prints to STDOUT.
Displays the results in    Logs to the console.
     a web page.



               The TestResult

                          Prints to STDOUT.
Displays the results in    Logs to the console.
     a web page.



               The TestResult
    Sends the data back
  to the server in JSON
                          Prints to STDOUT.
        (or XML).
The Magic
All of this is good to know,
but it’s tedious.
So...
Evidence handles it for you!
Evidence.TestCase.extend('ArrayTest', {
 setUp: function() {
   this.array = ['foo', 'bar', 'baz'];
 },

 testFirst: function() {
   this.assertEqual('foo', _.first(this.array));
   this.assertUndefined(_.first([]));
 },

  testLast: function() {
    this.assertEqual('bar', _.last(this.array),
     'Failed to grab the last element of the array.');
    this.assertUndefined(_.last([]));
  }
});
It’s all you need!
?
           @tobie on twitter

http://github.com/tobie/evidence
 (Soon hopefully on http://evidencejs.org)

More Related Content

What's hot (20)

PDF
Rxjs vienna
Christoffer Noring
 
PDF
0003 es5 핵심 정리
욱래 김
 
PDF
Testing your javascript code with jasmine
Rubyc Slides
 
PDF
The Ring programming language version 1.7 book - Part 12 of 196
Mahmoud Samir Fayed
 
DOCX
Registro de venta
lupe ga
 
PDF
Unit-Testing Bad-Practices by Example
Benjamin Eberlei
 
PDF
Unittesting Bad-Practices by Example
Benjamin Eberlei
 
PDF
Google guava
t fnico
 
PDF
The Ring programming language version 1.5.1 book - Part 75 of 180
Mahmoud Samir Fayed
 
PDF
Bad test, good test
Seb Rose
 
PDF
The Ring programming language version 1.7 book - Part 16 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 15 of 189
Mahmoud Samir Fayed
 
PDF
Transducers in JavaScript
Pavel Forkert
 
PDF
Google Guava for cleaner code
Mite Mitreski
 
PDF
The Ring programming language version 1.6 book - Part 11 of 189
Mahmoud Samir Fayed
 
ODT
Logic Equations Resolver J Script
Roman Agaev
 
PPTX
Rxjs ngvikings
Christoffer Noring
 
PPTX
Java script advance-auroskills (2)
BoneyGawande
 
PDF
The Ring programming language version 1.6 book - Part 34 of 189
Mahmoud Samir Fayed
 
PDF
Redux Sagas - React Alicante
Ignacio Martín
 
Rxjs vienna
Christoffer Noring
 
0003 es5 핵심 정리
욱래 김
 
Testing your javascript code with jasmine
Rubyc Slides
 
The Ring programming language version 1.7 book - Part 12 of 196
Mahmoud Samir Fayed
 
Registro de venta
lupe ga
 
Unit-Testing Bad-Practices by Example
Benjamin Eberlei
 
Unittesting Bad-Practices by Example
Benjamin Eberlei
 
Google guava
t fnico
 
The Ring programming language version 1.5.1 book - Part 75 of 180
Mahmoud Samir Fayed
 
Bad test, good test
Seb Rose
 
The Ring programming language version 1.7 book - Part 16 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 15 of 189
Mahmoud Samir Fayed
 
Transducers in JavaScript
Pavel Forkert
 
Google Guava for cleaner code
Mite Mitreski
 
The Ring programming language version 1.6 book - Part 11 of 189
Mahmoud Samir Fayed
 
Logic Equations Resolver J Script
Roman Agaev
 
Rxjs ngvikings
Christoffer Noring
 
Java script advance-auroskills (2)
BoneyGawande
 
The Ring programming language version 1.6 book - Part 34 of 189
Mahmoud Samir Fayed
 
Redux Sagas - React Alicante
Ignacio Martín
 

Similar to Unittesting JavaScript with Evidence (20)

PDF
Understanding JavaScript Testing
jeresig
 
PDF
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
PDF
Stop Making Excuses and Start Testing Your JavaScript
Ryan Anklam
 
PDF
JavaScript Unit Testing with Jasmine
Raimonds Simanovskis
 
PPTX
Introduction to Software Testing
Sergio Arroyo
 
PPTX
Qunit Java script Un
akanksha arora
 
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
PDF
04 Advanced Javascript
crgwbr
 
PDF
Writing testable js [by Ted Piotrowski]
JavaScript Meetup HCMC
 
PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
PPTX
Writing Good Tests
Matteo Baglini
 
PPTX
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
PDF
Test driven node.js
Jay Harris
 
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
PPTX
JS Frameworks Day April,26 of 2014
DA-14
 
PDF
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
PPTX
Understanding JavaScript Testing
Kissy Team
 
PPTX
Junit 4.0
pallavikhandekar212
 
PDF
Unit Testing JavaScript Applications
Ynon Perek
 
PDF
#codemotion2016: Everything you should know about testing to go with @pedro_g...
Sergio Arroyo
 
Understanding JavaScript Testing
jeresig
 
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
Stop Making Excuses and Start Testing Your JavaScript
Ryan Anklam
 
JavaScript Unit Testing with Jasmine
Raimonds Simanovskis
 
Introduction to Software Testing
Sergio Arroyo
 
Qunit Java script Un
akanksha arora
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
04 Advanced Javascript
crgwbr
 
Writing testable js [by Ted Piotrowski]
JavaScript Meetup HCMC
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
Writing Good Tests
Matteo Baglini
 
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
Test driven node.js
Jay Harris
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
JS Frameworks Day April,26 of 2014
DA-14
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
Understanding JavaScript Testing
Kissy Team
 
Unit Testing JavaScript Applications
Ynon Perek
 
#codemotion2016: Everything you should know about testing to go with @pedro_g...
Sergio Arroyo
 
Ad

Recently uploaded (20)

PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
July Patch Tuesday
Ivanti
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Ad

Unittesting JavaScript with Evidence

  • 1. Unittesting JavaScript with Evidence Tobie Langel @tobie on twitter
  • 2. “Evidence in its broadest sense includes everything that is used to determine or demonstrate the truth of an assertion.” –Wikipedia
  • 4. Yet Another Unit Test Framework
  • 5. DOH Test.Simple/More JSUnit TestCase QUnit jsUnitTest Crosscheck JSTest J3Unit jsUnity JSNUnit RhinoUnit YUI Test FireUnit JSSpec ... unittest.js JSpec screw-unit
  • 10. A lot of unit tests (> 2000 assertions).
  • 11. A lot of unit tests (> 2000 assertions). Complex cases (async, cross-browser).
  • 12. A lot of unit tests (> 2000 assertions). Complex cases (async, cross-browser). Uses a framework originally created in 2005 for script.aculo.us...
  • 13. A lot of unit tests (> 2000 assertions). Complex cases (async, cross-browser). Uses a framework originally created in 2005 for script.aculo.us... ...with a dependency on Prototype.
  • 16. #2 Environment agnostic (Attempts to run in as many different environments as possible.)
  • 18. #3 Self-contained (Doesn’t pollute the global scope.)
  • 23. “I recommend that developers spend 25-50% of their time developing tests.” –Kent Beck
  • 24. Drawing by Witold Riedel (http://www.witoldriedel.com)
  • 25. Kent Beck’s original white paper Drawing by Witold Riedel (http://www.witoldriedel.com)
  • 26. Kent Beck’s original white paper http://tr.im/kentbeck Drawing by Witold Riedel (http://www.witoldriedel.com)
  • 27. JUnit
  • 33. ... and of course, Kent Beck’s original Smalltalk implementation.
  • 34. (
  • 36. !=
  • 37. RSpec
  • 38. BDD
  • 39. Why?
  • 41. &
  • 42. JavaScript’s awful at writing DSLs
  • 44. you’re using it’s bad parts!
  • 45. function callAssertions(fn) { var rgxp = /^[^{]*{((.*n*)*)}/m; fn = fn.toString(); fn = fn.match(rgxp)[1]; eval('with (dsl) { with (context) { with (matchers) { ' + fn + ' }}}'); }
  • 46. Function decompilation function callAssertions(fn) { (Not part of any standard.) var rgxp = /^[^{]*{((.*n*)*)}/m; fn = fn.toString(); fn = fn.match(rgxp)[1]; eval('with (dsl) { with (context) { with (matchers) { ' + fn + ' }}}'); }
  • 47. function callAssertions(fn) { var rgxp = /^[^{]*{((.*n*)*)}/m; fn = fn.toString(); fn = fn.match(rgxp)[1]; eval('with (dsl) { with (context) { with (matchers) { ' + fn + ' }}}'); Nested with statements } (Not ES5 strict compliant.)
  • 48. function callAssertions(fn) { var rgxp = /^[^{]*{((.*n*)*)}/m; fn = fn.toString(); eval fn = fn.match(rgxp)[1]; (Not supported in some environments.) eval('with (dsl) { with (context) { with (matchers) { ' + fn + ' }}}'); }
  • 49. function callAssertions(fn) { var rgxp = /^[^{]*{((.*n*)*)}/m; fn = fn.toString(); fn = fn.match(rgxp)[1]; eval('with (dsl) { with (context) { with (matchers) { ' + fn + ' }}}'); }
  • 50. )
  • 51. xUnit
  • 52. xUnit
  • 57. Anatomy of a TestCase
  • 58. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 59. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, “Subclass” TestCase testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 60. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, Give it a name. testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 61. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, testFirst: function() { Add fixtures to your _.first(this.array)); this.assertEqual('foo', setUp method. this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 62. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { Prefix setUp:testcases {with your function() this.array = ['foo', 'bar', 'baz']; }, test. testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 63. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: your assertions. Write function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 64. var ArrayTest = Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } Optionally add }); meaningful error messages.
  • 66. Evidence.TestCase.extend('AjaxTest', { testAjaxRequest: function(testcase) { testcase.pause(); new Ajax.Request('/some/url', { onComplete: function(response) { testcase.resume(function() { this.assert(response); }); } }); } });
  • 67. Evidence.TestCase.extend('AjaxTest', { testAjaxRequest: function(testcase) { testcase.pause(); new Ajax.Request('/some/url', { instance TestCase onComplete: function(response) { conveniently passed as a testcase.resume(function() { this.assert(response); first argument. }); } }); } });
  • 68. Evidence.TestCase.extend('AjaxTest', { testAjaxRequest: function(testcase) { No need to bind this or testcase.pause(); create your own closure. new Ajax.Request('/some/url', { onComplete: function(response) { testcase.resume(function() { this.assert(response); }); } }); } });
  • 69. Evidence.TestCase.extend('AjaxTest', { testAjaxRequest: function(testcase) { testcase.pause(); new Ajax.Request('/some/url', { onComplete: function(response) { testcase.resume(function() { this.assert(response); }); Bound to the original } }); scope so: } this === testcase });
  • 71. function getTestCaseNames(testcaseClass) { var results = []; for (var property in testcaseClass.prototype) { if (property.indexOf('test') === 0) { results.push(property); } } return results.sort(); }
  • 72. Grab all the methods function getTestCaseNames(testcaseClass) { var results = []; starting with test. for (var property in testcaseClass.prototype) { if (property.indexOf('test') === 0) { results.push(property); } } return results.sort(); }
  • 73. function getTestCaseNames(testcaseClass) { var results = []; for (var property in testcaseClass.prototype) { if (property.indexOf('test') === 0) { results.push(property); } Sort the results. } return results.sort(); }
  • 74. function loadTestsFromTestCase(testcaseClass) { var suite = new TestSuite(testcaseClass.displayName), methodNames = getTestCaseNames(testcaseClass); for (var i = 0; i < methodNames.length; i++) { suite.push(new testcaseClass(methodNames[i])); } return suite; }
  • 75. Create a new suite. function loadTestsFromTestCase(testcaseClass) { var suite = new TestSuite(testcaseClass.displayName), methodNames = getTestCaseNames(testcaseClass); for (var i = 0; i < methodNames.length; i++) { suite.push(new testcaseClass(methodNames[i])); } return suite; }
  • 76. Give it a name. function loadTestsFromTestCase(testcaseClass) { var suite = new TestSuite(testcaseClass.displayName), methodNames = getTestCaseNames(testcaseClass); for (var i = 0; i < methodNames.length; i++) { suite.push(new testcaseClass(methodNames[i])); } return suite; }
  • 77. function loadTestsFromTestCase(testcaseClass) { var suite = new TestSuite(testcaseClass.displayName), methodNames = getTestCaseNames(testcaseClass); for (var i = 0; i < methodNames.length; i++) { suite.push(new testcaseClass(methodNames[i])); } Grab the testcase names. return suite; }
  • 78. function loadTestsFromTestCase(testcaseClass) { var suite = new TestSuite(testcaseClass.displayName), methodNames = getTestCaseNames(testcaseClass); for (var i = 0; i < methodNames.length; i++) { suite.push(new testcaseClass(methodNames[i])); } return suite; Create a new instance of } the testcase class.
  • 79. function loadTestsFromTestCase(testcaseClass) { var suite = new TestSuite(testcaseClass.displayName), methodNames = getTestCaseNames(testcaseClass); for (var i = 0; i < methodNames.length; i++) { suite.push(new testcaseClass(methodNames[i])); } return suite; Add it to your suite. }
  • 82. Evidence.TestCase.extend('ElementTest', { setUp: function() { this.element = document.createElement('div'); }, testElementExtend: function() { var extended = Element.extend(this.element); this.assertEqual(this.element, extended); this.assertRespondsTo('show', extended); // ... }, testElementClone: function() { var cloned = Element.clone(this.element); this.assert(cloned.show); // is extended // ... } });
  • 83. Evidence.TestCase.extend('ElementTest', { setUp: function() { this.element = document.createElement('div'); }, What!? testElementExtend: function() { var extended = Element.extend(this.element); this.assertEqual(this.element, extended); this.assertRespondsTo('show', extended); // ... }, testElementClone: function() { var cloned = Element.clone(this.element); this.assert(cloned.show); // is extended // ... } });
  • 84. Evidence.TestCase.extend('ElementTest', { setUp: function() { this.element = document.createElement('div'); }, testElementExtend: function() { var extended = Element.extend(this.element); this.assertEqual(this.element, extended); this.assertRespondsTo('show', extended); // ... }, Again?! testElementClone: function() { var cloned = Element.clone(this.element); this.assert(cloned.show); // is extended // ... } });
  • 85. Evidence.TestCase.extend('ElementTest', { setUp: function() { this.element = document.createElement('div'); }, testElementExtend: function() { var extended = Element.extend(this.element); this.assertEqual(this.element, extended); this.assertRespondsTo('show', extended); // ... }, testElementClone: function() { var cloned = Element.clone(this.element); this.assert(cloned.show); // is extended // ... OK, now I get it. } });
  • 87. Evidence.TestCase.extend('ElementTest', { //... assertElementExtended: function(element, message) { this._assertExpression( typeof element.show === 'function', message || 'Element is not extended.', 'Expected %o to be extended.', element ); }, testElementExtend: function() { var extended = Element.extend(this.element); this.assertElementExtended(extended); }, testElementClone: function() { var cloned = Element.clone(this.element); this.assertElementExtended(cloned); }
  • 88. Evidence.TestCase.extend('ElementTest', { //... assertElementExtended: function(element, message) { this._assertExpression( typeof element.show === 'function', ! message || 'Element is not extended.', 'Expected %o to be extended.', element ); Syntax still subject to }, change! testElementExtend: function() { var extended = Element.extend(this.element); this.assertElementExtended(extended); }, testElementClone: function() { var cloned = Element.clone(this.element); this.assertElementExtended(cloned); }
  • 90. function getInnerHTML(element) { var html = element.innerHTML.toString(); return html.toLowerCase().gsub(/[rnt]/, ''); } Evidence.TestCase.extend('ElementTest', { // ... testElementInsert: function() { var html = '<p>a paragraph</p>'; Element.insert(this.element, html); this.assertEqual(html, getInnerHTML(this.element)); } });
  • 91. function getInnerHTML(element) { var html = element.innerHTML.toString(); return html.toLowerCase().gsub(/[rnt]/, ''); } Global scope pollution. Evidence.TestCase.extend('ElementTest', { // ... testElementInsert: function() { var html = '<p>a paragraph</p>'; Element.insert(this.element, html); this.assertEqual(html, getInnerHTML(this.element)); } });
  • 92. function getInnerHTML(element) { var html = element.innerHTML.toString(); return html.toLowerCase().gsub(/[rnt]/, ''); } Obscure syntax. What’s wrong with Evidence.TestCase.extend('ElementTest', { //this.element.innerHTML? ... testElementInsert: function() { var html = '<p>a paragraph</p>'; Element.insert(this.element, html); this.assertEqual(html, getInnerHTML(this.element)); } });
  • 93. Evidence.TestCase.extend('ElementTest', { // ... testElementInsert: function() { var html = '<p>a paragraph</p>'; Element.insert(this.element, html); this.assertEqual(html, this.getElementHTML()); }, getElementHTML: function() { var html = this.element.innerHTML.toString(); return html.toLowerCase().gsub(/[rnt]/, ''); } });
  • 94. Evidence.TestCase.extend('ElementTest', { // ... Instance method! testElementInsert: function() { var html = '<p>a paragraph</p>'; Element.insert(this.element, html); this.assertEqual(html, this.getElementHTML()); }, getElementHTML: function() { var html = this.element.innerHTML.toString(); return html.toLowerCase().gsub(/[rnt]/, ''); } });
  • 95. Evidence.TestCase.extend('ElementTest', { // ... testElementInsert: function() { var html = '<p>a paragraph</p>'; Element.insert(this.element, html); this.assertEqual(html, this.getElementHTML()); }, getElementHTML: function() { Would benefit from a var html = this.element.innerHTML.toString(); return html.toLowerCase().gsub(/[rnt]/, ''); custom assertion too. } });
  • 97. var suite = loadTestsFromTestCase(ArrayTest); var runner = new Evidence.Runner(); var results = runner.run(suite);
  • 99. Logs to the console. The TestResult
  • 100. Logs to the console. The TestResult Prints to STDOUT.
  • 101. Displays the results in Logs to the console. a web page. The TestResult Prints to STDOUT.
  • 102. Displays the results in Logs to the console. a web page. The TestResult Sends the data back to the server in JSON Prints to STDOUT. (or XML).
  • 104. All of this is good to know,
  • 106. So...
  • 107. Evidence handles it for you!
  • 108. Evidence.TestCase.extend('ArrayTest', { setUp: function() { this.array = ['foo', 'bar', 'baz']; }, testFirst: function() { this.assertEqual('foo', _.first(this.array)); this.assertUndefined(_.first([])); }, testLast: function() { this.assertEqual('bar', _.last(this.array), 'Failed to grab the last element of the array.'); this.assertUndefined(_.last([])); } });
  • 109. It’s all you need!
  • 110. ? @tobie on twitter http://github.com/tobie/evidence (Soon hopefully on http://evidencejs.org)