Skip to content

Commit 138f787

Browse files
committed
update tests
1 parent 9f4819a commit 138f787

8 files changed

+26
-47
lines changed

test/es-module/test-esm-cjs-exports.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ describe('ESM: importing CJS', { concurrency: !process.env.TEST_PARALLEL }, () =
2121
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
2222
const { code, signal, stderr } = await spawnPromisified(execPath, [invalidEntry]);
2323

24+
assert.match(stderr, /SyntaxError: The requested module '\.\/invalid-cjs\.js' does not provide an export named 'default'/);
2425
assert.strictEqual(code, 1);
2526
assert.strictEqual(signal, null);
26-
assert.ok(stderr.includes('Warning: To load an ES module'));
27-
assert.ok(stderr.includes('Unexpected token \'export\''));
2827
});
2928
});

test/es-module/test-esm-detect-ambiguous.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
185185
]);
186186

187187
strictEqual(stderr, '');
188-
strictEqual(stdout, 'null\nexecuted\n');
188+
strictEqual(stdout, 'undefined\nexecuted\n');
189189
strictEqual(code, 0);
190190
strictEqual(signal, null);
191191

test/es-module/test-esm-extensionless-esm-and-wasm.mjs

+6-12
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,19 @@ describe('extensionless Wasm modules within a "type": "module" package scope', {
6060
});
6161

6262
describe('extensionless ES modules within no package scope', { concurrency: !process.env.TEST_PARALLEL }, () => {
63-
// This succeeds with `--experimental-default-type=module`
64-
it('should error as the entry point', async () => {
63+
it('should run as the entry point', async () => {
6564
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
6665
fixtures.path('es-modules/noext-esm'),
6766
]);
6867

69-
match(stderr, /SyntaxError/);
70-
strictEqual(stdout, '');
71-
strictEqual(code, 1);
68+
strictEqual(stdout, 'executed\n');
69+
strictEqual(stderr, '');
70+
strictEqual(code, 0);
7271
strictEqual(signal, null);
7372
});
7473

75-
// This succeeds with `--experimental-default-type=module`
76-
it('should error on import', async () => {
77-
try {
78-
await import(fixtures.fileURL('es-modules/noext-esm'));
79-
mustNotCall();
80-
} catch (err) {
81-
ok(err instanceof SyntaxError);
74+
it('should run on import', async () => {
75+
await import(fixtures.fileURL('es-modules/noext-esm'));
8276
}
8377
});
8478
});

test/es-module/test-esm-import-flag.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ describe('import modules using --import', { concurrency: !process.env.TEST_PARAL
146146
]
147147
);
148148

149-
assert.match(stderr, /SyntaxError: Unexpected token 'export'/);
149+
assert.strictEqual(stderr, '');
150150
assert.match(stdout, /^\.mjs file\r?\n$/);
151-
assert.strictEqual(code, 1);
151+
assert.strictEqual(code, 0);
152152
assert.strictEqual(signal, null);
153153
});
154154

test/es-module/test-esm-loader-hooks.mjs

+8-8
Original file line numberDiff line numberDiff line change
@@ -751,15 +751,15 @@ describe('Loader hooks', { concurrency: !process.env.TEST_PARALLEL }, () => {
751751
'--no-warnings',
752752
'--experimental-loader',
753753
`data:text/javascript,import{readFile}from"node:fs/promises";import{fileURLToPath}from"node:url";export ${
754-
async function load(u, c, n) {
755-
const r = await n(u, c);
756-
if (u.endsWith('/common/index.js')) {
757-
r.source = '"use strict";module.exports=require("node:module").createRequire(' +
758-
`${JSON.stringify(u)})(${JSON.stringify(fileURLToPath(u))});\n`;
759-
} else if (c.format === 'commonjs') {
760-
r.source = await readFile(new URL(u));
754+
async function load(url, context, nextLoad) {
755+
const result = await nextLoad(url, context);
756+
if (url.endsWith('/common/index.js')) {
757+
result.source = '"use strict";module.exports=require("node:module").createRequire(' +
758+
`${JSON.stringify(url)})(${JSON.stringify(fileURLToPath(url))});\n`;
759+
} else if (url.startsWith('file:') && (context.format === undefined || context.format === 'commonjs')) {
760+
result.source = await readFile(new URL(url));
761761
}
762-
return r;
762+
return result;
763763
}}`,
764764
'--experimental-loader',
765765
fixtures.fileURL('es-module-loaders/loader-resolve-passthru.mjs'),

test/es-module/test-esm-resolve-type.mjs

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ try {
4141
[ '/es-modules/package-ends-node_modules/index.js', 'module' ],
4242
[ '/es-modules/package-type-module/index.js', 'module' ],
4343
[ '/es-modules/package-type-commonjs/index.js', 'commonjs' ],
44-
[ '/es-modules/package-without-type/index.js', 'commonjs' ],
45-
[ '/es-modules/package-without-pjson/index.js', 'commonjs' ],
44+
[ '/es-modules/package-without-type/index.js', undefined ],
45+
[ '/es-modules/package-without-pjson/index.js', undefined ],
4646
].forEach(([ testScript, expectedType ]) => {
4747
const resolvedPath = path.resolve(fixtures.path(testScript));
4848
const resolveResult = resolve(url.pathToFileURL(resolvedPath));
@@ -55,11 +55,11 @@ try {
5555
*
5656
* for test-module-ne: everything .js that is not 'module' is 'commonjs'
5757
*/
58-
for (const [ moduleName, moduleExtenstion, moduleType, expectedResolvedType ] of
58+
for (const [ moduleName, moduleExtension, moduleType, expectedResolvedType ] of
5959
[ [ 'test-module-mainjs', 'js', 'module', 'module'],
6060
[ 'test-module-mainmjs', 'mjs', 'module', 'module'],
6161
[ 'test-module-cjs', 'js', 'commonjs', 'commonjs'],
62-
[ 'test-module-ne', 'js', undefined, 'commonjs'],
62+
[ 'test-module-ne', 'js', undefined, undefined],
6363
]) {
6464
process.chdir(previousCwd);
6565
tmpdir.refresh();
@@ -73,14 +73,14 @@ try {
7373
const mDir = rel(`node_modules/${moduleName}`);
7474
const subDir = rel(`node_modules/${moduleName}/subdir`);
7575
const pkg = rel(`node_modules/${moduleName}/package.json`);
76-
const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtenstion}`);
76+
const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtension}`);
7777

7878
createDir(nmDir);
7979
createDir(mDir);
8080
createDir(subDir);
8181
const pkgJsonContent = {
8282
...(moduleType !== undefined) && { type: moduleType },
83-
main: `subdir/mainfile.${moduleExtenstion}`
83+
main: `subdir/mainfile.${moduleExtension}`
8484
};
8585
fs.writeFileSync(pkg, JSON.stringify(pkgJsonContent));
8686
fs.writeFileSync(script,

test/es-module/test-require-module-implicit.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
// Flags: --experimental-require-module
22
'use strict';
33

4-
// Tests that require()ing modules without explicit module type information
5-
// warns and errors.
4+
// Tests that require()ing .mjs should not be matched as default extensions.
65
require('../common');
76
const assert = require('assert');
87
const { isModuleNamespaceObject } = require('util/types');
98

10-
assert.throws(() => {
11-
require('../fixtures/es-modules/package-without-type/noext-esm');
12-
}, {
13-
message: /Unexpected token 'export'/
14-
});
15-
16-
assert.throws(() => {
17-
require('../fixtures/es-modules/loose.js');
18-
}, {
19-
message: /Unexpected token 'export'/
20-
});
21-
229
{
23-
// .mjs should not be matched as default extensions.
2410
const id = '../fixtures/es-modules/should-not-be-resolved';
2511
assert.throws(() => {
2612
require(id);

test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function load(url, context, next) {
2828
source: generateBuiltinModule(urlObj.pathname),
2929
format: 'commonjs',
3030
};
31-
} else if (context.format === 'commonjs') {
31+
} else if (context.format === undefined || context.format === 'commonjs') {
3232
return {
3333
shortCircuit: true,
3434
source: readFileSync(new URL(url)),

0 commit comments

Comments
 (0)