aboutsummaryrefslogtreecommitdiffstats
path: root/test/node_smoke_tests
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2023-07-10 19:14:08 +0200
committerGitHub <noreply@github.com>2023-07-10 19:14:08 +0200
commit8be4c0e4f89d6c8f780e5937a0534921d8c7815e (patch)
tree55d87bcc040dfea54ec5c77ba86d942aa9526916 /test/node_smoke_tests
parent65b85031fb5688361c077bc04e641e4b502671e1 (diff)
downloadjquery-8be4c0e4f89d6c8f780e5937a0534921d8c7815e.tar.gz
jquery-8be4c0e4f89d6c8f780e5937a0534921d8c7815e.zip
Build: Add `exports` to package.json, export slim & esm builds
Summary of the changes: * define the `exports` field in `package.json`; `jQuery` & `$` are also exported as named exports in ESM builds now * declare `"type": "module"` globally except for the `build` folder * add the `--esm` option to `grunt custom`, generating jQuery as an ECMAScript module into the `dist-module` folder * expand `node_smoke_tests` to test the slim & ESM builds and their various combinations; also, test both jQuery loaded via a path to the file as well as from module specifiers that should be parsed via the `exports` feature * add details about ESM usage to the release package README * run `compare_size` on all built minified files; don't run it anymore on unminified files where they don't provide lots of value * remove the remove_map_comment task; SWC doesn't insert the `//# sourceMappingURL=` pragma by default so there's nothing to strip Fixes gh-4592 Closes gh-5255
Diffstat (limited to 'test/node_smoke_tests')
-rw-r--r--test/node_smoke_tests/.eslintrc.json13
-rw-r--r--test/node_smoke_tests/commonjs/.eslintrc.json13
-rw-r--r--test/node_smoke_tests/commonjs/document_missing.cjs15
-rw-r--r--test/node_smoke_tests/commonjs/document_passed.cjs16
-rw-r--r--test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs14
-rw-r--r--test/node_smoke_tests/commonjs/lib/ensure_global_not_created.cjs (renamed from test/node_smoke_tests/lib/ensure_global_not_created.js)4
-rw-r--r--test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs25
-rw-r--r--test/node_smoke_tests/commonjs/lib/ensure_jquery.cjs (renamed from test/node_smoke_tests/lib/ensure_jquery.js)4
-rw-r--r--test/node_smoke_tests/commonjs/lib/jquery-module-specifier.cjs22
-rw-r--r--test/node_smoke_tests/commonjs/window_present_originally.cjs19
-rw-r--r--test/node_smoke_tests/document_missing.js11
-rw-r--r--test/node_smoke_tests/document_passed.js12
-rw-r--r--test/node_smoke_tests/document_present_originally.js15
-rw-r--r--test/node_smoke_tests/iterable_with_native_symbol.js8
-rw-r--r--test/node_smoke_tests/lib/ensure_iterability_es6.js25
-rw-r--r--test/node_smoke_tests/module/.eslintrc.json13
-rw-r--r--test/node_smoke_tests/module/document_missing.js13
-rw-r--r--test/node_smoke_tests/module/document_passed.js14
-rw-r--r--test/node_smoke_tests/module/iterable_with_native_symbol.js12
-rw-r--r--test/node_smoke_tests/module/lib/ensure_global_not_created.js11
-rw-r--r--test/node_smoke_tests/module/lib/ensure_iterability_es6.js21
-rw-r--r--test/node_smoke_tests/module/lib/ensure_jquery.js7
-rw-r--r--test/node_smoke_tests/module/lib/jquery-module-specifier.js21
-rw-r--r--test/node_smoke_tests/module/window_present_originally.js17
24 files changed, 257 insertions, 88 deletions
diff --git a/test/node_smoke_tests/.eslintrc.json b/test/node_smoke_tests/.eslintrc.json
deleted file mode 100644
index a1bd6ec3f..000000000
--- a/test/node_smoke_tests/.eslintrc.json
+++ /dev/null
@@ -1,13 +0,0 @@
-{
- "root": true,
-
- "extends": "../../.eslintrc-node.json",
-
- "parserOptions": {
- "ecmaVersion": 6
- },
-
- "env": {
- "es6": true
- }
-}
diff --git a/test/node_smoke_tests/commonjs/.eslintrc.json b/test/node_smoke_tests/commonjs/.eslintrc.json
new file mode 100644
index 000000000..26409bd89
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/.eslintrc.json
@@ -0,0 +1,13 @@
+{
+ "root": true,
+
+ "extends": "../../../.eslintrc-node.json",
+
+ "parserOptions": {
+ "ecmaVersion": 2015,
+ "sourceType": "script"
+ },
+ "env": {
+ "es2022": true
+ }
+}
diff --git a/test/node_smoke_tests/commonjs/document_missing.cjs b/test/node_smoke_tests/commonjs/document_missing.cjs
new file mode 100644
index 000000000..cf31fe427
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/document_missing.cjs
@@ -0,0 +1,15 @@
+"use strict";
+
+const assert = require( "node:assert" );
+
+const { ensureGlobalNotCreated } = require( "./lib/ensure_global_not_created.cjs" );
+const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" );
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+const jQueryFactory = require( jQueryModuleSpecifier );
+
+assert.throws( () => {
+ jQueryFactory( {} );
+}, /jQuery requires a window with a document/ );
+
+ensureGlobalNotCreated( module.exports );
diff --git a/test/node_smoke_tests/commonjs/document_passed.cjs b/test/node_smoke_tests/commonjs/document_passed.cjs
new file mode 100644
index 000000000..4cf280109
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/document_passed.cjs
@@ -0,0 +1,16 @@
+"use strict";
+
+const { JSDOM } = require( "jsdom" );
+
+const { ensureJQuery } = require( "./lib/ensure_jquery.cjs" );
+const { ensureGlobalNotCreated } = require( "./lib/ensure_global_not_created.cjs" );
+const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" );
+
+const { window } = new JSDOM( "" );
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+const jQueryFactory = require( jQueryModuleSpecifier );
+const jQuery = jQueryFactory( window );
+
+ensureJQuery( jQuery );
+ensureGlobalNotCreated( module.exports );
diff --git a/test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs b/test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs
new file mode 100644
index 000000000..8fce34e06
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs
@@ -0,0 +1,14 @@
+"use strict";
+
+const process = require( "node:process" );
+
+if ( typeof Symbol === "undefined" ) {
+ console.log( "Symbols not supported, skipping the test..." );
+ process.exit();
+}
+
+const { ensureIterability } = require( "./lib/ensure_iterability_es6.cjs" );
+const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" );
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+ensureIterability( jQueryModuleSpecifier );
diff --git a/test/node_smoke_tests/lib/ensure_global_not_created.js b/test/node_smoke_tests/commonjs/lib/ensure_global_not_created.cjs
index 95db62226..5eee11d21 100644
--- a/test/node_smoke_tests/lib/ensure_global_not_created.js
+++ b/test/node_smoke_tests/commonjs/lib/ensure_global_not_created.cjs
@@ -1,6 +1,6 @@
"use strict";
-const assert = require( "assert" );
+const assert = require( "node:assert" );
// Ensure the jQuery property on global/window/module.exports/etc. was not
// created in a CommonJS environment.
@@ -12,4 +12,4 @@ const ensureGlobalNotCreated = ( ...args ) => {
} );
};
-module.exports = ensureGlobalNotCreated;
+module.exports = { ensureGlobalNotCreated };
diff --git a/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs b/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs
new file mode 100644
index 000000000..eb4914758
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs
@@ -0,0 +1,25 @@
+"use strict";
+
+const assert = require( "node:assert" );
+const { JSDOM } = require( "jsdom" );
+
+const { ensureJQuery } = require( "./ensure_jquery.cjs" );
+
+const ensureIterability = ( jQueryModuleSpecifier ) => {
+ const { window } = new JSDOM( "" );
+
+ const jQueryFactory = require( jQueryModuleSpecifier );
+ const jQuery = jQueryFactory( window );
+ const elem = jQuery( "<div></div><span></span><a></a>" );
+
+ ensureJQuery( jQuery );
+
+ let result = "";
+ for ( const node of elem ) {
+ result += node.nodeName;
+ }
+
+ assert.strictEqual( result, "DIVSPANA", "for-of works on jQuery objects" );
+};
+
+module.exports = { ensureIterability };
diff --git a/test/node_smoke_tests/lib/ensure_jquery.js b/test/node_smoke_tests/commonjs/lib/ensure_jquery.cjs
index 5b7c064f1..be23e9900 100644
--- a/test/node_smoke_tests/lib/ensure_jquery.js
+++ b/test/node_smoke_tests/commonjs/lib/ensure_jquery.cjs
@@ -1,6 +1,6 @@
"use strict";
-const assert = require( "assert" );
+const assert = require( "node:assert" );
// Check if the object we got is the jQuery object by invoking a basic API.
const ensureJQuery = ( jQuery ) => {
@@ -8,4 +8,4 @@ const ensureJQuery = ( jQuery ) => {
"jQuery.expando was not detected, the jQuery bootstrap process has failed" );
};
-module.exports = ensureJQuery;
+module.exports = { ensureJQuery };
diff --git a/test/node_smoke_tests/commonjs/lib/jquery-module-specifier.cjs b/test/node_smoke_tests/commonjs/lib/jquery-module-specifier.cjs
new file mode 100644
index 000000000..216660d80
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/lib/jquery-module-specifier.cjs
@@ -0,0 +1,22 @@
+"use strict";
+
+const path = require( "node:path" );
+
+const ROOT_DIR = path.resolve( __dirname, "..", "..", "..", ".." );
+
+// If `jQueryModuleSpecifier` is a real relative path, make it absolute
+// to make sure it resolves to the same file inside utils from
+// a subdirectory. Otherwise, leave it as-is as we may be testing `exports`
+// so we need input as-is.
+const getJQueryModuleSpecifier = () => {
+ const jQueryModuleInputSpecifier = process.argv[ 2 ];
+ if ( !jQueryModuleInputSpecifier ) {
+ throw new Error( "jQuery module specifier not passed" );
+ }
+
+ return jQueryModuleInputSpecifier.startsWith( "." ) ?
+ path.resolve( ROOT_DIR, jQueryModuleInputSpecifier ) :
+ jQueryModuleInputSpecifier;
+};
+
+module.exports = { getJQueryModuleSpecifier };
diff --git a/test/node_smoke_tests/commonjs/window_present_originally.cjs b/test/node_smoke_tests/commonjs/window_present_originally.cjs
new file mode 100644
index 000000000..908fcb940
--- /dev/null
+++ b/test/node_smoke_tests/commonjs/window_present_originally.cjs
@@ -0,0 +1,19 @@
+"use strict";
+
+const { JSDOM } = require( "jsdom" );
+
+const { ensureJQuery } = require( "./lib/ensure_jquery.cjs" );
+const { ensureGlobalNotCreated } = require( "./lib/ensure_global_not_created.cjs" );
+const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" );
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+
+const { window } = new JSDOM( "" );
+
+// Set the window global.
+globalThis.window = window;
+
+const jQuery = require( jQueryModuleSpecifier );
+
+ensureJQuery( jQuery );
+ensureGlobalNotCreated( module.exports, window );
diff --git a/test/node_smoke_tests/document_missing.js b/test/node_smoke_tests/document_missing.js
deleted file mode 100644
index 0f6a3f78b..000000000
--- a/test/node_smoke_tests/document_missing.js
+++ /dev/null
@@ -1,11 +0,0 @@
-"use strict";
-
-const assert = require( "assert" );
-const ensureGlobalNotCreated = require( "./lib/ensure_global_not_created" );
-const jQueryFactory = require( "../../dist/jquery.js" );
-
-assert.throws( () => {
- jQueryFactory( {} );
-}, /jQuery requires a window with a document/ );
-
-ensureGlobalNotCreated( module.exports );
diff --git a/test/node_smoke_tests/document_passed.js b/test/node_smoke_tests/document_passed.js
deleted file mode 100644
index b1154d3a3..000000000
--- a/test/node_smoke_tests/document_passed.js
+++ /dev/null
@@ -1,12 +0,0 @@
-"use strict";
-
-const { JSDOM } = require( "jsdom" );
-
-const { window } = new JSDOM( "" );
-
-const ensureJQuery = require( "./lib/ensure_jquery" );
-const ensureGlobalNotCreated = require( "./lib/ensure_global_not_created" );
-const jQuery = require( "../../dist/jquery.js" )( window );
-
-ensureJQuery( jQuery );
-ensureGlobalNotCreated( module.exports );
diff --git a/test/node_smoke_tests/document_present_originally.js b/test/node_smoke_tests/document_present_originally.js
deleted file mode 100644
index 89b0c7bd8..000000000
--- a/test/node_smoke_tests/document_present_originally.js
+++ /dev/null
@@ -1,15 +0,0 @@
-"use strict";
-
-const { JSDOM } = require( "jsdom" );
-
-const { window } = new JSDOM( "" );
-
-// Pretend the window is a global.
-global.window = window;
-
-const ensureJQuery = require( "./lib/ensure_jquery" );
-const ensureGlobalNotCreated = require( "./lib/ensure_global_not_created" );
-const jQuery = require( "../../dist/jquery.js" );
-
-ensureJQuery( jQuery );
-ensureGlobalNotCreated( module.exports, window );
diff --git a/test/node_smoke_tests/iterable_with_native_symbol.js b/test/node_smoke_tests/iterable_with_native_symbol.js
deleted file mode 100644
index 3376ebdc5..000000000
--- a/test/node_smoke_tests/iterable_with_native_symbol.js
+++ /dev/null
@@ -1,8 +0,0 @@
-"use strict";
-
-if ( typeof Symbol === "undefined" ) {
- console.log( "Symbols not supported, skipping the test..." );
- process.exit();
-}
-
-require( "./lib/ensure_iterability_es6" )();
diff --git a/test/node_smoke_tests/lib/ensure_iterability_es6.js b/test/node_smoke_tests/lib/ensure_iterability_es6.js
deleted file mode 100644
index a948f1996..000000000
--- a/test/node_smoke_tests/lib/ensure_iterability_es6.js
+++ /dev/null
@@ -1,25 +0,0 @@
-"use strict";
-
-const assert = require( "assert" );
-
-const ensureIterability = () => {
- const { JSDOM } = require( "jsdom" );
-
- const { window } = new JSDOM( "" );
-
- let i;
- const ensureJQuery = require( "./ensure_jquery" );
- const jQuery = require( "../../../dist/jquery.js" )( window );
- const elem = jQuery( "<div></div><span></span><a></a>" );
- let result = "";
-
- ensureJQuery( jQuery );
-
- for ( i of elem ) {
- result += i.nodeName;
- }
-
- assert.strictEqual( result, "DIVSPANA", "for-of works on jQuery objects" );
-};
-
-module.exports = ensureIterability;
diff --git a/test/node_smoke_tests/module/.eslintrc.json b/test/node_smoke_tests/module/.eslintrc.json
new file mode 100644
index 000000000..3a13e1732
--- /dev/null
+++ b/test/node_smoke_tests/module/.eslintrc.json
@@ -0,0 +1,13 @@
+{
+ "root": true,
+
+ "extends": "../../../.eslintrc-node.json",
+
+ "parserOptions": {
+ "ecmaVersion": 2022,
+ "sourceType": "module"
+ },
+ "env": {
+ "es2022": true
+ }
+}
diff --git a/test/node_smoke_tests/module/document_missing.js b/test/node_smoke_tests/module/document_missing.js
new file mode 100644
index 000000000..9394af796
--- /dev/null
+++ b/test/node_smoke_tests/module/document_missing.js
@@ -0,0 +1,13 @@
+import assert from "node:assert";
+
+import { ensureGlobalNotCreated } from "./lib/ensure_global_not_created.js";
+import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js";
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+const { default: jQueryFactory } = await import( jQueryModuleSpecifier );
+
+assert.throws( () => {
+ jQueryFactory( {} );
+}, /jQuery requires a window with a document/ );
+
+ensureGlobalNotCreated();
diff --git a/test/node_smoke_tests/module/document_passed.js b/test/node_smoke_tests/module/document_passed.js
new file mode 100644
index 000000000..7af5a7972
--- /dev/null
+++ b/test/node_smoke_tests/module/document_passed.js
@@ -0,0 +1,14 @@
+import { JSDOM } from "jsdom";
+
+import { ensureJQuery } from "./lib/ensure_jquery.js";
+import { ensureGlobalNotCreated } from "./lib/ensure_global_not_created.js";
+import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js";
+
+const { window } = new JSDOM( "" );
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+const { default: jQueryFactory } = await import( jQueryModuleSpecifier );
+const jQuery = jQueryFactory( window );
+
+ensureJQuery( jQuery );
+ensureGlobalNotCreated();
diff --git a/test/node_smoke_tests/module/iterable_with_native_symbol.js b/test/node_smoke_tests/module/iterable_with_native_symbol.js
new file mode 100644
index 000000000..3417e8ac0
--- /dev/null
+++ b/test/node_smoke_tests/module/iterable_with_native_symbol.js
@@ -0,0 +1,12 @@
+import process from "node:process";
+
+import { ensureIterability } from "./lib/ensure_iterability_es6.js";
+import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js";
+
+if ( typeof Symbol === "undefined" ) {
+ console.log( "Symbols not supported, skipping the test..." );
+ process.exit();
+}
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+await ensureIterability( jQueryModuleSpecifier );
diff --git a/test/node_smoke_tests/module/lib/ensure_global_not_created.js b/test/node_smoke_tests/module/lib/ensure_global_not_created.js
new file mode 100644
index 000000000..d7648bd68
--- /dev/null
+++ b/test/node_smoke_tests/module/lib/ensure_global_not_created.js
@@ -0,0 +1,11 @@
+import assert from "node:assert";
+
+// Ensure the jQuery property on global/window/module "this"/etc. was not
+// created in a CommonJS environment.
+// `global` is always checked in addition to passed parameters.
+export const ensureGlobalNotCreated = ( ...args ) => {
+ [ ...args, global ].forEach( function( object ) {
+ assert.strictEqual( object.jQuery, undefined,
+ "A jQuery global was created in a module environment." );
+ } );
+};
diff --git a/test/node_smoke_tests/module/lib/ensure_iterability_es6.js b/test/node_smoke_tests/module/lib/ensure_iterability_es6.js
new file mode 100644
index 000000000..8893267a0
--- /dev/null
+++ b/test/node_smoke_tests/module/lib/ensure_iterability_es6.js
@@ -0,0 +1,21 @@
+import assert from "node:assert";
+const { JSDOM } = await import( "jsdom" );
+
+const { ensureJQuery } = await import( "./ensure_jquery.js" );
+
+export const ensureIterability = async( jQueryModuleSpecifier ) => {
+ const { window } = new JSDOM( "" );
+
+ const { default: jQueryFactory } = await import( jQueryModuleSpecifier );
+ const jQuery = jQueryFactory( window );
+ const elem = jQuery( "<div></div><span></span><a></a>" );
+
+ ensureJQuery( jQuery );
+
+ let result = "";
+ for ( const node of elem ) {
+ result += node.nodeName;
+ }
+
+ assert.strictEqual( result, "DIVSPANA", "for-of works on jQuery objects" );
+};
diff --git a/test/node_smoke_tests/module/lib/ensure_jquery.js b/test/node_smoke_tests/module/lib/ensure_jquery.js
new file mode 100644
index 000000000..d07624ebd
--- /dev/null
+++ b/test/node_smoke_tests/module/lib/ensure_jquery.js
@@ -0,0 +1,7 @@
+import assert from "node:assert";
+
+// Check if the object we got is the jQuery object by invoking a basic API.
+export const ensureJQuery = ( jQuery ) => {
+ assert( /^jQuery/.test( jQuery.expando ),
+ "jQuery.expando was not detected, the jQuery bootstrap process has failed" );
+};
diff --git a/test/node_smoke_tests/module/lib/jquery-module-specifier.js b/test/node_smoke_tests/module/lib/jquery-module-specifier.js
new file mode 100644
index 000000000..39aff9fd4
--- /dev/null
+++ b/test/node_smoke_tests/module/lib/jquery-module-specifier.js
@@ -0,0 +1,21 @@
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const dirname = path.dirname( fileURLToPath( import.meta.url ) );
+
+const ROOT_DIR = path.resolve( dirname, "..", "..", "..", ".." );
+
+// If `jQueryModuleSpecifier` is a real relative path, make it absolute
+// to make sure it resolves to the same file inside utils from
+// a subdirectory. Otherwise, leave it as-is as we may be testing `exports`
+// so we need input as-is.
+export const getJQueryModuleSpecifier = () => {
+ const jQueryModuleInputSpecifier = process.argv[ 2 ];
+ if ( !jQueryModuleInputSpecifier ) {
+ throw new Error( "jQuery module specifier not passed" );
+ }
+
+ return jQueryModuleInputSpecifier.startsWith( "." ) ?
+ path.resolve( ROOT_DIR, jQueryModuleInputSpecifier ) :
+ jQueryModuleInputSpecifier;
+};
diff --git a/test/node_smoke_tests/module/window_present_originally.js b/test/node_smoke_tests/module/window_present_originally.js
new file mode 100644
index 000000000..ab60947e8
--- /dev/null
+++ b/test/node_smoke_tests/module/window_present_originally.js
@@ -0,0 +1,17 @@
+import { JSDOM } from "jsdom";
+
+import { ensureJQuery } from "./lib/ensure_jquery.js";
+import { ensureGlobalNotCreated } from "./lib/ensure_global_not_created.js";
+import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js";
+
+const jQueryModuleSpecifier = getJQueryModuleSpecifier();
+
+const { window } = new JSDOM( "" );
+
+// Set the window global.
+globalThis.window = window;
+
+const { default: jQuery } = await import( jQueryModuleSpecifier );
+
+ensureJQuery( jQuery );
+ensureGlobalNotCreated( window );