Skip to content

Commit

Permalink
Fixes #432 - Using a setTimeout stub can stop test suite from continu…
Browse files Browse the repository at this point in the history
…ing - closes gh-433
  • Loading branch information
qxjit authored and jzaefferer committed May 14, 2013
1 parent 38396bb commit cf41077
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
7 changes: 4 additions & 3 deletions qunit/qunit.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
Expand Down
63 changes: 63 additions & 0 deletions test/test.js
Expand Up @@ -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 =
' <html>' +
' <head>' +
' <meta charset="UTF-8">' +
' <title>QUnit Test Suite</title>' +
' <link rel="stylesheet" href="../qunit/qunit.css">' +
' </head>' +
' <body>' +
' <div id="qunit"></div>' +
' <script src="../qunit/qunit.js"></script>' +
' <script type="text/javascript">' +
' QUnit.config.updateRate = 1;' +
' module("Module that mucks with time", {' +
' setup: function() {' +
' this.setTimeout = window.setTimeout;' +
' window.setTimeout = function() {};' +
' },' +
' teardown: function() {' +
' window.setTimeout = this.setTimeout;' +
' }' +
' });' +
' test("just a test", function() { ok(true); });' +
' test("just a test", function() { ok(true); });' +
' </script>' +
' </body>' +
' </html>';

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");
Expand Down

0 comments on commit cf41077

Please sign in to comment.