Commit be1e4eee authored by Timmy Willison's avatar Timmy Willison

Convert testrunner to an AMD module and ensure jQuery is on the page when...

Convert testrunner to an AMD module and ensure jQuery is on the page when executing the testrunner (another race condition amplified by swarm)

Conflicts:
	test/data/testinit.js
	test/data/testrunner.js
parent d14904d0
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
"globals": { "globals": {
"require": false, "require": false,
"define": false,
"DOMParser": false, "DOMParser": false,
"QUnit": false, "QUnit": false,
"ok": false, "ok": false,
......
...@@ -259,9 +259,13 @@ this.iframeCallback = undefined; ...@@ -259,9 +259,13 @@ this.iframeCallback = undefined;
QUnit.config.autostart = false; QUnit.config.autostart = false;
this.loadTests = function() { this.loadTests = function() {
var loadSwarm, var loadSwarm,
url = window.location.search, url = window.location.search;
tests = [ url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + "swarmURL=".length ) );
"data/testrunner.js", loadSwarm = url && url.indexOf("http") === 0;
// Get testSubproject from testrunner first
require([ "data/testrunner.js" ], function( testSubproject ) {
var tests = [
"unit/core.js", "unit/core.js",
"unit/callbacks.js", "unit/callbacks.js",
"unit/deferred.js", "unit/deferred.js",
...@@ -281,8 +285,6 @@ this.loadTests = function() { ...@@ -281,8 +285,6 @@ this.loadTests = function() {
"unit/offset.js", "unit/offset.js",
"unit/dimensions.js" "unit/dimensions.js"
]; ];
url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + "swarmURL=".length ) );
loadSwarm = url && url.indexOf("http") === 0;
// Ensure load order (to preserve test numbers) // Ensure load order (to preserve test numbers)
(function loadDep() { (function loadDep() {
...@@ -295,15 +297,6 @@ this.loadTests = function() { ...@@ -295,15 +297,6 @@ this.loadTests = function() {
// Call load to build module filter select element // Call load to build module filter select element
QUnit.load(); QUnit.load();
// Load the TestSwarm listener if swarmURL is in the address.
if ( loadSwarm ) {
require( [ "http://swarm.jquery.org/js/inject.js?" + (new Date()).getTime() ], function() {
QUnit.start();
});
} else {
QUnit.start();
}
/** /**
* Run in noConflict mode * Run in noConflict mode
*/ */
...@@ -317,7 +310,17 @@ this.loadTests = function() { ...@@ -317,7 +310,17 @@ this.loadTests = function() {
supportjQuery.each( [ jQuery.expando, "getInterface", "Packages", "java", "netscape" ], function( i, name ) { supportjQuery.each( [ jQuery.expando, "getInterface", "Packages", "java", "netscape" ], function( i, name ) {
window[ name ] = window[ name ]; window[ name ] = window[ name ];
}); });
// Load the TestSwarm listener if swarmURL is in the address.
if ( loadSwarm ) {
require( [ "http://swarm.jquery.org/js/inject.js?" + (new Date()).getTime() ], function() {
QUnit.start();
});
} else {
QUnit.start();
}
}); });
} }
})(); })();
});
}; };
define(function() {
// Allow subprojects to test against their own fixtures // Allow subprojects to test against their own fixtures
var qunitModule = QUnit.module, var oldStart = window.start,
qunitTest = QUnit.test; qunitModule = QUnit.module,
qunitTest = QUnit.test,
/** // Store the old counts so that we only assert on tests that have actually leaked,
* Test a subproject with its own fixture // instead of asserting every time a test has leaked sometime in the past
* @param {String} label Project name oldCacheLength = 0,
* @param {String} url Test folder location oldFragmentsLength = 0,
* @param {RegExp} risTests To filter script sources oldActive = 0,
*/
this.testSubproject = function( label, url, risTests, complete ) {
var sub, fixture, fixtureHTML,
fixtureReplaced = false;
// Don't let subproject tests jump the gun
QUnit.config.reorder = false;
// Create module
module( label );
// Duckpunch QUnit
// TODO restore parent fixture on teardown to support reordering
module = QUnit.module = function( name ) {
var args = arguments;
// Remember subproject-scoped module name
sub = name;
// Override
args[0] = label;
return qunitModule.apply( this, args );
};
test = function( name ) {
var args = arguments,
i = args.length - 1;
// Prepend subproject-scoped module name to test name
args[0] = sub + ": " + name;
// Find test function and wrap to require subproject fixture
for ( ; i >= 0; i-- ) {
if ( supportjQuery.isFunction( args[i] ) ) {
args[i] = requireFixture( args[i] );
break;
}
}
return qunitTest.apply( this, args );
};
// Load tests and fixture from subproject
// Test order matters, so we must be synchronous and throw an error on load failure
supportjQuery.ajax( url, {
async: false,
dataType: "html",
error: function( jqXHR, status ) {
throw new Error( "Could not load: " + url + " (" + status + ")" );
},
success: function( data, status, jqXHR ) {
var sources = [],
page = supportjQuery.parseHTML(
// replace html/head with dummy elements so they are represented in the DOM
( data || "" ).replace( /<\/?((!DOCTYPE|html|head)\b.*?)>/gi, "[$1]" ),
document,
true
);
if ( !page || !page.length ) {
this.error( jqXHR, "no data" );
}
page = supportjQuery( page );
// Include subproject tests
page.filter("script[src]").add( page.find("script[src]") ).map(function() {
var src = supportjQuery( this ).attr("src");
if ( risTests.test( src ) ) {
sources.push( src );
}
});
// Ensure load order
(function loadDep() {
var dep = sources.shift();
if ( dep ) {
require( [ url + dep ], loadDep );
} else if ( complete ) {
complete();
}
})();
// Get the fixture, including content outside of #qunit-fixture
fixture = page.find("[id='qunit-fixture']");
fixtureHTML = fixture.html();
fixture.empty();
while ( fixture.length && !fixture.prevAll("[id='qunit']").length ) {
fixture = fixture.parent();
}
fixture = fixture.add( fixture.nextAll() );
}
});
function requireFixture( fn ) { expectedDataKeys = {},
return function() {
if ( !fixtureReplaced ) {
// Make sure that we retrieved a fixture for the subproject
if ( !fixture.length ) {
ok( false, "Found subproject fixture" );
return;
}
// Replace the current fixture, including content outside of #qunit-fixture splice = [].splice,
var oldFixture = supportjQuery("#qunit-fixture"); reset = QUnit.reset,
while ( oldFixture.length && !oldFixture.prevAll("[id='qunit']").length ) { ajaxSettings = jQuery.ajaxSettings;
oldFixture = oldFixture.parent();
}
oldFixture.nextAll().remove();
oldFixture.replaceWith( fixture );
// WARNING: UNDOCUMENTED INTERFACE
QUnit.config.fixture = fixtureHTML;
QUnit.reset();
if ( supportjQuery("#qunit-fixture").html() !== fixtureHTML ) {
ok( false, "Copied subproject fixture" );
return;
}
fixtureReplaced = true; /**
} * QUnit configuration
*/
fn.apply( this, arguments );
};
}
};
// Register globals for cleanup and the cleanup code itself // Max time for stop() and asyncTest() until it aborts test
// Explanation at http://perfectionkills.com/understanding-delete/#ie_bugs // and start()'s the next test.
this.Globals = (function() { QUnit.config.testTimeout = 20 * 1000; // 20 seconds
var globals = {};
return {
register: function( name ) {
globals[ name ] = true;
supportjQuery.globalEval( "var " + name + " = undefined;" );
},
cleanup: function() {
var name,
current = globals;
globals = {};
for ( name in current ) {
supportjQuery.globalEval( "try { " +
"delete " + ( supportjQuery.support.deleteExpando ? "window['" + name + "']" : name ) +
"; } catch( x ) {}" );
}
}
};
})();
// Enforce an "expect" argument or expect() call in all test bodies.
QUnit.config.requireExpects = true;
/** /**
* QUnit hooks * QUnit hooks
*/ */
// Sandbox start for great justice // Sandbox start for great justice
(function() { window.start = function() {
var oldStart = window.start;
window.start = function() {
oldStart(); oldStart();
}; };
})();
(function() {
// Store the old counts so that we only assert on tests that have actually leaked,
// instead of asserting every time a test has leaked sometime in the past
var oldCacheLength = 0,
oldFragmentsLength = 0,
oldActive = 0,
expectedDataKeys = {},
splice = [].splice,
reset = QUnit.reset,
ajaxSettings = jQuery.ajaxSettings;
function keys(o) { function keys(o) {
var ret, key; var ret, key;
if ( Object.keys ) { if ( Object.keys ) {
ret = Object.keys( o ); ret = Object.keys( o );
...@@ -193,20 +49,24 @@ this.Globals = (function() { ...@@ -193,20 +49,24 @@ this.Globals = (function() {
} }
ret.sort(); ret.sort();
return ret; return ret;
} }
/** /**
* @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data. * @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data.
* @param {string} key * @param {string} key
*/ */
QUnit.expectJqData = function( elems, key ) { QUnit.expectJqData = function( elems, key ) {
var i, elem, expando; var i, elem, expando;
// As of jQuery 2.0, there will be no "cache"-data is
// stored and managed completely below the API surface
if ( jQuery.cache ) {
QUnit.current_testEnvironment.checkJqData = true; QUnit.current_testEnvironment.checkJqData = true;
if ( elems.jquery && elems.toArray ) { if ( elems.jquery && elems.toArray ) {
elems = elems.toArray(); elems = elems.toArray();
} }
if ( !jQuery.isArray( elems ) ) { if ( !supportjQuery.isArray( elems ) ) {
elems = [ elems ]; elems = [ elems ];
} }
...@@ -242,18 +102,20 @@ this.Globals = (function() { ...@@ -242,18 +102,20 @@ this.Globals = (function() {
} }
} }
} }
}; }
QUnit.config.urlConfig.push({
};
QUnit.config.urlConfig.push({
id: "jqdata", id: "jqdata",
label: "Always check jQuery.data", label: "Always check jQuery.data",
tooltip: "Trigger QUnit.expectJqData detection for all tests instead of just the ones that call it" tooltip: "Trigger QUnit.expectJqData detection for all tests instead of just the ones that call it"
}); });
/** /**
* Ensures that tests have cleaned up properly after themselves. Should be passed as the * Ensures that tests have cleaned up properly after themselves. Should be passed as the
* teardown function on all modules' lifecycle object. * teardown function on all modules' lifecycle object.
*/ */
this.moduleTeardown = function() { window.moduleTeardown = function() {
var i, var i,
expectedKeys, actualKeys, expectedKeys, actualKeys,
fragmentsLength = 0, fragmentsLength = 0,
...@@ -318,15 +180,15 @@ this.Globals = (function() { ...@@ -318,15 +180,15 @@ this.Globals = (function() {
equal( fragmentsLength, oldFragmentsLength, "No unit tests leak memory in jQuery.fragments" ); equal( fragmentsLength, oldFragmentsLength, "No unit tests leak memory in jQuery.fragments" );
oldFragmentsLength = fragmentsLength; oldFragmentsLength = fragmentsLength;
} }
}; };
QUnit.done(function() { QUnit.done(function() {
// Remove our own fixtures outside #qunit-fixture // Remove our own fixtures outside #qunit-fixture
supportjQuery("#qunit ~ *").remove(); supportjQuery("#qunit ~ *").remove();
}); });
// jQuery-specific QUnit.reset // jQuery-specific QUnit.reset
QUnit.reset = function() { QUnit.reset = function() {
// Ensure jQuery events and data on the fixture are properly removed // Ensure jQuery events and data on the fixture are properly removed
jQuery("#qunit-fixture").empty(); jQuery("#qunit-fixture").empty();
...@@ -346,15 +208,160 @@ this.Globals = (function() { ...@@ -346,15 +208,160 @@ this.Globals = (function() {
// Let QUnit reset the fixture // Let QUnit reset the fixture
reset.apply( this, arguments ); reset.apply( this, arguments );
};
// Register globals for cleanup and the cleanup code itself
// Explanation at http://perfectionkills.com/understanding-delete/#ie_bugs
window.Globals = (function() {
var globals = {};
return {
register: function( name ) {
globals[ name ] = true;
supportjQuery.globalEval( "var " + name + " = undefined;" );
},
cleanup: function() {
var name,
current = globals;
globals = {};
for ( name in current ) {
supportjQuery.globalEval( "try { " +
"delete " + ( supportjQuery.support.deleteExpando ? "window['" + name + "']" : name ) +
"; } catch( x ) {}" );
}
}
}; };
})(); })();
/** /**
* QUnit configuration * Test a subproject with its own fixture
* @param {String} label Project name
* @param {String} url Test folder location
* @param {RegExp} risTests To filter script sources
*/ */
// Max time for stop() and asyncTest() until it aborts test function testSubproject( label, url, risTests, complete ) {
// and start()'s the next test. var sub, fixture, fixtureHTML,
QUnit.config.testTimeout = 20 * 1000; // 20 seconds fixtureReplaced = false;
// Enforce an "expect" argument or expect() call in all test bodies. // Don't let subproject tests jump the gun
QUnit.config.requireExpects = true; QUnit.config.reorder = false;
// Create module
module( label );
// Duckpunch QUnit
// TODO restore parent fixture on teardown to support reordering
module = QUnit.module = function( name ) {
var args = arguments;
// Remember subproject-scoped module name
sub = name;
// Override
args[0] = label;
return qunitModule.apply( this, args );
};
test = function( name ) {
var args = arguments,
i = args.length - 1;
// Prepend subproject-scoped module name to test name
args[0] = sub + ": " + name;
// Find test function and wrap to require subproject fixture
for ( ; i >= 0; i-- ) {
if ( supportjQuery.isFunction( args[i] ) ) {
args[i] = requireFixture( args[i] );
break;
}
}
return qunitTest.apply( this, args );
};
// Load tests and fixture from subproject
// Test order matters, so we must be synchronous and throw an error on load failure
supportjQuery.ajax( url, {
async: false,
dataType: "html",
error: function( jqXHR, status ) {
throw new Error( "Could not load: " + url + " (" + status + ")" );
},
success: function( data, status, jqXHR ) {
var sources = [],
page = supportjQuery.parseHTML(
// replace html/head with dummy elements so they are represented in the DOM
( data || "" ).replace( /<\/?((!DOCTYPE|html|head)\b.*?)>/gi, "[$1]" ),
document,
true
);
if ( !page || !page.length ) {
this.error( jqXHR, "no data" );
}
page = supportjQuery( page );
// Include subproject tests
page.filter("script[src]").add( page.find("script[src]") ).map(function() {
var src = supportjQuery( this ).attr("src");
if ( risTests.test( src ) ) {
sources.push( src );
}
});
// Ensure load order
(function loadDep() {
var dep = sources.shift();
if ( dep ) {
require( [ url + dep ], loadDep );
} else if ( complete ) {
complete();
}
})();
// Get the fixture, including content outside of #qunit-fixture
fixture = page.find("[id='qunit-fixture']");
fixtureHTML = fixture.html();
fixture.empty();
while ( fixture.length && !fixture.prevAll("[id='qunit']").length ) {
fixture = fixture.parent();
}
fixture = fixture.add( fixture.nextAll() );
}
});
function requireFixture( fn ) {
return function() {
if ( !fixtureReplaced ) {
// Make sure that we retrieved a fixture for the subproject
if ( !fixture.length ) {
ok( false, "Found subproject fixture" );
return;
}
// Replace the current fixture, including content outside of #qunit-fixture
var oldFixture = supportjQuery("#qunit-fixture");
while ( oldFixture.length && !oldFixture.prevAll("[id='qunit']").length ) {
oldFixture = oldFixture.parent();
}
oldFixture.nextAll().remove();
oldFixture.replaceWith( fixture );
// WARNING: UNDOCUMENTED INTERFACE
QUnit.config.fixture = fixtureHTML;
QUnit.reset();
if ( supportjQuery("#qunit-fixture").html() !== fixtureHTML ) {
ok( false, "Copied subproject fixture" );
return;
}
fixtureReplaced = true;
}
fn.apply( this, arguments );
};
}
}
return testSubproject;
});
...@@ -29,6 +29,13 @@ ...@@ -29,6 +29,13 @@
).replace(/\w+/g, function(n) { ).replace(/\w+/g, function(n) {
document.createElement(n); document.createElement(n);
}); });
// Load tests if they have not been loaded
// This is in a different script tag to ensure that
// jQuery is on the page when the testrunner executes
if ( !QUnit.urlParams.amd ) {
loadTests();
}
</script> </script>
</head> </head>
......
...@@ -54,11 +54,9 @@ ...@@ -54,11 +54,9 @@
// Load jQuery // Load jQuery
document.write( "<script id='jquery-js' src='" + path + src + "'><\x2Fscript>" ); document.write( "<script id='jquery-js' src='" + path + src + "'><\x2Fscript>" );
// Load tests if available
// These can be loaded async as QUnit won't start until finished
if ( typeof loadTests !== "undefined" ) {
loadTests();
// Synchronous-only tests // Synchronous-only tests
// Other tests are loaded from the test page
if ( typeof loadTests !== "undefined" ) {
document.write( "<script src='" + path + "test/unit/ready.js'><\x2Fscript>"); document.write( "<script src='" + path + "test/unit/ready.js'><\x2Fscript>");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment