Tests: Allow Karma to load unminfied source

Closes gh-4128
This commit is contained in:
Richard Gibson 2018-09-07 10:14:01 -04:00 committed by GitHub
parent f997241f00
commit dfa92ccead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 43 deletions

View File

@ -177,7 +177,7 @@ module.exports = function( grunt ) {
"external/requirejs/require.js", "external/requirejs/require.js",
"test/data/testinit.js", "test/data/testinit.js",
"dist/jquery.min.js", "test/jquery.js",
// Replacement for testinit.js#loadTests() // Replacement for testinit.js#loadTests()
"test/data/testrunner.js", "test/data/testrunner.js",
@ -205,9 +205,9 @@ module.exports = function( grunt ) {
"test/unit/tween.js", "test/unit/tween.js",
"test/unit/ready.js", "test/unit/ready.js",
{ pattern: "dist/jquery.js", included: false, served: true }, { pattern: "dist/jquery.*", included: false, served: true },
{ pattern: "dist/*.map", included: false, served: true }, { pattern: "src/**", included: false, served: true },
{ pattern: "external/qunit/qunit.css", included: false, served: true }, { pattern: "external/**", included: false, served: true },
{ {
pattern: "test/**/*.@(js|css|jpg|html|xml|svg)", pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
included: false, included: false,

View File

@ -1,11 +1,17 @@
/* eslint no-multi-str: "off" */ /* eslint no-multi-str: "off" */
var FILEPATH = "/test/data/testinit.js",
activeScript = [].slice.call( document.getElementsByTagName( "script" ), -1 )[ 0 ],
parentUrl = activeScript && activeScript.src ?
activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" :
"../",
// baseURL is intentionally set to "data/" instead of "". // baseURL is intentionally set to "data/" instead of "".
// This is not just for convenience (since most files are in data/) // This is not just for convenience (since most files are in data/)
// but also to ensure that urls without prefix fail. // but also to ensure that urls without prefix fail.
// Otherwise it's easy to write tests that pass on test/index.html // Otherwise it's easy to write tests that pass on test/index.html
// but fail in Karma runner (where the baseURL is different). // but fail in Karma runner (where the baseURL is different).
var baseURL = "data/", baseURL = parentUrl + "test/data/",
supportjQuery = this.jQuery, supportjQuery = this.jQuery,
// see RFC 2606 // see RFC 2606
@ -271,12 +277,9 @@ this.testIframe = function( title, fileName, func, wrapper ) {
}; };
this.iframeCallback = undefined; this.iframeCallback = undefined;
if ( window.__karma__ ) {
// In Karma, files are served from /base
baseURL = "base/test/data/";
} else {
// Tests are always loaded async // Tests are always loaded async
// except when running tests in Karma (See Gruntfile) // except when running tests in Karma (See Gruntfile)
if ( !window.__karma__ ) {
QUnit.config.autostart = false; QUnit.config.autostart = false;
} }
@ -295,8 +298,19 @@ moduleTypeSupported();
this.loadTests = function() { this.loadTests = function() {
// Directly load tests that need synchronous evaluation
if ( !QUnit.urlParams.amd || document.readyState === "loading" ) {
document.write( "<script src='" + parentUrl + "test/unit/ready.js'><\x2Fscript>" );
} else {
QUnit.module( "ready", function() {
QUnit.test( "jQuery ready", function( assert ) {
assert.ok( false, "Test should be initialized before DOM ready" );
} );
} );
}
// Get testSubproject from testrunner first // Get testSubproject from testrunner first
require( [ "data/testrunner.js" ], function() { require( [ parentUrl + "test/data/testrunner.js" ], function() {
var i = 0, var i = 0,
tests = [ tests = [
// A special module with basic tests, meant for // A special module with basic tests, meant for
@ -334,7 +348,7 @@ this.loadTests = function() {
if ( dep ) { if ( dep ) {
if ( !QUnit.basicTests || i === 1 ) { if ( !QUnit.basicTests || i === 1 ) {
require( [ dep ], loadDep ); require( [ parentUrl + "test/" + dep ], loadDep );
// Support: Android 2.3 only // Support: Android 2.3 only
// When running basic tests, replace other modules with dummies to avoid overloading // When running basic tests, replace other modules with dummies to avoid overloading

21
test/jquery.js vendored
View File

@ -2,8 +2,11 @@
( function() { ( function() {
/* global loadTests: false */ /* global loadTests: false */
var pathname = window.location.pathname, var FILEPATH = "/test/jquery.js",
path = pathname.slice( 0, pathname.lastIndexOf( "test" ) ), activeScript = [].slice.call( document.getElementsByTagName( "script" ), -1 )[ 0 ],
parentUrl = activeScript && activeScript.src ?
activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" :
"../",
QUnit = window.QUnit || parent.QUnit, QUnit = window.QUnit || parent.QUnit,
require = window.require || parent.require, require = window.require || parent.require,
@ -17,11 +20,16 @@
// Define configuration parameters controlling how jQuery is loaded // Define configuration parameters controlling how jQuery is loaded
if ( QUnit ) { if ( QUnit ) {
// AMD loading is asynchronous and incompatible with synchronous test loading in Karma
if ( !window.__karma__ ) {
QUnit.config.urlConfig.push( { QUnit.config.urlConfig.push( {
id: "amd", id: "amd",
label: "Load with AMD", label: "Load with AMD",
tooltip: "Load the AMD jQuery file (and its dependencies)" tooltip: "Load the AMD jQuery file (and its dependencies)"
} ); } );
}
QUnit.config.urlConfig.push( { QUnit.config.urlConfig.push( {
id: "dev", id: "dev",
label: "Load unminified", label: "Load unminified",
@ -33,7 +41,7 @@
// This doesn't apply to iframes because they synchronously expect jQuery to be there. // This doesn't apply to iframes because they synchronously expect jQuery to be there.
if ( urlParams.amd && window.QUnit ) { if ( urlParams.amd && window.QUnit ) {
require.config( { require.config( {
baseUrl: path baseUrl: parentUrl
} ); } );
src = "src/jquery"; src = "src/jquery";
@ -46,12 +54,7 @@
// Otherwise, load synchronously // Otherwise, load synchronously
} else { } else {
document.write( "<script id='jquery-js' src='" + path + src + "'><\x2Fscript>" ); document.write( "<script id='jquery-js' src='" + parentUrl + src + "'><\x2Fscript>" );
// 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>" );
}
} }
} )(); } )();

View File

@ -345,12 +345,13 @@ QUnit.module( "ajax", {
}; };
} ); } );
ajaxTest( "jQuery.ajax() - hash", 4, function( assert ) { ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) {
return [ return [
{ {
url: baseURL + "name.html#foo", url: baseURL + "name.html#foo",
beforeSend: function( xhr, settings ) { beforeSend: function( xhr, settings ) {
assert.equal( settings.url, baseURL + "name.html#foo", "Make sure that the URL has its hash." ); assert.equal( settings.url, baseURL + "name.html#foo",
"hash preserved for request with no query component." );
return false; return false;
}, },
error: true error: true
@ -358,7 +359,8 @@ QUnit.module( "ajax", {
{ {
url: baseURL + "name.html?abc#foo", url: baseURL + "name.html?abc#foo",
beforeSend: function( xhr, settings ) { beforeSend: function( xhr, settings ) {
assert.equal( settings.url, baseURL + "name.html?abc#foo", "Make sure that the URL has its hash." ); assert.equal( settings.url, baseURL + "name.html?abc#foo",
"hash preserved for request with query component." );
return false; return false;
}, },
error: true error: true
@ -369,7 +371,8 @@ QUnit.module( "ajax", {
"test": 123 "test": 123
}, },
beforeSend: function( xhr, settings ) { beforeSend: function( xhr, settings ) {
assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo", "Make sure that the URL has its hash." ); assert.equal( settings.url, baseURL + "name.html?abc&test=123#foo",
"hash preserved for request with query component and data." );
return false; return false;
}, },
error: true error: true
@ -381,9 +384,10 @@ QUnit.module( "ajax", {
}, },
cache: false, cache: false,
beforeSend: function( xhr, settings ) { beforeSend: function( xhr, settings ) {
// Remove the random number, but ensure the cache-buster param is there // Clear the cache-buster param value
var url = settings.url.replace( /\d+/, "" ); var url = settings.url.replace( /_=[^&#]+/, "_=" );
assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies", "Make sure that the URL has its hash." ); assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies",
"hash preserved for cache-busting request with query component and data." );
return false; return false;
}, },
error: true error: true
@ -1133,7 +1137,7 @@ QUnit.module( "ajax", {
setup: function() { setup: function() {
Globals.register( "testBar" ); Globals.register( "testBar" );
}, },
url: window.location.href.replace( /[^\/]*$/, "" ) + baseURL + "mock.php?action=testbar", url: url( "mock.php?action=testbar" ),
dataType: "script", dataType: "script",
success: function() { success: function() {
assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" ); assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
@ -1146,7 +1150,7 @@ QUnit.module( "ajax", {
setup: function() { setup: function() {
Globals.register( "testBar" ); Globals.register( "testBar" );
}, },
url: window.location.href.replace( /[^\/]*$/, "" ) + baseURL + "mock.php?action=testbar", url: url( "mock.php?action=testbar" ),
type: "POST", type: "POST",
dataType: "script", dataType: "script",
success: function( data, status ) { success: function( data, status ) {
@ -1161,7 +1165,7 @@ QUnit.module( "ajax", {
setup: function() { setup: function() {
Globals.register( "testBar" ); Globals.register( "testBar" );
}, },
url: window.location.href.replace( /[^\/]*$/, "" ).replace( /^.*?\/\//, "//" ) + baseURL + "mock.php?action=testbar", url: url( "mock.php?action=testbar" ),
dataType: "script", dataType: "script",
success: function() { success: function() {
assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" ); assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
@ -2303,7 +2307,22 @@ if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().re
} ); } );
QUnit.asyncTest( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", 2, function( assert ) { QUnit.asyncTest( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", 2, function( assert ) {
jQuery.getJSON( window.location.href.replace( /[^\/]*$/, "" ) + url( "mock.php?action=json" ), function( json ) { var absoluteUrl = url( "mock.php?action=json" );
// Make a relative URL absolute relative to the document location
if ( !/^[a-z][a-z0-9+.-]*:/i.test( absoluteUrl ) ) {
// An absolute path replaces everything after the host
if ( absoluteUrl.charAt( 0 ) === "/" ) {
absoluteUrl = window.location.href.replace( /(:\/*[^/]*).*$/, "$1" ) + absoluteUrl;
// A relative path replaces the last slash-separated path segment
} else {
absoluteUrl = window.location.href.replace( /[^/]*$/, "" ) + absoluteUrl;
}
}
jQuery.getJSON( absoluteUrl, function( json ) {
assert.strictEqual( json.data.lang, "en", "Check JSON: lang" ); assert.strictEqual( json.data.lang, "en", "Check JSON: lang" );
assert.strictEqual( json.data.length, 25, "Check JSON: length" ); assert.strictEqual( json.data.length, 25, "Check JSON: length" );
QUnit.start(); QUnit.start();