Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Jun 29, 2024
1 parent f225058 commit 28c75eb
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 59 deletions.
3 changes: 1 addition & 2 deletions test/es-module/test-esm-cjs-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ describe('ESM: importing CJS', { concurrency: !process.env.TEST_PARALLEL }, () =
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
const { code, signal, stderr } = await spawnPromisified(execPath, [invalidEntry]);

assert.match(stderr, /SyntaxError: The requested module '\.\/invalid-cjs\.js' does not provide an export named 'default'/);
assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.ok(stderr.includes('Warning: To load an ES module'));
assert.ok(stderr.includes('Unexpected token \'export\''));
});
});
2 changes: 1 addition & 1 deletion test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
]);

strictEqual(stderr, '');
strictEqual(stdout, 'null\nexecuted\n');
strictEqual(stdout, 'undefined\nexecuted\n');
strictEqual(code, 0);
strictEqual(signal, null);

Expand Down
6 changes: 3 additions & 3 deletions test/es-module/test-esm-extensionless-esm-and-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ describe('extensionless ES modules within no package scope', { concurrency: !pro
fixtures.path('es-modules/noext-esm'),
]);

match(stderr, /SyntaxError/);
strictEqual(stdout, '');
strictEqual(code, 1);
strictEqual(stdout, 'executed\n');
strictEqual(stderr, '');
strictEqual(code, 0);
strictEqual(signal, null);
});

Expand Down
4 changes: 2 additions & 2 deletions test/es-module/test-esm-import-flag.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ describe('import modules using --import', { concurrency: !process.env.TEST_PARAL
]
);

assert.match(stderr, /SyntaxError: Unexpected token 'export'/);
assert.strictEqual(stderr, '');
assert.match(stdout, /^\.mjs file\r?\n$/);
assert.strictEqual(code, 1);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

Expand Down
29 changes: 8 additions & 21 deletions test/es-module/test-esm-loader-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,15 @@ describe('Loader hooks', { concurrency: !process.env.TEST_PARALLEL }, () => {
'--no-warnings',
'--experimental-loader',
`data:text/javascript,import{readFile}from"node:fs/promises";import{fileURLToPath}from"node:url";export ${
async function load(u, c, n) {
const r = await n(u, c);
if (u.endsWith('/common/index.js')) {
r.source = '"use strict";module.exports=require("node:module").createRequire(' +
`${JSON.stringify(u)})(${JSON.stringify(fileURLToPath(u))});\n`;
} else if (c.format === 'commonjs') {
r.source = await readFile(new URL(u));
async function load(url, context, nextLoad) {
const result = await nextLoad(url, context);
if (url.endsWith('/common/index.js')) {
result.source = '"use strict";module.exports=require("node:module").createRequire(' +
`${JSON.stringify(url)})(${JSON.stringify(fileURLToPath(url))});\n`;
} else if (url.startsWith('file:') && (context.format === undefined || context.format === 'commonjs')) {
result.source = await readFile(new URL(url));
}
return r;
return result;
}}`,
'--experimental-loader',
fixtures.fileURL('es-module-loaders/loader-resolve-passthru.mjs'),
Expand All @@ -772,19 +772,6 @@ describe('Loader hooks', { concurrency: !process.env.TEST_PARALLEL }, () => {
assert.strictEqual(signal, null);
});

describe('should use hooks', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--import',
fixtures.fileURL('es-module-loaders/builtin-named-exports.mjs'),
fixtures.path('es-modules/require-esm-throws-with-loaders.js'),
]);

assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should support source maps in commonjs translator', async () => {
const readFile = async () => {};
const hook = `
Expand Down
12 changes: 6 additions & 6 deletions test/es-module/test-esm-resolve-type.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ try {
[ '/es-modules/package-ends-node_modules/index.js', 'module' ],
[ '/es-modules/package-type-module/index.js', 'module' ],
[ '/es-modules/package-type-commonjs/index.js', 'commonjs' ],
[ '/es-modules/package-without-type/index.js', 'commonjs' ],
[ '/es-modules/package-without-pjson/index.js', 'commonjs' ],
[ '/es-modules/package-without-type/index.js', undefined ],
[ '/es-modules/package-without-pjson/index.js', undefined ],
].forEach(([ testScript, expectedType ]) => {
const resolvedPath = path.resolve(fixtures.path(testScript));
const resolveResult = resolve(url.pathToFileURL(resolvedPath));
Expand All @@ -55,11 +55,11 @@ try {
*
* for test-module-ne: everything .js that is not 'module' is 'commonjs'
*/
for (const [ moduleName, moduleExtenstion, moduleType, expectedResolvedType ] of
for (const [ moduleName, moduleExtension, moduleType, expectedResolvedType ] of
[ [ 'test-module-mainjs', 'js', 'module', 'module'],
[ 'test-module-mainmjs', 'mjs', 'module', 'module'],
[ 'test-module-cjs', 'js', 'commonjs', 'commonjs'],
[ 'test-module-ne', 'js', undefined, 'commonjs'],
[ 'test-module-ne', 'js', undefined, undefined],
]) {
process.chdir(previousCwd);
tmpdir.refresh();
Expand All @@ -73,14 +73,14 @@ try {
const mDir = rel(`node_modules/${moduleName}`);
const subDir = rel(`node_modules/${moduleName}/subdir`);
const pkg = rel(`node_modules/${moduleName}/package.json`);
const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtenstion}`);
const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtension}`);

createDir(nmDir);
createDir(mDir);
createDir(subDir);
const pkgJsonContent = {
...(moduleType !== undefined) && { type: moduleType },
main: `subdir/mainfile.${moduleExtenstion}`
main: `subdir/mainfile.${moduleExtension}`
};
fs.writeFileSync(pkg, JSON.stringify(pkgJsonContent));
fs.writeFileSync(script,
Expand Down
16 changes: 1 addition & 15 deletions test/es-module/test-require-module-implicit.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
// Flags: --experimental-require-module
'use strict';

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

assert.throws(() => {
require('../fixtures/es-modules/package-without-type/noext-esm');
}, {
message: /Unexpected token 'export'/
});

assert.throws(() => {
require('../fixtures/es-modules/loose.js');
}, {
message: /Unexpected token 'export'/
});

{
// .mjs should not be matched as default extensions.
const id = '../fixtures/es-modules/should-not-be-resolved';
assert.throws(() => {
require(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function load(url, context, next) {
source: generateBuiltinModule(urlObj.pathname),
format: 'commonjs',
};
} else if (context.format === 'commonjs') {
} else if (context.format === undefined || context.format === 'commonjs') {
return {
shortCircuit: true,
source: readFileSync(new URL(url)),
Expand Down
8 changes: 0 additions & 8 deletions test/fixtures/es-modules/require-esm-throws-with-loaders.js

This file was deleted.

0 comments on commit 28c75eb

Please sign in to comment.