Skip to content

Commit 6ee5b48

Browse files
authored
test: add test for -R import-only-loader (#5391)
1 parent c12d85d commit 6ee5b48

File tree

8 files changed

+126
-0
lines changed

8 files changed

+126
-0
lines changed

.knip.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"ignoreDependencies": [
2020
"@mocha/docdash",
21+
"@test/esm-only-loader",
2122
"coffeescript",
2223
"fake",
2324
"jsdoc-ts-utils",

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
"@rollup/plugin-json": "^4.1.0",
128128
"@rollup/plugin-multi-entry": "^4.0.1",
129129
"@rollup/plugin-node-resolve": "^13.1.3",
130+
"@test/esm-only-loader": "./test/compiler-fixtures/esm-only-loader",
130131
"chai": "^4.3.4",
131132
"coffeescript": "^2.6.1",
132133
"cross-env": "^7.0.2",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Test package used to cover a loader package that is only defined with `exports.imports` in the `package.json`.
2+
3+
This must be linked to `node_modules` and be `require`/`import`-ed with a bare specifier, as
4+
defined in algorithm [`ESM_RESOLVE`][] step 5.2:
5+
6+
```
7+
ESM_RESOLVE(specifier, parentURL)
8+
...
9+
5. Otherwise,
10+
1. Note: specifier is now a bare specifier.
11+
2. Set resolved the result of PACKAGE_RESOLVE(specifier, parentURL).
12+
```
13+
14+
[`ESM_RESOLVE`]: https://nodejs.org/api/esm.html?fts_query=algorithm#resolution-algorithm-specification
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import fsPromises from 'node:fs/promises';
2+
import { fileURLToPath } from 'node:url';
3+
4+
export async function load(url, context, nextLoad) {
5+
if (!url.includes('compiler-esm') && !url.includes('compiler-cjs')) {
6+
return nextLoad(url, context);
7+
}
8+
const format = url.includes('compiler-esm') ? 'module' : 'commonjs';
9+
const content = await fsPromises.readFile(fileURLToPath(url + '.compiled'), 'utf8');
10+
return {
11+
format,
12+
source: content,
13+
shortCircuit: true,
14+
}
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Module from 'node:module';
2+
3+
Module.register(new URL('./esm-loader.fixture.mjs', import.meta.url));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@test/esm-only-loader",
3+
"type": "module",
4+
"exports": {
5+
".": {
6+
"import": "./esm.fixture.js"
7+
}
8+
}
9+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
var exec = require('node:child_process').exec;
4+
var path = require('node:path');
5+
6+
// Regression test for https://github.com/mochajs/mocha/issues/5380
7+
describe('support ESM only module loader packages', function () {
8+
it('should support ESM .js extension', function (done) {
9+
exec(
10+
'"' +
11+
process.execPath +
12+
'" "' +
13+
path.join('bin', 'mocha') +
14+
'" -R json --require "@test/esm-only-loader" "test/compiler-esm/*.js"',
15+
{cwd: path.join(__dirname, '..', '..')},
16+
function (error, stdout) {
17+
if (error && !stdout) {
18+
return done(error);
19+
}
20+
var results = JSON.parse(stdout);
21+
expect(results, 'to have property', 'tests');
22+
var titles = [];
23+
for (var index = 0; index < results.tests.length; index += 1) {
24+
expect(results.tests[index], 'to have property', 'fullTitle');
25+
titles.push(results.tests[index].fullTitle);
26+
}
27+
expect(
28+
titles,
29+
'to contain',
30+
'esm written in esm should work',
31+
).and(
32+
'to contain',
33+
'esm written in esm with top-level-await should work',
34+
).and('to have length', 2);
35+
done();
36+
}
37+
);
38+
});
39+
40+
it('should support ESM .ts extension', function (done) {
41+
exec(
42+
'"' +
43+
process.execPath +
44+
'" "' +
45+
path.join('bin', 'mocha') +
46+
'" -R json --require "@test/esm-only-loader" "test/compiler-esm/*.ts"',
47+
{cwd: path.join(__dirname, '..', '..')},
48+
function (error, stdout) {
49+
if (error && !stdout) {
50+
return done(error);
51+
}
52+
var results = JSON.parse(stdout);
53+
expect(results, 'to have property', 'tests');
54+
var titles = [];
55+
for (var index = 0; index < results.tests.length; index += 1) {
56+
expect(results.tests[index], 'to have property', 'fullTitle');
57+
titles.push(results.tests[index].fullTitle);
58+
}
59+
expect(
60+
titles,
61+
'to contain',
62+
'esm written in esm should work',
63+
).and(
64+
'to contain',
65+
'esm written in esm with top-level-await should work',
66+
).and('to have length', 2);
67+
done();
68+
}
69+
);
70+
});
71+
});

0 commit comments

Comments
 (0)