]> source.dussan.org Git - jquery.git/commitdiff
Tests: Allow Karma to load unminfied source
authorRichard Gibson <richard.gibson@gmail.com>
Fri, 7 Sep 2018 14:14:01 +0000 (10:14 -0400)
committerGitHub <noreply@github.com>
Fri, 7 Sep 2018 14:14:01 +0000 (10:14 -0400)
Closes gh-4128

Gruntfile.js
test/data/testinit.js
test/jquery.js
test/unit/ajax.js

index ed2bd775371f66045bb6b00f44725d2b9318e5dc..b46b7457f201b4f43e0a9c0dffba9b51d8f9caef 100644 (file)
@@ -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",
@@ -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,
index a91e59b6accacb12a547ccc8eb9aecf5414c97e2..a87ebf2064bce5444a17a40ec0b0c7692c7bfabc 100644 (file)
@@ -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
@@ -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;
 }
 
@@ -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
@@ -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
index d8eaf86293e5af362f9865d01b6f3a77609aeac9..8ba139e6baecdb117b6bf194ec0a1c33e2391a58 100644 (file)
@@ -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,
 
 
        // 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",
@@ -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";
 
 
        // 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>" );
        }
 
 } )();
index 72a4e721550b9473f70d215813cd0d5332cbb54e..b8b46e245c06a5aa5ee9d4d518295842dde7980e 100644 (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 [
                        {
                                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
@@ -358,7 +359,8 @@ QUnit.module( "ajax", {
                        {
                                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
@@ -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
@@ -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
@@ -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)" );
@@ -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 ) {
@@ -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)" );
@@ -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();