Tuesday, March 18th, 2008

jqunit: extending jquerys testrunner to all

Category: Framework, JavaScript, jQuery, Testing

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

that produces:

Posted by Dion Almaer at 11:01 am
3 Comments

+++--
3.1 rating from 32 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.