runner.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. $(function() {
  2. var Runner = function( ) {
  3. var self = this;
  4. $.extend( self, {
  5. frame: window.frames[ "testFrame" ],
  6. testTimeout: 3 * 60 * 1000,
  7. $frameElem: $( "#testFrame" ),
  8. assertionResultPrefix: "assertion result for test:",
  9. onTimeout: QUnit.start,
  10. onFrameLoad: function() {
  11. // establish a timeout for a given suite in case of async tests hanging
  12. self.testTimer = setTimeout( self.onTimeout, self.testTimeout );
  13. // it might be a redirect with query params for push state
  14. // tests skip this call and expect another
  15. if( !self.frame.QUnit ) {
  16. self.$frameElem.one( "load", self.onFrameLoad );
  17. return;
  18. }
  19. // when the QUnit object reports done in the iframe
  20. // run the onFrameDone method
  21. self.frame.QUnit.done = self.onFrameDone;
  22. self.frame.QUnit.testDone = self.onTestDone;
  23. },
  24. onTestDone: function( result ) {
  25. QUnit.ok( !(result.failed > 0), result.name );
  26. self.recordAssertions( result.total - result.failed, result.name );
  27. },
  28. onFrameDone: function( failed, passed, total, runtime ){
  29. // make sure we don't time out the tests
  30. clearTimeout( self.testTimer );
  31. // TODO decipher actual cause of multiple test results firing twice
  32. // clear the done call to prevent early completion of other test cases
  33. self.frame.QUnit.done = $.noop;
  34. self.frame.QUnit.testDone = $.noop;
  35. // hide the extra assertions made to propogate the count
  36. // to the suite level test
  37. self.hideAssertionResults();
  38. // continue on to the next suite
  39. QUnit.start();
  40. },
  41. recordAssertions: function( count, parentTest ) {
  42. for( var i = 0; i < count; i++ ) {
  43. ok( true, self.assertionResultPrefix + parentTest );
  44. }
  45. },
  46. hideAssertionResults: function() {
  47. $( "li:not([id]):contains('" + self.assertionResultPrefix + "')" ).hide();
  48. },
  49. exec: function( data ) {
  50. var template = self.$frameElem.attr( "data-src" );
  51. $.each( data.testPages, function(i, dir) {
  52. QUnit.asyncTest( dir, function() {
  53. self.dir = dir;
  54. self.$frameElem.one( "load", self.onFrameLoad );
  55. self.$frameElem.attr( "src", template.replace("{{testdir}}", dir) );
  56. });
  57. });
  58. // having defined all suite level tests let QUnit run
  59. QUnit.start();
  60. }
  61. });
  62. };
  63. // prevent qunit from starting the test suite until all tests are defined
  64. QUnit.begin = function( ) {
  65. this.config.autostart = false;
  66. };
  67. // get the test directories
  68. $.get( "ls.php", (new Runner()).exec );
  69. });