SlideShare a Scribd company logo
1 of 81
Download to read offline
Testing, Performance
Analysis, and jQuery 1.4
                   John Resig
  http://ejohn.org/ - http://twitter.com/jeresig
Why Test JavaScript?
✦   Cross-browser issues.
✦   The possibility for causing an unforeseen
    problem is simply too great.
JavaScript Testing Isn’t
The Same As Desktop
or Server-Side Testing
What should I use?
Looong Tail

300


225


150


 75


  0


               Other
Basic Components
✦   Writing and understanding a JavaScript
    test suite is easy.
✦   Test Suite
    ✦ Tests
      ✦ Assertions
    ✦ Async Tests

✦   Test Runner
Assertions
   <html> 
   <head> 
     <title>Test Suite</title> 
     <script> 
     function assert( value, desc ) { 
       var li = document.createElement("li"); 
       li.className = value ? "pass" : "fail"; 
       li.appendChild( document.createTextNode( desc ) ); 
       document.getElementById("results").appendChild( li ); 
     } 
                                    
     window.onload = function(){ 
       assert( true, "The test suite is running." ); 
     }; 
     </script> 
     <style> 
       #results li.pass { color: green; } 
       #results li.fail { color: red; } 
     </style> 
   </head> 
   <body> 
     <ul id="results"></ul> 
   </body> 
   </html> 
Tests
      test("A test.", function(){ 
        assert( true, "First assertion completed" ); 
        assert( true, "Second assertion completed" ); 
        assert( true, "Third assertion completed" ); 
      }); 
                                  
      test("Another test.", function(){ 
        assert( true, "First test completed" ); 
        assert( false, "Second test failed" ); 
        assert( true, "Third assertion completed" ); 
      }); 
Tests
    var results; 
                                  
    function assert( value, desc ) { 
      var li = document.createElement("li"); 
      li.className = value ? "pass" : "fail"; 
      li.appendChild( document.createTextNode( desc ) ); 
      results.appendChild( li ); 
      if ( !value ) { 
        li.parentNode.parentNode.className = "fail"; 
      } 
      return li; 
    } 
                                  
    function test(name, fn){ 
      results = document.getElementById("results"); 
      results = assert( true, name ).appendChild( 
        document.createElement("ul") ); 
      fn(); 
    } 
Async Tests
    test("Async Test #1", function(){ 
      pause(); 
      setTimeout(function(){ 
        assert( true, "First test completed" ); 
        resume(); 
      }, 1000); 
    }); 
                            
    test("Async Test #2", function(){ 
      pause(); 
      setTimeout(function(){ 
        assert( true, "Second test completed" ); 
        resume(); 
      }, 1000); 
    }); 
Async Tests
      (function(){ 
        var queue = [], paused = false; 
                             
        this.test = function(fn){ 
          queue.push( fn ); 
          runTest(); 
        }; 
                             
        this.pause = function(){ 
          paused = true; 
        }; 
                             
        this.resume = function(){ 
          paused = false; 
          setTimeout(runTest, 1); 
        }; 
                             
        function runTest(){ 
          if ( !paused && queue.length ) { 
            queue.shift()(); 
            if ( !paused ) { 
              resume(); 
            } 
          } 
        } 
      })(); 
People don’t test. :-(

900


675


450


225


  0


                         None
Popular Test Frameworks

300


225


150


 75


  0
      QUnit   JSUnit   Selenium YUITest   FireUnit Screw.Unit JSSpec
Unit Testing
✦   Break code into logical chucks for testing.
✦   Focus on one method at a time.
✦   Good for testing APIs.
✦   Popular Frameworks:
    ✦ QUnit
    ✦ JSUnit
    ✦ YUITest
    ✦ FireUnit
JSUnit
✦   One of the oldest JavaScript testing
    frameworks.
✦   A port of JUnit to JavaScript, circa 2001.
✦   Code feels very 2001 (frames!)
✦   http://www.jsunit.net/
JSUnit
        function coreSuite() { 
            var result = new top.jsUnitTestSuite(); 
            result.addTestPage("tests/jsUnitAssertionTests.html"); 
            result.addTestPage("tests/jsUnitSetUpTearDownTests.html"); 
            result.addTestPage("tests/jsUnitRestoredHTMLDivTests.html"); 
            result.addTestPage("tests/jsUnitFrameworkUtilityTests.html"); 
            result.addTestPage("tests/jsUnitOnLoadTests.html"); 
            result.addTestPage("tests/jsUnitUtilityTests.html"); 
            return result; 
        } 
                                               
        function serverSuite() { 
            var result = new top.jsUnitTestSuite(); 
            result.addTestPage("tests/server/jsUnitVersionCheckTests.html"); 
            result.addTestPage("tests/server/jsUnitServerAjaxTests.html"); 
            return result; 
        } 
                                               
        function librariesSuite() { 
            var result = new top.jsUnitTestSuite(); 
            result.addTestPage("tests/jsUnitMockTimeoutTest.html"); 
            return result; 
        } 
                                               
        function suite() { 
            var newsuite = new top.jsUnitTestSuite(); 
            newsuite.addTestSuite(coreSuite()); 
            newsuite.addTestSuite(serverSuite()); 
            newsuite.addTestSuite(librariesSuite()); 
            return newsuite; 
        }
JSUnit
   function testAssertNotUndefined() { 
       assertNotUndefined("1 should not be undefined", 1); 
       assertNotUndefined(1); 
   } 
                                              
   function testAssertNaN() { 
       assertNaN("a string should not be a number", "string"); 
       assertNaN("string"); 
   } 
                                              
   function testAssertNotNaN() { 
       assertNotNaN("1 should not be not a number", 1); 
       assertNotNaN(1); 
   } 
                                              
   function testFail() { 
       var excep = null; 
       try { 
           fail("Failure message"); 
       } catch (e) { 
           excep = e; 
       } 
       assertJsUnitException("fail(string) should throw a JsUnitException", excep); 
   } 
                                              
   function testTooFewArguments() { 
       var excep = null; 
       try { 
           assert(); 
       } catch (e1) { 
           excep = e1; 
       } 
       assertNonJsUnitException("Calling an assertion function with too 
          few arguments should throw an exception", excep); 
   }
JSUnit
YUITest (2 & 3)
✦   Testing framework built and developed by
    Yahoo (released Oct 2008).
✦   Completely overhauled to go with YUI v3.
✦   Features:
    ✦ Supports async tests.
    ✦ Has good event simulation.

✦   v2: http://developer.yahoo.com/yui/
    examples/yuitest/
✦   v3: http://developer.yahoo.com/yui/3/test/
YUITest 2
   YAHOO.example.yuitest.ArrayTestCase = new YAHOO.tool.TestCase({ 
      name : "Array Tests", 
                                         
      setUp : function () { 
          this.data = [0,1,2,3,4] 
      }, 
                                         
      tearDown : function () { 
          delete this.data; 
      }, 
                                           
      testPop : function () { 
          var Assert = YAHOO.util.Assert;          
          var value = this.data.pop(); 
                                             
          Assert.areEqual(4, this.data.length); 
          Assert.areEqual(4, value);             
      },         
                                         
      testPush : function () { 
          var Assert = YAHOO.util.Assert; 
                                             
          this.data.push(5); 
                                             
          Assert.areEqual(6, this.data.length); 
          Assert.areEqual(5, this.data[5]);             
      }
  }); 
YUITest 2
YUITest 3
    Y.example.test.DataTestCase = new Y.Test.Case({ 
        name : "Data Tests", 
                                        
        setUp : function () { 
            this.data = { 
                name: "test", 
                year: 2007, 
                beta: true 
            }; 
        }, 
         
        tearDown : function () { 
            delete this.data; 
        }, 
                                        
        testName : function () { 
            var Assert = Y.Assert; 
                                            
            Assert.isObject(this.data); 
            Assert.isString(this.data.name); 
            Assert.areEqual("test", this.data.name);             
        }, 
                                        
        testYear : function () { 
            var Assert = Y.Assert; 
                                            
            Assert.isObject(this.data); 
            Assert.isNumber(this.data.year); 
            Assert.areEqual(2007, this.data.year);             
        }
    }); 
YUITest 3
QUnit
✦   Unit Testing framework built for jQuery.
✦   Features:
    ✦ Supports asynchronous testing.
    ✦ Can break code into modules.
    ✦ Supports test timeouts.
    ✦ No dependencies.
    ✦ Painfully simple.

✦   http://docs.jquery.com/QUnit
QUnit Style
   test("a basic test example", function() { 
     ok( true, "this test is fine" ); 
     var value = "hello"; 
     equals( "hello", value, "We expect value to be hello" ); 
   }); 
                                  
   module("Module A"); 
                                  
   test("first test within module", function() { 
     ok( true, "all pass" ); 
   }); 
                                  
   test("second test within module", function() { 
     ok( true, "all pass" ); 
   }); 
                                  
   module("Module B"); 
                                  
   test("some other test", function() { 
     expect(2); 
     equals( true, false, "failing test" ); 
     equals( true, true, "passing test" ); 
   }); 
QUnit
FireUnit
✦   Unit testing extension for Firebug
✦   fireunit.ok( true, “...” );
✦   http://fireunit.org/
Standardization
✦   CommonJS: A unified cross-platform API
    for JavaScript.
✦   (Including the server-side!)
✦   Working to standardize a simple testing
    API.
✦   http://wiki.commonjs.org/wiki/CommonJS
Server-Side
✦   Ignore the browser! Simulate it on the
    server-side.
✦   Almost always uses Java + Rhino to
    construct a browser.
✦   Some frameworks:
    ✦ Crosscheck
    ✦ Env.js
    ✦ Blueridge
Server-Side
✦   Crosscheck
    ✦ Pure Java, even simulates browser bugs.
    ✦ http://www.thefrontside.net/crosscheck

✦   Env.js
    ✦ Pure JavaScript, focuses on standards
      support.
    ✦ http://github.com/thatcher/env-js/tree/
      master
✦   Blueridge
    ✦ Env.js + Screw.Unit + Rhino
    ✦ http://github.com/relevance/blue-ridge/
Env.js
  $ java -jar build/js.jar
  Rhino 1.6 release 6 2007 06 28
  js> load('build/runtest/env.js');

  js> window.location = 'test/index.html';
  test/index.html

  js> load('dist/jquery.js');

  // Add pretty printing to jQuery objects:
  js> jQuery.fn.toString = DOMNodeList.prototype.toString;

  js> $('span').remove();
  [ <span#å°åŒ—Taibei>, <span#å°åŒ—>, <span#utf8class1>,
    <span#utf8class2>, <span#foo:bar>, <span#test.foo[5]bar> ]

  // Yes - UTF-8 is supported in DOM documents!
  js> $('span')
  [ ]

  js> $('div').append('<span><b>hello!</b> world</span>');
  [ <div#main>, <div#foo> ]

  js> $('span')
  [ <span>, <span> ]

  js> $('span').text()
  hello! worldhello! world
Browser Launching
✦   Automates the process of opening browser
    windows, running tests, and getting results.
✦   Frequently require a specific framework.
✦   Popular frameworks:
    ✦ WebDriver http://code.google.com/p/
      webdriver/ (Java)
    ✦ Waitr http://wtr.rubyforge.org/ (Ruby)
    ✦ JsTestDriver http://code.google.com/p/
      js-test-driver/ (Java)
    ✦ Selenium RC http://seleniumhq.org/
      projects/remote-control/ (Java)
Browser Launching
Distributed
✦   Selenium Grid
    ✦ Push Selenium tests out to many
      machines (that you manage),
      simultaneously.
    ✦ Collect and store the results.
    ✦ http://selenium-grid.seleniumhq.org/

✦   TestSwarm
    ✦ Push tests to a distributed swarm of
      clients.
    ✦ Results viewable on the server.
    ✦ http://testswarm.com/
The Scaling Problem
✦   The Problem:
    ✦ jQuery has 6 test suites
    ✦ Run in 15 browsers
    ✦ (Not even including multiple platforms
      or mobile browsers!)
✦   All need to be run for every commit,
    patch, and plugin.
✦   JavaScript testing doesn’t scale well.
Distributed Testing
✦   Hub server
✦   Clients connect and help run tests
✦   A simple JavaScript client that can be run
    in all browsers
    ✦ Including mobile browsers!


✦ TestSwarm
FF 3.5 FF 3.5 FF 3.5
                                                  IE 6
                                                         IE 6
       FF 3                                                      IE 6
                                           Op 9
FF 3

                                                                 IE 7

                               TestSwarm
                                                                  IE 7




              Test Suite        Test Suite          Test Suite
TestSwarm.com
✦   Incentives for top testers (t-shirts, books)
✦   Will be opening for alpha testing very soon
✦   Help your favorite JavaScript library
    become better tested!
✦   http://testswarm.com
Accurately Measuring
     JavaScript
Major Cases
✦   Same Code, Different Platforms
    ✦ Compare V8 vs. SpiderMonkey vs.
      JavaScriptCore
✦   Different Code, Same Platform
    ✦ Compare CSS Selector Engines
    ✦ A/B testing a piece of code
Same Code, Different Platform
✦   A number of suites analyzing JS perf:
    ✦ SunSpider (from WebKit)
    ✦ V8 Benchmark (from V8/Chrome)
    ✦ Dromaeo (from Mozilla)

✦   Statistical accuracy and reproducibility is
    paramount.
SunSpider
✦   All tests were highly balanced.
✦   Provide some level of statistical accuracy.
    ✦ +/- 5ms (for example)

✦   Tests are run by loading an iframe with the
    test 5 times.
    ✦ getTime() is run before/after each test.

✦   Entire suite must be trashed in order to
    upgrade/fix a test.
Error Rate?
✦   How do we get it? What does it mean?
✦   It’s how confident we are that we arrived
    at the result we want in the number of
    runs that we’ve done.
Normal Distribution
✦   First: Assume that the results are coming
    back in a normal distribution.
✦   The “bell curve”
Confidence
✦   Next: We need a confidence level.
✦   T-Distribution works well here.




        http://en.wikipedia.org/wiki/Student%27s_t-distribution
Error Rate
✦   5 runs
    ✦ (each run is potentially 1000s of
       individual test runs)
✦   95% Confidence (t-distribution = 2.776)
✦   Standard Errors Mean =
    ✦ (std_dev / sqrt(runs)) * 2.776

✦   Error = (err_mean / mean) * 100
✦   This way you can get results like:
    ✦ 123ms +/- 5ms
V8 Benchmark
✦   Tests are run, potentially, 1000s of times.
✦   Also provides an error rate.
✦   (Use a geometric mean to arrive at a
    result.)
Small Time Accuracy
✦   Small time:
    ✦ 1ms, 1ms, 1ms, 1ms, 3ms
    ✦ huge error!

✦   Large time:
    ✦ 1234ms, 1234ms, 1234ms, 1234ms, 1238ms
    ✦ tiny error!

✦   Tests that run faster need to be run more
    times.
    ✦ Running more times = less potential for
      weird results.
         http://ejohn.org/blog/javascript-benchmark-quality/
Runs/Second
✦   var start = (new Date).getTime();
    while (time < 1000) {
      runTest();
      time = (new Date).getTime() - start;
    }
✦   More test runs, more statistical accuracy.
✦   V8 & Dromaeo-style suites handle this.
✦   (Problem: getTime() is being run on every
    loop - it should be run less frequently in
    order to influence the numbers less.)
Runs/Second
✦   You are now measuring tests/second rather
    than seconds per test.
✦   You run tests as many times in one second
    as you can.
✦   Then you do that multiple times (5?)
✦   THEN you analyze the final numbers:
    ✦ 1234run/s, 1230runs/s, 1240runs/s, ...
Harmonic Mean
✦   A way to average rates
    ✦ Which is what we have! runs/second




✦   For example:
    ✦ 1234run/s, 1230runs/s, 1240runs/s,
      1236runs/ms, 1232runs/s
    ✦ 5 / ( (1/1234) + (1/1230) + (1/1240) + (1/1236)
      + (1/1232) ) =
    ✦ 1234.39runs/s!
       http://en.wikipedia.org/wiki/Harmonic_mean
Dromaeo
✦   All individual tests are versioned
    ✦ Makes it easy to update or fix a bug in a
      test
    ✦ Can only run tests of specific versions
      against each other
✦   Uses V8’s style of running tests.
✦   Also has DOM and framework tests.
✦   ...and hooks for doing Shark profiling.
Bug Fixes
✦   Tests will, inevitably, have bugs that need
    to be fixed.
    ✦ Fixing a bug changes the result quality.

✦   Tests need to be versioned so that changes
    can be made.
✦   You look at Test v1 vs. Test v1 results.
    ✦ Not Test v2 vs. Test v1.

✦   Tip: Just use the last revision control
    commit # for the test file.
Different Code, Same Platform
✦   Most solutions here are very poor.
✦   Run the test very few times, use getTime().
    ✦ Highly inaccurate results, massive error.
Garbage Collection
✦   Browsers periodically run garbage
    collectors to clean up old objects no longer
    referenced.
✦   This can take a long time and spike your
    test results.
✦   Example:
    ✦ 10ms, 13ms, 11ms, 12ms, 486ms, 12ms, ...

✦   When comparing engine to engine, this
    doesn’t matter.
    ✦ Comparing code vs. code, it does.
Mean, Median, Mode?
✦   Mode!
✦   Run your tests a large number of times.
    ✦ What is the ‘mode’ (the result that
      occurs most frequently)
✦   Example:
    ✦ 10, 11, 11, 12, 12, 12, 13, 14
    ✦ Mode = 12ms.

✦   Less accurate than mean, but gives you a
    more-consistent result.
    ✦ DON’T DISCARD “BAD” RESULTS!
getTime() Accuracy


 http://ejohn.org/blog/accuracy-of-javascript-time/
15ms intervals ONLY!

     Error Rate of 50-750%!
IE in Wine
✦   Running Internet Explorer in Wine (on
    Linux) gives fine-grained timer results
    ✦ Down to the millisecond!

✦   You can also run IE, in Wine, on OS X:
    ✦ ies4osx

✦   Huge Caveat: It gives you fine-grained
    time, but that doesn’t mean it’s accurate.
Different Code, Same Platform
✦   How can we get good numbers?
✦   We have to go straight to the source: Use
    the tools the browsers provide.
Firebug Profiler
Safari 4 Profiler
IE 8 Profiler
IE 6-8: DynaTrace Ajax
IE 6-8: DynaTrace Ajax
Shark Profiling
✦   Extremely low-level
    ✦ Watch for all internal function calls
    ✦ See what gets called the most.

✦   Dromaeo includes shark profiling hooks.
Interesting Problems
Tackled in jQuery 1.4
Tackled in 1.4
✦   Complexity reduction
✦   Bubbling change, submit, focus, blur
✦   Required script loading
Questions
✦   Contact:
    ✦ John Resig
    ✦ http://ejohn.org/
    ✦ http://twitter.com/jeresig

More Related Content

What's hot

33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Unit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon GaliciaUnit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon GaliciaRobot Media
 
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
 
Automated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xAutomated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xTatsuya Maki
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unitliminescence
 
AngularJS Testing Strategies
AngularJS Testing StrategiesAngularJS Testing Strategies
AngularJS Testing Strategiesnjpst8
 
From typing the test to testing the type
From typing the test to testing the typeFrom typing the test to testing the type
From typing the test to testing the typeWim Godden
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languagesRafael Winterhalter
 

What's hot (20)

33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Qunit Java script Un
Qunit Java script UnQunit Java script Un
Qunit Java script Un
 
Unit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon GaliciaUnit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon Galicia
 
Easy Button
Easy ButtonEasy Button
Easy Button
 
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
 
Automated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xAutomated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.x
 
Calculon
CalculonCalculon
Calculon
 
JUnit
JUnitJUnit
JUnit
 
Agile Android
Agile AndroidAgile Android
Agile Android
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
Agile Swift
Agile SwiftAgile Swift
Agile Swift
 
AngularJS Testing Strategies
AngularJS Testing StrategiesAngularJS Testing Strategies
AngularJS Testing Strategies
 
Test
TestTest
Test
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Testing in-groovy
Testing in-groovyTesting in-groovy
Testing in-groovy
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
iOS Talks 6: Unit Testing
iOS Talks 6: Unit TestingiOS Talks 6: Unit Testing
iOS Talks 6: Unit Testing
 
Java Testing Tools
Java Testing ToolsJava Testing Tools
Java Testing Tools
 
From typing the test to testing the type
From typing the test to testing the typeFrom typing the test to testing the type
From typing the test to testing the type
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languages
 

Viewers also liked

Arithmetic, geometric and harmonic progression
Arithmetic, geometric and harmonic progression Arithmetic, geometric and harmonic progression
Arithmetic, geometric and harmonic progression Dr. Trilok Kumar Jain
 
Harmonic Mean for Monitored Rate Data
Harmonic Mean for Monitored Rate DataHarmonic Mean for Monitored Rate Data
Harmonic Mean for Monitored Rate DataNeil Gunther
 
Calculation of arithmetic mean
Calculation of arithmetic meanCalculation of arithmetic mean
Calculation of arithmetic meanSajina Nair
 
Measure OF Central Tendency
Measure OF Central TendencyMeasure OF Central Tendency
Measure OF Central TendencyIqrabutt038
 
How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...
How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...
How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...Daniel Stevens
 
Mean, Median, Mode: Measures of Central Tendency
Mean, Median, Mode: Measures of Central Tendency Mean, Median, Mode: Measures of Central Tendency
Mean, Median, Mode: Measures of Central Tendency Jan Nah
 
Measures of central tendency
Measures of central tendencyMeasures of central tendency
Measures of central tendencyChie Pegollo
 
A Beginners Guide to noSQL
A Beginners Guide to noSQLA Beginners Guide to noSQL
A Beginners Guide to noSQLMike Crabb
 

Viewers also liked (9)

Arithmetic, geometric and harmonic progression
Arithmetic, geometric and harmonic progression Arithmetic, geometric and harmonic progression
Arithmetic, geometric and harmonic progression
 
Harmonic Mean for Monitored Rate Data
Harmonic Mean for Monitored Rate DataHarmonic Mean for Monitored Rate Data
Harmonic Mean for Monitored Rate Data
 
Geometric Mean
Geometric MeanGeometric Mean
Geometric Mean
 
Calculation of arithmetic mean
Calculation of arithmetic meanCalculation of arithmetic mean
Calculation of arithmetic mean
 
Measure OF Central Tendency
Measure OF Central TendencyMeasure OF Central Tendency
Measure OF Central Tendency
 
How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...
How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...
How to Understand a Harmonic Progression in Fewer than 10 Hearings: Extending...
 
Mean, Median, Mode: Measures of Central Tendency
Mean, Median, Mode: Measures of Central Tendency Mean, Median, Mode: Measures of Central Tendency
Mean, Median, Mode: Measures of Central Tendency
 
Measures of central tendency
Measures of central tendencyMeasures of central tendency
Measures of central tendency
 
A Beginners Guide to noSQL
A Beginners Guide to noSQLA Beginners Guide to noSQL
A Beginners Guide to noSQL
 

Similar to Testing, Performance Analysis, and jQuery 1.4

Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceTobie Langel
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 
Unit Testing Front End JavaScript
Unit Testing Front End JavaScriptUnit Testing Front End JavaScript
Unit Testing Front End JavaScriptFITC
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developersAnton Udovychenko
 
Data-Driven Unit Testing for Java
Data-Driven Unit Testing for JavaData-Driven Unit Testing for Java
Data-Driven Unit Testing for JavaDenilson Nastacio
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.jsJay Harris
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
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
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
Selenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkSelenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkDavid Rajah Selvaraj
 
Unit testing with PHPUnit
Unit testing with PHPUnitUnit testing with PHPUnit
Unit testing with PHPUnitferca_sl
 
Android testing
Android testingAndroid testing
Android testingSean Tsai
 

Similar to Testing, Performance Analysis, and jQuery 1.4 (20)

Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with Evidence
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Unit Testing Front End JavaScript
Unit Testing Front End JavaScriptUnit Testing Front End JavaScript
Unit Testing Front End JavaScript
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developers
 
Data-Driven Unit Testing for Java
Data-Driven Unit Testing for JavaData-Driven Unit Testing for Java
Data-Driven Unit Testing for Java
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.js
 
3 j unit
3 j unit3 j unit
3 j unit
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
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
 
Google guava
Google guavaGoogle guava
Google guava
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Selenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkSelenium Webdriver with data driven framework
Selenium Webdriver with data driven framework
 
Unit testing with PHPUnit
Unit testing with PHPUnitUnit testing with PHPUnit
Unit testing with PHPUnit
 
Testing AngularJS
Testing AngularJSTesting AngularJS
Testing AngularJS
 
Android testing
Android testingAndroid testing
Android testing
 
Angular testing
Angular testingAngular testing
Angular testing
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 

More from jeresig

Does Coding Every Day Matter?
Does Coding Every Day Matter?Does Coding Every Day Matter?
Does Coding Every Day Matter?jeresig
 
Accidentally Becoming a Digital Librarian
Accidentally Becoming a Digital LibrarianAccidentally Becoming a Digital Librarian
Accidentally Becoming a Digital Librarianjeresig
 
2014: John's Favorite Thing (Neo4j)
2014: John's Favorite Thing (Neo4j)2014: John's Favorite Thing (Neo4j)
2014: John's Favorite Thing (Neo4j)jeresig
 
Computer Vision as Art Historical Investigation
Computer Vision as Art Historical InvestigationComputer Vision as Art Historical Investigation
Computer Vision as Art Historical Investigationjeresig
 
Hacking Art History
Hacking Art HistoryHacking Art History
Hacking Art Historyjeresig
 
Using JS to teach JS at Khan Academy
Using JS to teach JS at Khan AcademyUsing JS to teach JS at Khan Academy
Using JS to teach JS at Khan Academyjeresig
 
Applying Computer Vision to Art History
Applying Computer Vision to Art HistoryApplying Computer Vision to Art History
Applying Computer Vision to Art Historyjeresig
 
NYARC 2014: Frick/Zeri Results
NYARC 2014: Frick/Zeri ResultsNYARC 2014: Frick/Zeri Results
NYARC 2014: Frick/Zeri Resultsjeresig
 
EmpireJS: Hacking Art with Node js and Image Analysis
EmpireJS: Hacking Art with Node js and Image AnalysisEmpireJS: Hacking Art with Node js and Image Analysis
EmpireJS: Hacking Art with Node js and Image Analysisjeresig
 
Applying Computer Vision to Art History
Applying Computer Vision to Art HistoryApplying Computer Vision to Art History
Applying Computer Vision to Art Historyjeresig
 
JavaScript Libraries (Ajax Exp 2006)
JavaScript Libraries (Ajax Exp 2006)JavaScript Libraries (Ajax Exp 2006)
JavaScript Libraries (Ajax Exp 2006)jeresig
 
Introduction to jQuery (Ajax Exp 2006)
Introduction to jQuery (Ajax Exp 2006)Introduction to jQuery (Ajax Exp 2006)
Introduction to jQuery (Ajax Exp 2006)jeresig
 
jQuery Recommendations to the W3C (2011)
jQuery Recommendations to the W3C (2011)jQuery Recommendations to the W3C (2011)
jQuery Recommendations to the W3C (2011)jeresig
 
jQuery Open Source Process (RIT 2011)
jQuery Open Source Process (RIT 2011)jQuery Open Source Process (RIT 2011)
jQuery Open Source Process (RIT 2011)jeresig
 
jQuery Open Source Process (Knight Foundation 2011)
jQuery Open Source Process (Knight Foundation 2011)jQuery Open Source Process (Knight Foundation 2011)
jQuery Open Source Process (Knight Foundation 2011)jeresig
 
jQuery Mobile
jQuery MobilejQuery Mobile
jQuery Mobilejeresig
 
jQuery Open Source (Fronteer 2011)
jQuery Open Source (Fronteer 2011)jQuery Open Source (Fronteer 2011)
jQuery Open Source (Fronteer 2011)jeresig
 
Holistic JavaScript Performance
Holistic JavaScript PerformanceHolistic JavaScript Performance
Holistic JavaScript Performancejeresig
 
New Features Coming in Browsers (RIT '09)
New Features Coming in Browsers (RIT '09)New Features Coming in Browsers (RIT '09)
New Features Coming in Browsers (RIT '09)jeresig
 
Introduction to jQuery (Ajax Exp 2007)
Introduction to jQuery (Ajax Exp 2007)Introduction to jQuery (Ajax Exp 2007)
Introduction to jQuery (Ajax Exp 2007)jeresig
 

More from jeresig (20)

Does Coding Every Day Matter?
Does Coding Every Day Matter?Does Coding Every Day Matter?
Does Coding Every Day Matter?
 
Accidentally Becoming a Digital Librarian
Accidentally Becoming a Digital LibrarianAccidentally Becoming a Digital Librarian
Accidentally Becoming a Digital Librarian
 
2014: John's Favorite Thing (Neo4j)
2014: John's Favorite Thing (Neo4j)2014: John's Favorite Thing (Neo4j)
2014: John's Favorite Thing (Neo4j)
 
Computer Vision as Art Historical Investigation
Computer Vision as Art Historical InvestigationComputer Vision as Art Historical Investigation
Computer Vision as Art Historical Investigation
 
Hacking Art History
Hacking Art HistoryHacking Art History
Hacking Art History
 
Using JS to teach JS at Khan Academy
Using JS to teach JS at Khan AcademyUsing JS to teach JS at Khan Academy
Using JS to teach JS at Khan Academy
 
Applying Computer Vision to Art History
Applying Computer Vision to Art HistoryApplying Computer Vision to Art History
Applying Computer Vision to Art History
 
NYARC 2014: Frick/Zeri Results
NYARC 2014: Frick/Zeri ResultsNYARC 2014: Frick/Zeri Results
NYARC 2014: Frick/Zeri Results
 
EmpireJS: Hacking Art with Node js and Image Analysis
EmpireJS: Hacking Art with Node js and Image AnalysisEmpireJS: Hacking Art with Node js and Image Analysis
EmpireJS: Hacking Art with Node js and Image Analysis
 
Applying Computer Vision to Art History
Applying Computer Vision to Art HistoryApplying Computer Vision to Art History
Applying Computer Vision to Art History
 
JavaScript Libraries (Ajax Exp 2006)
JavaScript Libraries (Ajax Exp 2006)JavaScript Libraries (Ajax Exp 2006)
JavaScript Libraries (Ajax Exp 2006)
 
Introduction to jQuery (Ajax Exp 2006)
Introduction to jQuery (Ajax Exp 2006)Introduction to jQuery (Ajax Exp 2006)
Introduction to jQuery (Ajax Exp 2006)
 
jQuery Recommendations to the W3C (2011)
jQuery Recommendations to the W3C (2011)jQuery Recommendations to the W3C (2011)
jQuery Recommendations to the W3C (2011)
 
jQuery Open Source Process (RIT 2011)
jQuery Open Source Process (RIT 2011)jQuery Open Source Process (RIT 2011)
jQuery Open Source Process (RIT 2011)
 
jQuery Open Source Process (Knight Foundation 2011)
jQuery Open Source Process (Knight Foundation 2011)jQuery Open Source Process (Knight Foundation 2011)
jQuery Open Source Process (Knight Foundation 2011)
 
jQuery Mobile
jQuery MobilejQuery Mobile
jQuery Mobile
 
jQuery Open Source (Fronteer 2011)
jQuery Open Source (Fronteer 2011)jQuery Open Source (Fronteer 2011)
jQuery Open Source (Fronteer 2011)
 
Holistic JavaScript Performance
Holistic JavaScript PerformanceHolistic JavaScript Performance
Holistic JavaScript Performance
 
New Features Coming in Browsers (RIT '09)
New Features Coming in Browsers (RIT '09)New Features Coming in Browsers (RIT '09)
New Features Coming in Browsers (RIT '09)
 
Introduction to jQuery (Ajax Exp 2007)
Introduction to jQuery (Ajax Exp 2007)Introduction to jQuery (Ajax Exp 2007)
Introduction to jQuery (Ajax Exp 2007)
 

Recently uploaded

Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneUiPathCommunity
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Français Patch Tuesday - Avril
Français Patch Tuesday - AvrilFrançais Patch Tuesday - Avril
Français Patch Tuesday - AvrilIvanti
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 

Recently uploaded (20)

Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyone
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Français Patch Tuesday - Avril
Français Patch Tuesday - AvrilFrançais Patch Tuesday - Avril
Français Patch Tuesday - Avril
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 

Testing, Performance Analysis, and jQuery 1.4

  • 1. Testing, Performance Analysis, and jQuery 1.4 John Resig http://ejohn.org/ - http://twitter.com/jeresig
  • 2. Why Test JavaScript? ✦ Cross-browser issues. ✦ The possibility for causing an unforeseen problem is simply too great.
  • 3. JavaScript Testing Isn’t The Same As Desktop or Server-Side Testing
  • 6. Basic Components ✦ Writing and understanding a JavaScript test suite is easy. ✦ Test Suite ✦ Tests ✦ Assertions ✦ Async Tests ✦ Test Runner
  • 7. Assertions  <html>   <head>     <title>Test Suite</title>     <script>     function assert( value, desc ) {       var li = document.createElement("li");       li.className = value ? "pass" : "fail";       li.appendChild( document.createTextNode( desc ) );       document.getElementById("results").appendChild( li );     }          window.onload = function(){       assert( true, "The test suite is running." );     };     </script>     <style>       #results li.pass { color: green; }       #results li.fail { color: red; }     </style>   </head>   <body>     <ul id="results"></ul>   </body>   </html> 
  • 8. Tests      test("A test.", function(){         assert( true, "First assertion completed" );         assert( true, "Second assertion completed" );         assert( true, "Third assertion completed" );       });              test("Another test.", function(){         assert( true, "First test completed" );         assert( false, "Second test failed" );         assert( true, "Third assertion completed" );       }); 
  • 9. Tests    var results;          function assert( value, desc ) {       var li = document.createElement("li");       li.className = value ? "pass" : "fail";       li.appendChild( document.createTextNode( desc ) );       results.appendChild( li );       if ( !value ) {         li.parentNode.parentNode.className = "fail";       }       return li;     }          function test(name, fn){       results = document.getElementById("results");       results = assert( true, name ).appendChild(         document.createElement("ul") );       fn();     } 
  • 10. Async Tests  test("Async Test #1", function(){     pause();     setTimeout(function(){       assert( true, "First test completed" );       resume();     }, 1000);   });      test("Async Test #2", function(){     pause();     setTimeout(function(){       assert( true, "Second test completed" );       resume();     }, 1000);   }); 
  • 11. Async Tests  (function(){     var queue = [], paused = false;          this.test = function(fn){       queue.push( fn );       runTest();     };          this.pause = function(){       paused = true;     };          this.resume = function(){       paused = false;       setTimeout(runTest, 1);     };          function runTest(){       if ( !paused && queue.length ) {         queue.shift()();         if ( !paused ) {           resume();         }       }     }   })(); 
  • 12. People don’t test. :-( 900 675 450 225 0 None
  • 13. Popular Test Frameworks 300 225 150 75 0 QUnit JSUnit Selenium YUITest FireUnit Screw.Unit JSSpec
  • 14. Unit Testing ✦ Break code into logical chucks for testing. ✦ Focus on one method at a time. ✦ Good for testing APIs. ✦ Popular Frameworks: ✦ QUnit ✦ JSUnit ✦ YUITest ✦ FireUnit
  • 15. JSUnit ✦ One of the oldest JavaScript testing frameworks. ✦ A port of JUnit to JavaScript, circa 2001. ✦ Code feels very 2001 (frames!) ✦ http://www.jsunit.net/
  • 16. JSUnit        function coreSuite() {             var result = new top.jsUnitTestSuite();             result.addTestPage("tests/jsUnitAssertionTests.html");             result.addTestPage("tests/jsUnitSetUpTearDownTests.html");             result.addTestPage("tests/jsUnitRestoredHTMLDivTests.html");             result.addTestPage("tests/jsUnitFrameworkUtilityTests.html");             result.addTestPage("tests/jsUnitOnLoadTests.html");             result.addTestPage("tests/jsUnitUtilityTests.html");             return result;         }                  function serverSuite() {             var result = new top.jsUnitTestSuite();             result.addTestPage("tests/server/jsUnitVersionCheckTests.html");             result.addTestPage("tests/server/jsUnitServerAjaxTests.html");             return result;         }                  function librariesSuite() {             var result = new top.jsUnitTestSuite();             result.addTestPage("tests/jsUnitMockTimeoutTest.html");             return result;         }                  function suite() {             var newsuite = new top.jsUnitTestSuite();             newsuite.addTestSuite(coreSuite());             newsuite.addTestSuite(serverSuite());             newsuite.addTestSuite(librariesSuite());             return newsuite;         }
  • 17. JSUnit  function testAssertNotUndefined() {       assertNotUndefined("1 should not be undefined", 1);       assertNotUndefined(1);   }      function testAssertNaN() {       assertNaN("a string should not be a number", "string");       assertNaN("string");   }      function testAssertNotNaN() {       assertNotNaN("1 should not be not a number", 1);       assertNotNaN(1);   }      function testFail() {       var excep = null;       try {           fail("Failure message");       } catch (e) {           excep = e;       }       assertJsUnitException("fail(string) should throw a JsUnitException", excep);   }      function testTooFewArguments() {       var excep = null;       try {           assert();       } catch (e1) {           excep = e1;       }       assertNonJsUnitException("Calling an assertion function with too  few arguments should throw an exception", excep);   }
  • 19. YUITest (2 & 3) ✦ Testing framework built and developed by Yahoo (released Oct 2008). ✦ Completely overhauled to go with YUI v3. ✦ Features: ✦ Supports async tests. ✦ Has good event simulation. ✦ v2: http://developer.yahoo.com/yui/ examples/yuitest/ ✦ v3: http://developer.yahoo.com/yui/3/test/
  • 20. YUITest 2   YAHOO.example.yuitest.ArrayTestCase = new YAHOO.tool.TestCase({       name : "Array Tests",              setUp : function () {           this.data = [0,1,2,3,4]       },              tearDown : function () {           delete this.data;       },                testPop : function () {           var Assert = YAHOO.util.Assert;                    var value = this.data.pop();                      Assert.areEqual(4, this.data.length);           Assert.areEqual(4, value);                   },                      testPush : function () {           var Assert = YAHOO.util.Assert;                      this.data.push(5);                      Assert.areEqual(6, this.data.length);           Assert.areEqual(5, this.data[5]);                   }  }); 
  • 22. YUITest 3  Y.example.test.DataTestCase = new Y.Test.Case({       name : "Data Tests",              setUp : function () {           this.data = {               name: "test",               year: 2007,               beta: true           };       },              tearDown : function () {           delete this.data;       },              testName : function () {           var Assert = Y.Assert;                      Assert.isObject(this.data);           Assert.isString(this.data.name);           Assert.areEqual("test", this.data.name);                   },              testYear : function () {           var Assert = Y.Assert;                      Assert.isObject(this.data);           Assert.isNumber(this.data.year);           Assert.areEqual(2007, this.data.year);                   }  }); 
  • 24. QUnit ✦ Unit Testing framework built for jQuery. ✦ Features: ✦ Supports asynchronous testing. ✦ Can break code into modules. ✦ Supports test timeouts. ✦ No dependencies. ✦ Painfully simple. ✦ http://docs.jquery.com/QUnit
  • 25. QUnit Style  test("a basic test example", function() {     ok( true, "this test is fine" );     var value = "hello";     equals( "hello", value, "We expect value to be hello" );   });      module("Module A");      test("first test within module", function() {     ok( true, "all pass" );   });      test("second test within module", function() {     ok( true, "all pass" );   });      module("Module B");      test("some other test", function() {     expect(2);     equals( true, false, "failing test" );     equals( true, true, "passing test" );   }); 
  • 26. QUnit
  • 27. FireUnit ✦ Unit testing extension for Firebug ✦ fireunit.ok( true, “...” ); ✦ http://fireunit.org/
  • 28. Standardization ✦ CommonJS: A unified cross-platform API for JavaScript. ✦ (Including the server-side!) ✦ Working to standardize a simple testing API. ✦ http://wiki.commonjs.org/wiki/CommonJS
  • 29. Server-Side ✦ Ignore the browser! Simulate it on the server-side. ✦ Almost always uses Java + Rhino to construct a browser. ✦ Some frameworks: ✦ Crosscheck ✦ Env.js ✦ Blueridge
  • 30. Server-Side ✦ Crosscheck ✦ Pure Java, even simulates browser bugs. ✦ http://www.thefrontside.net/crosscheck ✦ Env.js ✦ Pure JavaScript, focuses on standards support. ✦ http://github.com/thatcher/env-js/tree/ master ✦ Blueridge ✦ Env.js + Screw.Unit + Rhino ✦ http://github.com/relevance/blue-ridge/
  • 31. Env.js $ java -jar build/js.jar Rhino 1.6 release 6 2007 06 28 js> load('build/runtest/env.js'); js> window.location = 'test/index.html'; test/index.html js> load('dist/jquery.js'); // Add pretty printing to jQuery objects: js> jQuery.fn.toString = DOMNodeList.prototype.toString; js> $('span').remove(); [ <span#å°åŒ—Taibei>, <span#å°åŒ—>, <span#utf8class1>, <span#utf8class2>, <span#foo:bar>, <span#test.foo[5]bar> ] // Yes - UTF-8 is supported in DOM documents! js> $('span') [ ] js> $('div').append('<span><b>hello!</b> world</span>'); [ <div#main>, <div#foo> ] js> $('span') [ <span>, <span> ] js> $('span').text() hello! worldhello! world
  • 32. Browser Launching ✦ Automates the process of opening browser windows, running tests, and getting results. ✦ Frequently require a specific framework. ✦ Popular frameworks: ✦ WebDriver http://code.google.com/p/ webdriver/ (Java) ✦ Waitr http://wtr.rubyforge.org/ (Ruby) ✦ JsTestDriver http://code.google.com/p/ js-test-driver/ (Java) ✦ Selenium RC http://seleniumhq.org/ projects/remote-control/ (Java)
  • 34. Distributed ✦ Selenium Grid ✦ Push Selenium tests out to many machines (that you manage), simultaneously. ✦ Collect and store the results. ✦ http://selenium-grid.seleniumhq.org/ ✦ TestSwarm ✦ Push tests to a distributed swarm of clients. ✦ Results viewable on the server. ✦ http://testswarm.com/
  • 35. The Scaling Problem ✦ The Problem: ✦ jQuery has 6 test suites ✦ Run in 15 browsers ✦ (Not even including multiple platforms or mobile browsers!) ✦ All need to be run for every commit, patch, and plugin. ✦ JavaScript testing doesn’t scale well.
  • 36. Distributed Testing ✦ Hub server ✦ Clients connect and help run tests ✦ A simple JavaScript client that can be run in all browsers ✦ Including mobile browsers! ✦ TestSwarm
  • 37. FF 3.5 FF 3.5 FF 3.5 IE 6 IE 6 FF 3 IE 6 Op 9 FF 3 IE 7 TestSwarm IE 7 Test Suite Test Suite Test Suite
  • 38.
  • 39.
  • 40.
  • 41. TestSwarm.com ✦ Incentives for top testers (t-shirts, books) ✦ Will be opening for alpha testing very soon ✦ Help your favorite JavaScript library become better tested! ✦ http://testswarm.com
  • 42. Accurately Measuring JavaScript
  • 43. Major Cases ✦ Same Code, Different Platforms ✦ Compare V8 vs. SpiderMonkey vs. JavaScriptCore ✦ Different Code, Same Platform ✦ Compare CSS Selector Engines ✦ A/B testing a piece of code
  • 44. Same Code, Different Platform ✦ A number of suites analyzing JS perf: ✦ SunSpider (from WebKit) ✦ V8 Benchmark (from V8/Chrome) ✦ Dromaeo (from Mozilla) ✦ Statistical accuracy and reproducibility is paramount.
  • 45. SunSpider ✦ All tests were highly balanced. ✦ Provide some level of statistical accuracy. ✦ +/- 5ms (for example) ✦ Tests are run by loading an iframe with the test 5 times. ✦ getTime() is run before/after each test. ✦ Entire suite must be trashed in order to upgrade/fix a test.
  • 46. Error Rate? ✦ How do we get it? What does it mean? ✦ It’s how confident we are that we arrived at the result we want in the number of runs that we’ve done.
  • 47. Normal Distribution ✦ First: Assume that the results are coming back in a normal distribution. ✦ The “bell curve”
  • 48.
  • 49. Confidence ✦ Next: We need a confidence level. ✦ T-Distribution works well here. http://en.wikipedia.org/wiki/Student%27s_t-distribution
  • 50. Error Rate ✦ 5 runs ✦ (each run is potentially 1000s of individual test runs) ✦ 95% Confidence (t-distribution = 2.776) ✦ Standard Errors Mean = ✦ (std_dev / sqrt(runs)) * 2.776 ✦ Error = (err_mean / mean) * 100 ✦ This way you can get results like: ✦ 123ms +/- 5ms
  • 51. V8 Benchmark ✦ Tests are run, potentially, 1000s of times. ✦ Also provides an error rate. ✦ (Use a geometric mean to arrive at a result.)
  • 52. Small Time Accuracy ✦ Small time: ✦ 1ms, 1ms, 1ms, 1ms, 3ms ✦ huge error! ✦ Large time: ✦ 1234ms, 1234ms, 1234ms, 1234ms, 1238ms ✦ tiny error! ✦ Tests that run faster need to be run more times. ✦ Running more times = less potential for weird results. http://ejohn.org/blog/javascript-benchmark-quality/
  • 53.
  • 54.
  • 55. Runs/Second ✦ var start = (new Date).getTime(); while (time < 1000) { runTest(); time = (new Date).getTime() - start; } ✦ More test runs, more statistical accuracy. ✦ V8 & Dromaeo-style suites handle this. ✦ (Problem: getTime() is being run on every loop - it should be run less frequently in order to influence the numbers less.)
  • 56. Runs/Second ✦ You are now measuring tests/second rather than seconds per test. ✦ You run tests as many times in one second as you can. ✦ Then you do that multiple times (5?) ✦ THEN you analyze the final numbers: ✦ 1234run/s, 1230runs/s, 1240runs/s, ...
  • 57. Harmonic Mean ✦ A way to average rates ✦ Which is what we have! runs/second ✦ For example: ✦ 1234run/s, 1230runs/s, 1240runs/s, 1236runs/ms, 1232runs/s ✦ 5 / ( (1/1234) + (1/1230) + (1/1240) + (1/1236) + (1/1232) ) = ✦ 1234.39runs/s! http://en.wikipedia.org/wiki/Harmonic_mean
  • 58. Dromaeo ✦ All individual tests are versioned ✦ Makes it easy to update or fix a bug in a test ✦ Can only run tests of specific versions against each other ✦ Uses V8’s style of running tests. ✦ Also has DOM and framework tests. ✦ ...and hooks for doing Shark profiling.
  • 59. Bug Fixes ✦ Tests will, inevitably, have bugs that need to be fixed. ✦ Fixing a bug changes the result quality. ✦ Tests need to be versioned so that changes can be made. ✦ You look at Test v1 vs. Test v1 results. ✦ Not Test v2 vs. Test v1. ✦ Tip: Just use the last revision control commit # for the test file.
  • 60. Different Code, Same Platform ✦ Most solutions here are very poor. ✦ Run the test very few times, use getTime(). ✦ Highly inaccurate results, massive error.
  • 61. Garbage Collection ✦ Browsers periodically run garbage collectors to clean up old objects no longer referenced. ✦ This can take a long time and spike your test results. ✦ Example: ✦ 10ms, 13ms, 11ms, 12ms, 486ms, 12ms, ... ✦ When comparing engine to engine, this doesn’t matter. ✦ Comparing code vs. code, it does.
  • 62. Mean, Median, Mode? ✦ Mode! ✦ Run your tests a large number of times. ✦ What is the ‘mode’ (the result that occurs most frequently) ✦ Example: ✦ 10, 11, 11, 12, 12, 12, 13, 14 ✦ Mode = 12ms. ✦ Less accurate than mean, but gives you a more-consistent result. ✦ DON’T DISCARD “BAD” RESULTS!
  • 64.
  • 65.
  • 66.
  • 67. 15ms intervals ONLY! Error Rate of 50-750%!
  • 68. IE in Wine ✦ Running Internet Explorer in Wine (on Linux) gives fine-grained timer results ✦ Down to the millisecond! ✦ You can also run IE, in Wine, on OS X: ✦ ies4osx ✦ Huge Caveat: It gives you fine-grained time, but that doesn’t mean it’s accurate.
  • 69.
  • 70. Different Code, Same Platform ✦ How can we get good numbers? ✦ We have to go straight to the source: Use the tools the browsers provide.
  • 76. Shark Profiling ✦ Extremely low-level ✦ Watch for all internal function calls ✦ See what gets called the most. ✦ Dromaeo includes shark profiling hooks.
  • 77.
  • 78.
  • 80. Tackled in 1.4 ✦ Complexity reduction ✦ Bubbling change, submit, focus, blur ✦ Required script loading
  • 81. Questions ✦ Contact: ✦ John Resig ✦ http://ejohn.org/ ✦ http://twitter.com/jeresig