]> source.dussan.org Git - jquery.git/commitdiff
Build: Run the basic test suite in jsdom
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Mon, 11 Mar 2019 19:03:54 +0000 (20:03 +0100)
committerGitHub <noreply@github.com>
Mon, 11 Mar 2019 19:03:54 +0000 (20:03 +0100)
The basic test suite is now run in jsdom on all supported Node.js versions
(8, 10 & 11 as of now).

Closes gh-4310

Gruntfile.js
package.json
test/.eslintrc.json
test/data/testinit-jsdom.js [new file with mode: 0644]
test/unit/basic.js

index 904abbcea269133c9f1e32bf973551f9dfd39aa8..7fa324399b084f04b22457701470589744ddb570 100644 (file)
@@ -14,7 +14,15 @@ module.exports = function( grunt ) {
 
        var fs = require( "fs" ),
                gzip = require( "gzip-js" ),
-               isTravis = process.env.TRAVIS;
+               isTravis = process.env.TRAVIS,
+               oldNode = /^v6\./.test( process.version );
+
+       // Support: Node.js <8
+       // Skip running tasks that dropped support for Node.js 6
+       // in those Node versions.
+       function runIfNewNode( task ) {
+               return oldNode ? "print_old_node_message:" + task : task;
+       }
 
        if ( !grunt.option( "filename" ) ) {
                grunt.option( "filename", "jquery.js" );
@@ -220,6 +228,33 @@ module.exports = function( grunt ) {
                                browsers: [ isTravis ? "ChromeHeadlessNoSandbox" : "ChromeHeadless" ]
                        },
 
+                       jsdom: {
+                               options: {
+                                       files: [
+                                               "test/data/jquery-1.9.1.js",
+                                               "test/data/testinit-jsdom.js",
+
+                                               // We don't support various loading methods like AMD,
+                                               // choosing a version etc. for jsdom.
+                                               "dist/jquery.js",
+
+                                               // Replacement for testinit.js#loadTests()
+                                               "test/data/testrunner.js",
+
+                                               // jsdom only runs basic tests
+                                               "test/unit/basic.js",
+
+                                               { pattern: "external/**", included: false, served: true },
+                                               {
+                                                       pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",
+                                                       included: false,
+                                                       served: true
+                                               }
+                                       ]
+                               },
+                               browsers: [ "jsdom" ]
+                       },
+
                        // To debug tests with Karma:
                        // 1. Run 'grunt karma:chrome-debug' or 'grunt karma:firefox-debug'
                        //    (any karma subtask that has singleRun=false)
@@ -285,6 +320,13 @@ module.exports = function( grunt ) {
        // Integrate jQuery specific tasks
        grunt.loadTasks( "build/tasks" );
 
+       // Support: Node.js <8
+       // Print a message on Node.js <8 notifying the task is skipped there.
+       grunt.registerTask( "print_old_node_message", function() {
+               var task = [].slice.call( arguments ).join( ":" );
+               grunt.log.writeln( "Old Node.js detected, running the task \"" + task + "\" skipped..." );
+       } );
+
        grunt.registerTask( "lint", [
                "jsonlint",
 
@@ -305,7 +347,13 @@ module.exports = function( grunt ) {
        ] );
 
        grunt.registerTask( "test:fast", "node_smoke_tests" );
-       grunt.registerTask( "test:slow", "promises_aplus_tests" );
+       grunt.registerTask( "test:slow", [
+               "promises_aplus_tests",
+
+               // Support: Node.js <8
+               // Karma no longer supports Node.js <8 as it relies on async-await internally.
+               runIfNewNode( "karma:jsdom" )
+       ] );
 
        grunt.registerTask( "test", [
                "test:fast",
index ffe9d1b1d1c8bdd29535ca58b105c464ece5550e..4124d2d931dee9b8bb37a7819fd26023db8bdcf2 100644 (file)
@@ -51,6 +51,7 @@
     "karma-chrome-launcher": "2.2.0",
     "karma-firefox-launcher": "1.1.0",
     "karma-ie-launcher": "1.0.0",
+    "karma-jsdom-launcher": "7.1.0",
     "karma-qunit": "3.0.0",
     "load-grunt-tasks": "4.0.0",
     "native-promise-only": "0.8.1",
index 2c724626fc0ab96285897d5f0717fc74d31ca2f3..a23ea5f98466f60ff932bb1f625df1928d70ad44 100644 (file)
 
        "overrides": [
                {
-                       "files": ["data/core/jquery-iterability-transpiled-es6.js"],
+                       "files": [
+                               "data/core/jquery-iterability-transpiled-es6.js",
+                               "data/testinit-jsdom.js"
+                       ],
                        "parserOptions": {
                                "ecmaVersion": 2015
                        }
diff --git a/test/data/testinit-jsdom.js b/test/data/testinit-jsdom.js
new file mode 100644 (file)
index 0000000..e0830cc
--- /dev/null
@@ -0,0 +1,40 @@
+"use strict";
+
+// Support: jsdom 13.2+
+// jsdom implements a throwing `window.scrollTo`.
+QUnit.config.scrolltop = false;
+
+const FILEPATH = "/test/data/testinit-jsdom.js";
+const activeScript = document.currentScript;
+const parentUrl = activeScript && activeScript.src ?
+       activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" :
+       "../";
+const supportjQuery = this.jQuery;
+
+// 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).
+const baseURL = parentUrl + "test/data/";
+
+// Setup global variables before loading jQuery for testing .noConflict()
+supportjQuery.noConflict( true );
+window.originaljQuery = this.jQuery = undefined;
+window.original$ = this.$ = "replaced";
+
+/**
+ * Add random number to url to stop caching
+ *
+ * Also prefixes with baseURL automatically.
+ *
+ * @example url("index.html")
+ * @result "data/index.html?10538358428943"
+ *
+ * @example url("mock.php?foo=bar")
+ * @result "data/mock.php?foo=bar&10538358345554"
+ */
+function url( value ) {
+       return baseURL + value + ( /\?/.test( value ) ? "&" : "?" ) +
+               new Date().getTime() + "" + parseInt( Math.random() * 100000, 10 );
+}
index b57c0c0ba9e9c564beca96c1b7d2666b4a565b33..bed27dcbabe52380811a11773e428f12d98935e0 100644 (file)
@@ -193,7 +193,9 @@ QUnit.test( "manipulation", function( assert ) {
        );
 } );
 
-QUnit.test( "offset", function( assert ) {
+// Support: jsdom 13.2+
+// jsdom returns 0 for offset-related properties
+QUnit[ /jsdom\//.test( navigator.userAgent ) ? "skip" : "test" ]( "offset", function( assert ) {
        assert.expect( 3 );
 
        var parent = jQuery( "<div style='position:fixed;top:20px;'/>" ).appendTo( "#qunit-fixture" ),