Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Allow Karma to load unminfied source #4128

Merged
merged 8 commits into from Sep 7, 2018
8 changes: 4 additions & 4 deletions Gruntfile.js
Expand Up @@ -177,7 +177,7 @@ module.exports = function( grunt ) {
"external/requirejs/require.js",
"test/data/testinit.js",

"dist/jquery.min.js",
"test/jquery.js",

// Replacement for testinit.js#loadTests()
"test/data/testrunner.js",
Expand Down Expand Up @@ -205,9 +205,9 @@ module.exports = function( grunt ) {
"test/unit/tween.js",
"test/unit/ready.js",

{ pattern: "dist/jquery.js", included: false, served: true },
{ pattern: "dist/*.map", included: false, served: true },
{ pattern: "external/qunit/qunit.css", included: false, served: true },
{ pattern: "dist/jquery.*", included: false, served: true },
{ pattern: "src/**", included: false, served: true },
{ pattern: "external/**", included: false, served: true },
{
pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
included: false,
Expand Down
42 changes: 28 additions & 14 deletions test/data/testinit.js
@@ -1,11 +1,17 @@
/* eslint no-multi-str: "off" */

// baseURL is intentionally set to "data/" instead of "".
// This is not just for convenience (since most files are in data/)
// but also to ensure that urls without prefix fail.
// Otherwise it's easy to write tests that pass on test/index.html
// but fail in Karma runner (where the baseURL is different).
var baseURL = "data/",
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 "".
// This is not just for convenience (since most files are in data/)
// but also to ensure that urls without prefix fail.
// Otherwise it's easy to write tests that pass on test/index.html
// but fail in Karma runner (where the baseURL is different).
baseURL = parentUrl + "test/data/",
supportjQuery = this.jQuery,

// see RFC 2606
Expand Down Expand Up @@ -271,12 +277,9 @@ this.testIframe = function( title, fileName, func, wrapper ) {
};
this.iframeCallback = undefined;

if ( window.__karma__ ) {
// In Karma, files are served from /base
baseURL = "base/test/data/";
} else {
// Tests are always loaded async
// except when running tests in Karma (See Gruntfile)
// Tests are always loaded async
// except when running tests in Karma (See Gruntfile)
if ( !window.__karma__ ) {
QUnit.config.autostart = false;
}

Expand All @@ -295,8 +298,19 @@ moduleTypeSupported();

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
require( [ "data/testrunner.js" ], function() {
require( [ parentUrl + "test/data/testrunner.js" ], function() {
var i = 0,
tests = [
// A special module with basic tests, meant for
Expand Down Expand Up @@ -334,7 +348,7 @@ this.loadTests = function() {

if ( dep ) {
if ( !QUnit.basicTests || i === 1 ) {
require( [ dep ], loadDep );
require( [ parentUrl + "test/" + dep ], loadDep );

// Support: Android 2.3 only
// When running basic tests, replace other modules with dummies to avoid overloading
Expand Down
31 changes: 17 additions & 14 deletions test/jquery.js
Expand Up @@ -2,8 +2,11 @@
( function() {
/* global loadTests: false */

var pathname = window.location.pathname,
path = pathname.slice( 0, pathname.lastIndexOf( "test" ) ),
var FILEPATH = "/test/jquery.js",
activeScript = [].slice.call( document.getElementsByTagName( "script" ), -1 )[ 0 ],
parentUrl = activeScript && activeScript.src ?
activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" :
"../",
QUnit = window.QUnit || parent.QUnit,
require = window.require || parent.require,

Expand All @@ -17,11 +20,16 @@

// Define configuration parameters controlling how jQuery is loaded
if ( QUnit ) {
QUnit.config.urlConfig.push( {
id: "amd",
label: "Load with AMD",
tooltip: "Load the AMD jQuery file (and its dependencies)"
} );

// AMD loading is asynchronous and incompatible with synchronous test loading in Karma
if ( !window.__karma__ ) {
QUnit.config.urlConfig.push( {
id: "amd",
label: "Load with AMD",
tooltip: "Load the AMD jQuery file (and its dependencies)"
} );
}

QUnit.config.urlConfig.push( {
id: "dev",
label: "Load unminified",
Expand All @@ -33,7 +41,7 @@
// This doesn't apply to iframes because they synchronously expect jQuery to be there.
if ( urlParams.amd && window.QUnit ) {
require.config( {
baseUrl: path
baseUrl: parentUrl
} );
src = "src/jquery";

Expand All @@ -46,12 +54,7 @@

// Otherwise, load synchronously
} else {
document.write( "<script id='jquery-js' src='" + path + 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>" );
}
document.write( "<script id='jquery-js' src='" + parentUrl + src + "'><\x2Fscript>" );
}

} )();
41 changes: 30 additions & 11 deletions test/unit/ajax.js
Expand Up @@ -345,20 +345,22 @@ QUnit.module( "ajax", {
};
} );

ajaxTest( "jQuery.ajax() - hash", 4, function( assert ) {
ajaxTest( "jQuery.ajax() - URL fragment component preservation", 4, function( assert ) {
return [
{
url: baseURL + "name.html#foo",
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;
},
error: true
},
{
url: baseURL + "name.html?abc#foo",
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;
},
error: true
Expand All @@ -369,7 +371,8 @@ QUnit.module( "ajax", {
"test": 123
},
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;
},
error: true
Expand All @@ -381,9 +384,10 @@ QUnit.module( "ajax", {
},
cache: false,
beforeSend: function( xhr, settings ) {
// Remove the random number, but ensure the cache-buster param is there
var url = settings.url.replace( /\d+/, "" );
assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies", "Make sure that the URL has its hash." );
// Clear the cache-buster param value
var url = settings.url.replace( /_=[^&#]+/, "_=" );
assert.equal( url, baseURL + "name.html?abc&devo=hat&_=#brownies",
"hash preserved for cache-busting request with query component and data." );
return false;
},
error: true
Expand Down Expand Up @@ -1133,7 +1137,7 @@ QUnit.module( "ajax", {
setup: function() {
Globals.register( "testBar" );
},
url: window.location.href.replace( /[^\/]*$/, "" ) + baseURL + "mock.php?action=testbar",
url: url( "mock.php?action=testbar" ),
dataType: "script",
success: function() {
assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
Expand All @@ -1146,7 +1150,7 @@ QUnit.module( "ajax", {
setup: function() {
Globals.register( "testBar" );
},
url: window.location.href.replace( /[^\/]*$/, "" ) + baseURL + "mock.php?action=testbar",
url: url( "mock.php?action=testbar" ),
type: "POST",
dataType: "script",
success: function( data, status ) {
Expand All @@ -1161,7 +1165,7 @@ QUnit.module( "ajax", {
setup: function() {
Globals.register( "testBar" );
},
url: window.location.href.replace( /[^\/]*$/, "" ).replace( /^.*?\/\//, "//" ) + baseURL + "mock.php?action=testbar",
url: url( "mock.php?action=testbar" ),
dataType: "script",
success: function() {
assert.strictEqual( window[ "testBar" ], "bar", "Script results returned (GET, no callback)" );
Expand Down Expand Up @@ -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 ) {
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.length, 25, "Check JSON: length" );
QUnit.start();
Expand Down