esm: refactor esm tests out of test/message

PR-URL: https://github.com/nodejs/node/pull/41352
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Geoffrey Booth 2022-01-06 03:07:52 -08:00 committed by GitHub
parent 0b4e9ae656
commit 8dd0658e87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 210 additions and 241 deletions

View file

@ -2,6 +2,7 @@
const path = require('path');
const fs = require('fs');
const { pathToFileURL } = require('url');
const fixturesDir = path.join(__dirname, '..', 'fixtures');
@ -9,6 +10,10 @@ function fixturesPath(...args) {
return path.join(fixturesDir, ...args);
}
function fixturesFileURL(...args) {
return pathToFileURL(fixturesPath(...args));
}
function readFixtureSync(args, enc) {
if (Array.isArray(args))
return fs.readFileSync(fixturesPath(...args), enc);
@ -26,6 +31,7 @@ function readFixtureKeys(enc, ...names) {
module.exports = {
fixturesDir,
path: fixturesPath,
fileURL: fixturesFileURL,
readSync: readFixtureSync,
readKey: readFixtureKey,
readKeys: readFixtureKeys,

View file

@ -3,6 +3,7 @@ import fixtures from './fixtures.js';
const {
fixturesDir,
path,
fileURL,
readSync,
readKey,
} = fixtures;
@ -10,6 +11,7 @@ const {
export {
fixturesDir,
path,
fileURL,
readSync,
readKey,
};

View file

@ -0,0 +1,39 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const importStatement =
'import { foo, notfound } from \'./module-named-exports.mjs\';';
const importStatementMultiline = `import {
foo,
notfound
} from './module-named-exports.mjs';
`;
[importStatement, importStatementMultiline].forEach((input) => {
const child = spawn(execPath, [
'--input-type=module',
'--eval',
input,
], {
cwd: path('es-module-loaders'),
});
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
// SyntaxError: The requested module './module-named-exports.mjs'
// does not provide an export named 'notfound'
match(stderr, /SyntaxError:/);
// The quotes ensure that the path starts with ./ and not ../
match(stderr, /'\.\/module-named-exports\.mjs'/);
match(stderr, /notfound/);
}));
});

View file

@ -0,0 +1,25 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const child = spawn(execPath, [
'--experimental-json-modules',
path('es-modules', 'import-json-named-export.mjs'),
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
// SyntaxError: The requested module '../experimental.json'
// does not provide an export named 'ofLife'
match(stderr, /SyntaxError:/);
match(stderr, /'\.\.\/experimental\.json'/);
match(stderr, /'ofLife'/);
}));

View file

@ -0,0 +1,27 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, ok, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const child = spawn(execPath, [
'--experimental-loader',
'i-dont-exist',
path('print-error-message.js'),
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
// Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist'
// imported from
match(stderr, /ERR_MODULE_NOT_FOUND/);
match(stderr, /'i-dont-exist'/);
ok(!stderr.includes('Bad command or file name'));
}));

View file

@ -0,0 +1,30 @@
import { mustCall } from '../common/index.mjs';
import { fileURL, path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const child = spawn(execPath, [
'--no-warnings',
'--throw-deprecation',
'--experimental-loader',
fileURL('es-module-loaders', 'hooks-obsolete.mjs').href,
path('print-error-message.js'),
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
// DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored:
// dynamicInstantiate, getFormat, getSource, transformSource
match(stderr, /DeprecationWarning:/);
match(stderr, /dynamicInstantiate/);
match(stderr, /getFormat/);
match(stderr, /getSource/);
match(stderr, /transformSource/);
}));

View file

@ -0,0 +1,24 @@
import { mustCall } from '../common/index.mjs';
import { fileURL, path } from '../common/fixtures.mjs';
import { match, ok, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const child = spawn(execPath, [
'--experimental-loader',
fileURL('es-module-loaders', 'syntax-error.mjs').href,
path('print-error-message.js'),
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
match(stderr, /SyntaxError:/);
ok(!stderr.includes('Bad command or file name'));
}));

View file

@ -0,0 +1,35 @@
import { mustCall } from '../common/index.mjs';
import { fixturesDir } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
[
{
input: 'import "./print-error-message"',
// Did you mean to import ../print-error-message.js?
expected: / \.\.\/print-error-message\.js\?/,
},
{
input: 'import obj from "some_module/obj"',
expected: / some_module\/obj\.js\?/,
},
].forEach(({ input, expected }) => {
const child = spawn(execPath, [
'--input-type=module',
'--eval',
input,
], {
cwd: fixturesDir,
});
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
match(stderr, expected);
}));
});

View file

@ -0,0 +1,19 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const child = spawn(execPath, [
path('es-module-loaders', 'syntax-error.mjs'),
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
match(stderr, /SyntaxError:/);
}));

View file

@ -1 +0,0 @@
import { foo, notfound } from './module-named-exports.mjs';

View file

@ -0,0 +1,2 @@
/* eslint-disable no-unused-vars */
import { ofLife } from '../experimental.json' assert { type: 'json' };

View file

@ -1,5 +0,0 @@
'use strict';
import obj from 'some_module/obj';
throw new Error('Should have errored');

1
test/fixtures/print-error-message.js vendored Normal file
View file

@ -0,0 +1 @@
console.error('Bad command or file name');

View file

@ -1,6 +0,0 @@
/* eslint-disable no-unused-vars */
import '../common/index.mjs';
import {
foo,
notfound
} from '../fixtures/es-module-loaders/module-named-exports.mjs';

View file

@ -1,12 +0,0 @@
file:///*/test/message/esm_display_syntax_error_import.mjs:5
notfound
^^^^^^^^
SyntaxError: The requested module '../fixtures/es-module-loaders/module-named-exports.mjs' does not provide an export named 'notfound'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:*:*)
at async ModuleJob.run (node:internal/modules/esm/module_job:*:*)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:*:*)
at async loadESM (node:internal/process/esm_loader:*:*)
at async handleMainPromise (node:internal/modules/run_main:*:*)
Node.js *

View file

@ -1,4 +0,0 @@
// Flags: --experimental-json-modules --no-warnings
/* eslint-disable no-unused-vars */
import '../common/index.mjs';
import { ofLife } from '../fixtures/experimental.json' assert { type: 'json' };

View file

@ -1,12 +0,0 @@
file:///*/test/message/esm_display_syntax_error_import_json_named_export.mjs:*
import { ofLife } from '../fixtures/experimental.json' assert { type: 'json' };
^^^^^^
SyntaxError: The requested module '../fixtures/experimental.json' does not provide an export named 'ofLife'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:*:*)
at async ModuleJob.run (node:internal/modules/esm/module_job:*:*)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:*:*)
at async loadESM (node:internal/process/esm_loader:*:*)
at async handleMainPromise (node:internal/modules/run_main:*:*)
Node.js *

View file

@ -1,2 +0,0 @@
import '../common/index.mjs';
import '../fixtures/es-module-loaders/syntax-error-import.mjs';

View file

@ -1,12 +0,0 @@
file:///*/test/fixtures/es-module-loaders/syntax-error-import.mjs:1
import { foo, notfound } from './module-named-exports.mjs';
^^^^^^^^
SyntaxError: The requested module './module-named-exports.mjs' does not provide an export named 'notfound'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:*:*)
at async ModuleJob.run (node:internal/modules/esm/module_job:*:*)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:*:*)
at async loadESM (node:internal/process/esm_loader:*:*)
at async handleMainPromise (node:internal/modules/run_main:*:*)
Node.js *

View file

@ -1,2 +0,0 @@
import '../common/index.mjs';
import '../fixtures/es-module-loaders/syntax-error.mjs';

View file

@ -1,9 +0,0 @@
file:///*/test/fixtures/es-module-loaders/syntax-error.mjs:2
await async () => 0;
^^^^^^^^^^^^^
SyntaxError: Malformed arrow function parameter list
at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:*:*)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:*:*)
Node.js *

View file

@ -1,2 +0,0 @@
import '../common/index.mjs';
import 'data:text/javascript,export{}' assert {type:'json'};

View file

@ -1,18 +0,0 @@
node:internal/errors:*
ErrorCaptureStackTrace(err);
^
TypeError [ERR_IMPORT_ASSERTION_TYPE_FAILED]: Module "data:text/javascript,export{}" is not of type "json"
at new NodeError (node:internal/errors:*:*)
at handleInvalidType (node:internal/modules/esm/assert:*:*)
at validateAssertions (node:internal/modules/esm/assert:*:*)
at defaultLoad (node:internal/modules/esm/load:*:*)
at ESMLoader.load (node:internal/modules/esm/loader:*:*)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:*:*)
at new ModuleJob (node:internal/modules/esm/module_job:*:*)
at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:*:*)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:*:*)
at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:*:*) {
code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED'
}
Node.js *

View file

@ -1,3 +0,0 @@
// Flags: --experimental-json-modules
import '../common/index.mjs';
import 'data:application/json,{}';

View file

@ -1,19 +0,0 @@
node:internal/errors:*
ErrorCaptureStackTrace(err);
^
TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "data:application/json,{}" needs an import assertion of type "json"
at new NodeError (node:internal/errors:*:*)
at validateAssertions (node:internal/modules/esm/assert:*:*)
at defaultLoad (node:internal/modules/esm/load:*:*)
at ESMLoader.load (node:internal/modules/esm/loader:*:*)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:*:*)
at new ModuleJob (node:internal/modules/esm/module_job:*:*)
at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:*:*)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:*:*)
at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:*:*)
at async Promise.all (index *) {
code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}
Node.js *

View file

@ -1,2 +0,0 @@
import '../common/index.mjs';
import '../fixtures/empty.js' assert { type: 'unsupported' };

View file

@ -1,19 +0,0 @@
node:internal/errors:*
ErrorCaptureStackTrace(err);
^
TypeError [ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED]: Import assertion type "unsupported" is unsupported
at new NodeError (node:internal/errors:*:*)
at handleInvalidType (node:internal/modules/esm/assert:*:*)
at validateAssertions (node:internal/modules/esm/assert:*:*)
at defaultLoad (node:internal/modules/esm/load:*:*)
at ESMLoader.load (node:internal/modules/esm/loader:*:*)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:*:*)
at new ModuleJob (node:internal/modules/esm/module_job:*:*)
at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:*:*)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:*:*)
at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:*:*) {
code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED'
}
Node.js *

View file

@ -1,3 +0,0 @@
// Flags: --experimental-loader i-dont-exist
import '../common/index.mjs';
console.log('This should not be printed');

View file

@ -1,20 +0,0 @@
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `* --trace-warnings ...` to show where the warning was created)
node:internal/errors:*
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist' imported from *
at new NodeError (node:internal/errors:*:*)
at packageResolve (node:internal/modules/esm/resolve:*:*)
at moduleResolve (node:internal/modules/esm/resolve:*:*)
at defaultResolve (node:internal/modules/esm/resolve:*:*)
at ESMLoader.resolve (node:internal/modules/esm/loader:*:*)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:*:*)
at ESMLoader.import (node:internal/modules/esm/loader:*:*)
at initializeLoader (node:internal/process/esm_loader:*:*)
at loadESM (node:internal/process/esm_loader:*:*)
at runMainESM (node:internal/modules/run_main:*:*) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js *

View file

@ -1,17 +0,0 @@
'use strict';
require('../common');
const { spawn } = require('child_process');
const { join } = require('path');
const { fixturesDir } = require('../common/fixtures');
spawn(
process.execPath,
[
join(fixturesDir, 'esm_loader_not_found_cjs_hint_bare.mjs'),
],
{
cwd: fixturesDir,
stdio: 'inherit'
}
);

View file

@ -1,18 +0,0 @@
node:internal/process/esm_loader:*
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '*test*fixtures*node_modules*some_module*obj' imported from *test*fixtures*esm_loader_not_found_cjs_hint_bare.mjs
Did you mean to import some_module/obj.js?
at new NodeError (node:internal/errors:*:*)
at finalizeResolution (node:internal/modules/esm/resolve:*:*)
at moduleResolve (node:internal/modules/esm/resolve:*:*)
at defaultResolve (node:internal/modules/esm/resolve:*:*)
at ESMLoader.resolve (node:internal/modules/esm/loader:*:*)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:*:*)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:*:*)
at link (node:internal/modules/esm/module_job:*:*) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js *

View file

@ -1,3 +0,0 @@
// Flags: --experimental-loader ./test/common/fixtures
import '../common/index.mjs';
console.log('This should not be printed');

View file

@ -1,22 +0,0 @@
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `* --trace-warnings ...` to show where the warning was created)
node:internal/process/esm_loader:*
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '*test*common*fixtures' imported from *
Did you mean to import ./test/common/fixtures.js?
at new NodeError (node:internal/errors:*:*)
at finalizeResolution (node:internal/modules/esm/resolve:*:*)
at moduleResolve (node:internal/modules/esm/resolve:*:*)
at defaultResolve (node:internal/modules/esm/resolve:*:*)
at ESMLoader.resolve (node:internal/modules/esm/loader:*:*)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:*:*)
at ESMLoader.import (node:internal/modules/esm/loader:*:*)
at initializeLoader (node:internal/process/esm_loader:*:*)
at loadESM (node:internal/process/esm_loader:*:*)
at runMainESM (node:internal/modules/run_main:*:*) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js *

View file

@ -1,3 +0,0 @@
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/syntax-error.mjs
import '../common/index.mjs';
console.log('This should not be printed');

View file

@ -1,12 +0,0 @@
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `* --trace-warnings ...` to show where the warning was created)
file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2
await async () => 0;
^^^^^^^^^^^^^
SyntaxError: Malformed arrow function parameter list
at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:*:*)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:*:*)
at async link (node:internal/modules/esm/module_job:*:*)
Node.js *

View file

@ -1,4 +0,0 @@
// Flags: --no-warnings --throw-deprecation --experimental-loader ./test/fixtures/es-module-loaders/hooks-obsolete.mjs
/* eslint-disable node-core/require-common-first, node-core/required-modules */
await import('whatever');

View file

@ -1,11 +0,0 @@
node:internal/process/warning:*
throw warning;
^
DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: dynamicInstantiate, getFormat, getSource, transformSource
at Function.pluckHooks (node:internal/modules/esm/loader:*:*)
at ESMLoader.addCustomLoaders (node:internal/modules/esm/loader:*:*)
at initializeLoader (node:internal/process/esm_loader:*:*)
at async loadESM (node:internal/process/esm_loader:*:*)
at async handleMainPromise (node:internal/modules/run_main:*:*)
Node.js *