Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

test: add test for `-R import-only-loader` (#5391)

Chengzhong Wu6ee5b483b8c2
diff --git a/.knip.jsonc b/.knip.jsonc
index 3b19b3e6..5e6fdf78 100644
--- a/.knip.jsonc
+++ b/.knip.jsonc
@@ -18,6 +18,7 @@
],
"ignoreDependencies": [
"@mocha/docdash",
+ "@test/esm-only-loader",
"coffeescript",
"fake",
"jsdoc-ts-utils",
diff --git a/package-lock.json b/package-lock.json
index 888eb28e..d877a238 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -44,6 +44,7 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-multi-entry": "^4.0.1",
"@rollup/plugin-node-resolve": "^13.1.3",
+ "@test/esm-only-loader": "./test/compiler-fixtures/esm-only-loader",
"chai": "^4.3.4",
"coffeescript": "^2.6.1",
"cross-env": "^7.0.2",
@@ -1626,6 +1627,10 @@
"integrity": "sha512-+9jVqKhRSpsc591z5vX+X5Yyw+he/HCB4iQ/RYxw35CEPaY1gnsNE43nf9n9AaYjAQrTiI/mOwKUKdUs9vf7Xg==",
"dev": true
},
+ "node_modules/@test/esm-only-loader": {
+ "resolved": "test/compiler-fixtures/esm-only-loader",
+ "link": true
+ },
"node_modules/@types/aria-query": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/@types/aria-query/-/aria-query-5.0.3.tgz",
@@ -16597,6 +16602,10 @@
"type": "github",
"url": "https://github.com/sponsors/wooorm"
}
+ },
+ "test/compiler-fixtures/esm-only-loader": {
+ "name": "@test/esm-only-loader",
+ "dev": true
}
},
"dependencies": {
@@ -17732,6 +17741,9 @@
"integrity": "sha512-+9jVqKhRSpsc591z5vX+X5Yyw+he/HCB4iQ/RYxw35CEPaY1gnsNE43nf9n9AaYjAQrTiI/mOwKUKdUs9vf7Xg==",
"dev": true
},
+ "@test/esm-only-loader": {
+ "version": "file:test/compiler-fixtures/esm-only-loader"
+ },
"@types/aria-query": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/@types/aria-query/-/aria-query-5.0.3.tgz",
diff --git a/package.json b/package.json
index 9c1e24e8..b67f1e8e 100644
--- a/package.json
+++ b/package.json
@@ -127,6 +127,7 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-multi-entry": "^4.0.1",
"@rollup/plugin-node-resolve": "^13.1.3",
+ "@test/esm-only-loader": "./test/compiler-fixtures/esm-only-loader",
"chai": "^4.3.4",
"coffeescript": "^2.6.1",
"cross-env": "^7.0.2",
diff --git a/test/compiler-fixtures/esm-only-loader/README.md b/test/compiler-fixtures/esm-only-loader/README.md
new file mode 100644
index 00000000..33084a08
--- /dev/null
+++ b/test/compiler-fixtures/esm-only-loader/README.md
@@ -0,0 +1,14 @@
+Test package used to cover a loader package that is only defined with `exports.imports` in the `package.json`.
+
+This must be linked to `node_modules` and be `require`/`import`-ed with a bare specifier, as
+defined in algorithm [`ESM_RESOLVE`][] step 5.2:
+
+```
+ESM_RESOLVE(specifier, parentURL)
+ ...
+ 5. Otherwise,
+ 1. Note: specifier is now a bare specifier.
+ 2. Set resolved the result of PACKAGE_RESOLVE(specifier, parentURL).
+```
+
+[`ESM_RESOLVE`]: https://nodejs.org/api/esm.html?fts_query=algorithm#resolution-algorithm-specification
diff --git a/test/compiler-fixtures/esm-only-loader/esm-loader.fixture.mjs b/test/compiler-fixtures/esm-only-loader/esm-loader.fixture.mjs
new file mode 100644
index 00000000..45c0a3ff
--- /dev/null
+++ b/test/compiler-fixtures/esm-only-loader/esm-loader.fixture.mjs
@@ -0,0 +1,15 @@
+import fsPromises from 'node:fs/promises';
+import { fileURLToPath } from 'node:url';
+
+export async function load(url, context, nextLoad) {
+ if (!url.includes('compiler-esm') && !url.includes('compiler-cjs')) {
+ return nextLoad(url, context);
+ }
+ const format = url.includes('compiler-esm') ? 'module' : 'commonjs';
+ const content = await fsPromises.readFile(fileURLToPath(url + '.compiled'), 'utf8');
+ return {
+ format,
+ source: content,
+ shortCircuit: true,
+ }
+}
diff --git a/test/compiler-fixtures/esm-only-loader/esm.fixture.js b/test/compiler-fixtures/esm-only-loader/esm.fixture.js
new file mode 100644
index 00000000..33ed3ba5
--- /dev/null
+++ b/test/compiler-fixtures/esm-only-loader/esm.fixture.js
@@ -0,0 +1,3 @@
+import Module from 'node:module';
+
+Module.register(new URL('./esm-loader.fixture.mjs', import.meta.url));
diff --git a/test/compiler-fixtures/esm-only-loader/package.json b/test/compiler-fixtures/esm-only-loader/package.json
new file mode 100644
index 00000000..314f3e6d
--- /dev/null
+++ b/test/compiler-fixtures/esm-only-loader/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "@test/esm-only-loader",
+ "type": "module",
+ "exports": {
+ ".": {
+ "import": "./esm.fixture.js"
+ }
+ }
+}
diff --git a/test/integration/compiler-esm-only-loader.spec.js b/test/integration/compiler-esm-only-loader.spec.js
new file mode 100644
index 00000000..c1c1e49f
--- /dev/null
+++ b/test/integration/compiler-esm-only-loader.spec.js
@@ -0,0 +1,71 @@
+'use strict';
+
+var exec = require('node:child_process').exec;
+var path = require('node:path');
+
+// Regression test for https://github.com/mochajs/mocha/issues/5380
+describe('support ESM only module loader packages', function () {
+ it('should support ESM .js extension', function (done) {
+ exec(
+ '"' +
+ process.execPath +
+ '" "' +
+ path.join('bin', 'mocha') +
+ '" -R json --require "@test/esm-only-loader" "test/compiler-esm/*.js"',
+ {cwd: path.join(__dirname, '..', '..')},
+ function (error, stdout) {
+ if (error && !stdout) {
+ return done(error);
+ }
+ var results = JSON.parse(stdout);
+ expect(results, 'to have property', 'tests');
+ var titles = [];
+ for (var index = 0; index < results.tests.length; index += 1) {
+ expect(results.tests[index], 'to have property', 'fullTitle');
+ titles.push(results.tests[index].fullTitle);
+ }
+ expect(
+ titles,
+ 'to contain',
+ 'esm written in esm should work',
+ ).and(
+ 'to contain',
+ 'esm written in esm with top-level-await should work',
+ ).and('to have length', 2);
+ done();
+ }
+ );
+ });
+
+ it('should support ESM .ts extension', function (done) {
+ exec(
+ '"' +
+ process.execPath +
+ '" "' +
+ path.join('bin', 'mocha') +
+ '" -R json --require "@test/esm-only-loader" "test/compiler-esm/*.ts"',
+ {cwd: path.join(__dirname, '..', '..')},
+ function (error, stdout) {
+ if (error && !stdout) {
+ return done(error);
+ }
+ var results = JSON.parse(stdout);
+ expect(results, 'to have property', 'tests');
+ var titles = [];
+ for (var index = 0; index < results.tests.length; index += 1) {
+ expect(results.tests[index], 'to have property', 'fullTitle');
+ titles.push(results.tests[index].fullTitle);
+ }
+ expect(
+ titles,
+ 'to contain',
+ 'esm written in esm should work',
+ ).and(
+ 'to contain',
+ 'esm written in esm with top-level-await should work',
+ ).and('to have length', 2);
+ done();
+ }
+ );
+ });
+});