From cf41077c30080650ba12dbba2b193b24e0b3a7d0 Mon Sep 17 00:00:00 2001 From: David Vollbracht Date: Mon, 25 Mar 2013 08:32:44 -0400 Subject: [PATCH] Fixes #432 - Using a setTimeout stub can stop test suite from continuing - closes gh-433 --- qunit/qunit.js | 7 +++--- test/test.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/qunit/qunit.js b/qunit/qunit.js index 5a2b2c173..d80974598 100644 --- a/qunit/qunit.js +++ b/qunit/qunit.js @@ -20,6 +20,7 @@ var QUnit, hasOwn = Object.prototype.hasOwnProperty, // Keep a local reference to Date (GH-283) Date = window.Date, + setTimeout = window.setTimeout, defined = { setTimeout: typeof window.setTimeout !== "undefined", sessionStorage: (function() { @@ -466,7 +467,7 @@ QUnit = { } // A slight delay, to avoid any current callbacks if ( defined.setTimeout ) { - window.setTimeout(function() { + setTimeout(function() { if ( config.semaphore > 0 ) { return; } @@ -489,7 +490,7 @@ QUnit = { if ( config.testTimeout && defined.setTimeout ) { clearTimeout( config.timeout ); - config.timeout = window.setTimeout(function() { + config.timeout = setTimeout(function() { QUnit.ok( false, "Test timed out" ); config.semaphore = 1; QUnit.start(); @@ -1445,7 +1446,7 @@ function process( last ) { if ( !defined.setTimeout || config.updateRate <= 0 || ( ( new Date().getTime() - start ) < config.updateRate ) ) { config.queue.shift()(); } else { - window.setTimeout( next, 13 ); + setTimeout( next, 13 ); break; } } diff --git a/test/test.js b/test/test.js index a03f53c51..c105e51f6 100644 --- a/test/test.js +++ b/test/test.js @@ -743,6 +743,69 @@ test('Circular reference - test reported by soniciq in #105', function() { deepEqual(a.children(), [b]); }); +module("Faking setTimeout"); +test("QUnit isn't stopped by fake setTimeout", function() { + var innerRunner = + ' ' + + ' ' + + ' ' + + ' QUnit Test Suite' + + ' ' + + ' ' + + ' ' + + '
' + + ' ' + + ' ' + + ' ' + + ' '; + + var frame = document.createElement('iframe'); + + var supportsSrcDoc = 'srcdoc' in frame; + frame.setAttribute('srcdoc', innerRunner); + + if (!supportsSrcDoc) { + var url = ":window.frameElement.getAttribute('srcdoc')"; + frame.setAttribute('src', "javascript" + url); + } + + document.getElementById('qunit-fixture').appendChild(frame); + + expect(1); + QUnit.stop(); + + var start = Date.now(); + setTimeout(checkFinished, 25); + + function checkFinished() { + var qunitElement = frame.contentDocument.getElementById('qunit'); + var completed = /completed/.test(qunitElement && qunitElement.innerHTML); + + if (completed) { + ok(true); + QUnit.start(); + } else if (Date.now() - start > 1000) { + ok(false, "QUnit was stopped by fake setTimeout!"); + QUnit.start(); + } else { + setTimeout(checkFinished, 25); + } + } +}); + (function() { var reset = QUnit.reset; module("reset");