Activate your free membership today | Log-in

Tuesday, March 18th, 2008

jqunit: extending jquerys testrunner to all

Category: Framework, JavaScript, Testing, jQuery

Michael Grosser has created jQuerys testrunner in a way that makes it work with jsUnit, and also useful for libraries other than jQuery.

Here is a full example:

JAVASCRIPT:
  1.  
  2. var temp = function($) {
  3.   jqUnit.module('Without local interface');
  4.   jqUnit.test('test a', function(){
  5.     jqUnit.ok(true);
  6.     this.ok(true);
  7.   });
  8.        
  9. with(jqUnit) {
  10.   module('With local interface');
  11.   test('test b', function(){
  12.     ok(true);
  13. });
  14.  
  15.  
  16.   module('Example tests');
  17.   test('Real Click vs False Click',function(){
  18.     var clicked = false;
  19.     $('#test-form').click(function(){clicked=true;});
  20.    
  21.     //false click
  22.     $('#test-form input').click();
  23.     ok(!clicked);
  24.    
  25.     //real click
  26.     triggerEvent($('#test-form input').get(0),'click');
  27.     ok(clicked);
  28.   });
  29.  
  30.   test('Waiting',function(){
  31.     $('#ajax').load('fixtures/1.html');
  32.     expect(1);//expect 1 assertion, here: fails if ajaxStop is never called
  33.     stop();//pause: so we can wait with setTimeout,setInterval,...
  34.    
  35.     $().ajaxStop(function(){setTimeout(function(){
  36.       //field is not filled directly after ajaxStop
  37.       //since DOM traversal comes after stopping to load
  38.       equals($('#ajax').html(),1);//!reverted jsUnit order
  39.       start();//resume: make sure its called or tests will halt!
  40.     })});
  41.   });
  42. }}(jQuery);
  43.  

that produces:

Posted by Dion Almaer at 11:01 am

+++--
3.2 rating from 26 votes

3 Comments »

Comments feed TrackBack URI

correction:
i did not create ‘jQuerys testrunner’ it was created by the jQuery team. Most, if not all, credit for jqUnit goes to Colin Clark (http://groups.google.com/group/jquery-en/msg/b6b7ff05f5f06945)

Comment by grosser — March 18, 2008

correction2:
link for jQuery testrunner is not working

Comment by grosser — March 18, 2008

image is broken, since of today, since wordpress screwed me and somehow moved/renamed my pictures…
http://pragmatig.files.wordpress.com/2008/03/jqunit.png

maybe it can be fixed when wordpress cache expires…

Comment by grosser — March 27, 2008

Leave a comment

You must be logged in to post a comment.