Skip to content

Commit 8fd5146

Browse files
.
1 parent 9709547 commit 8fd5146

File tree

1 file changed

+79
-60
lines changed

1 file changed

+79
-60
lines changed
Lines changed: 79 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,81 @@
1-
// Flags: --experimental-specifier-resolution=node
2-
import { mustNotCall } from '../common/index.mjs';
3-
import assert from 'assert';
4-
import path from 'path';
5-
import { spawn } from 'child_process';
6-
import { fileURLToPath } from 'url';
7-
8-
// commonJS index.js
9-
import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs';
10-
// esm index.js
11-
import module from '../fixtures/es-module-specifiers/package-type-module';
12-
// Notice the trailing slash
13-
import success, { explicit, implicit, implicitModule, getImplicitCommonjs }
14-
from '../fixtures/es-module-specifiers/';
15-
16-
assert.strictEqual(commonjs, 'commonjs');
17-
assert.strictEqual(module, 'module');
18-
assert.strictEqual(success, 'success');
19-
assert.strictEqual(explicit, 'esm');
20-
assert.strictEqual(implicit, 'cjs');
21-
assert.strictEqual(implicitModule, 'cjs');
22-
23-
async function main() {
24-
try {
25-
await import('../fixtures/es-module-specifiers/do-not-exist.js');
26-
} catch (e) {
27-
// Files that do not exist should throw
28-
assert.strictEqual(e.name, 'Error');
29-
}
30-
try {
31-
await getImplicitCommonjs();
32-
} catch (e) {
33-
// Legacy loader cannot resolve .mjs automatically from main
34-
assert.strictEqual(e.name, 'Error');
35-
}
36-
}
37-
38-
main().catch(mustNotCall);
39-
40-
// Test path from command line arguments
41-
[
42-
'package-type-commonjs',
43-
'package-type-module',
44-
'/',
45-
'/index',
46-
].forEach((item) => {
47-
const modulePath = path.join(
48-
fileURLToPath(import.meta.url),
49-
'../../fixtures/es-module-specifiers',
50-
item,
51-
);
52-
[
53-
'--experimental-specifier-resolution',
54-
'--es-module-specifier-resolution',
55-
].forEach((option) => {
56-
spawn(process.execPath,
57-
[`${option}=node`, modulePath],
58-
{ stdio: 'inherit' }).on('exit', (code) => {
59-
assert.strictEqual(code, 0);
60-
});
1+
import '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { match, strictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
6+
7+
import spawn from './helper.spawnAsPromised.mjs';
8+
9+
10+
describe('ESM: specifier-resolution=node', { concurrency: true }, () => {
11+
it(async () => {
12+
const { code, stderr, stdout } = await spawn(execPath, [
13+
'--no-warnings',
14+
'--experimental-specifier-resolution=node',
15+
'--input-type=module',
16+
'--eval',
17+
[
18+
'import { strictEqual } from "node:assert";',
19+
// commonJS index.js
20+
`import commonjs from "${fixtures.path('es-module-specifiers/package-type-commonjs')}";`,
21+
// esm index.js
22+
`import module from "${fixtures.path('es-module-specifiers/package-type-module')}";`,
23+
// Notice the trailing slash
24+
`import success, { explicit, implicit, implicitModule } from "${fixtures.path('es-module-specifiers/')}";`,
25+
'strictEqual(commonjs, "commonjs");',
26+
'strictEqual(module, "module");',
27+
'strictEqual(success, "success");',
28+
'strictEqual(explicit, "esm");',
29+
'strictEqual(implicit, "cjs");',
30+
'strictEqual(implicitModule, "cjs");',
31+
].join('\n')
32+
]);
33+
34+
strictEqual(stderr, '');
35+
strictEqual(stdout, '');
36+
strictEqual(code, 0);
37+
});
38+
39+
it('should throw when the file doesn\'t exist', async () => {
40+
const { code, stderr, stdout } = await spawn(execPath, [
41+
'--no-warnings',
42+
fixtures.path('es-module-specifiers/do-not-exist.js'),
43+
]);
44+
45+
match(stderr, /Cannot find module/);
46+
strictEqual(stdout, '');
47+
strictEqual(code, 1);
48+
});
49+
50+
it('should throw when the omitted file extension is .mjs (legacy loader doesn\'t support it)', async () => {
51+
const { code, stderr, stdout } = await spawn(execPath, [
52+
'--no-warnings',
53+
'--experimental-specifier-resolution=node',
54+
'--input-type=module',
55+
'--eval',
56+
`import whatever from "${fixtures.path('es-module-specifiers/implicit-main-type-commonjs')}";`,
57+
]);
58+
59+
match(stderr, /ERR_MODULE_NOT_FOUND/);
60+
strictEqual(stdout, '');
61+
strictEqual(code, 1);
62+
});
63+
64+
for (
65+
const item of [
66+
'package-type-commonjs',
67+
'package-type-module',
68+
'/',
69+
'/index',
70+
]
71+
) it('should ', async () => {
72+
const { code } = await spawn(execPath, [
73+
'--no-warnings',
74+
'--experimental-specifier-resolution=node',
75+
'--es-module-specifier-resolution=node',
76+
fixtures.path('es-module-specifiers', item),
77+
]);
78+
79+
strictEqual(code, 0);
6180
});
6281
});

0 commit comments

Comments
 (0)