node/test/es-module/test-require-module-preload.js
Joyee Cheung 06206af181
module: unflag --experimental-require-module
This unflags --experimental-require-module so require(esm) can be
used without the flag. For now, when require() actually encounters
an ESM, it will still emit an experimental warning. To opt out
of the feature, --no-experimental-require-module can be used.

There are some tests specifically testing ERR_REQUIRE_ESM. Some
of them are repurposed to test --no-experimental-require-module.
Some of them are modified to just expect loading require(esm) to
work, when it's appropriate.

PR-URL: https://github.com/nodejs/node/pull/55085
Refs: https://github.com/nodejs/node/issues/52697
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
2024-09-26 14:21:37 +00:00

123 lines
2.4 KiB
JavaScript

'use strict';
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const { fixturesDir } = require('../common/fixtures');
const warningRE = /ExperimentalWarning: Support for loading ES Module in require/;
function testPreload(preloadFlag) {
// The warning is only emitted when ESM is loaded by --require.
const stderr = preloadFlag !== '--import' ? warningRE : undefined;
// Test named exports.
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
'./es-module-loaders/module-named-exports.mjs',
'./printA.js',
],
{
cwd: fixturesDir
},
{
stdout: 'A',
stderr,
trim: true,
}
);
}
// Test ESM that import ESM.
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
'./es-modules/import-esm.mjs',
'./printA.js',
],
{
cwd: fixturesDir
},
{
stderr,
stdout: /^world\s+A$/,
trim: true,
}
);
}
// Test ESM that import CJS.
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
'./es-modules/cjs-exports.mjs',
'./printA.js',
],
{
cwd: fixturesDir
},
{
stdout: /^ok\s+A$/,
stderr,
trim: true,
}
);
}
// Test ESM that require() CJS.
// Can't use the common/index.mjs here because that checks the globals, and
// -r injects a bunch of globals.
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
'./es-modules/require-cjs.mjs',
'./printA.js',
],
{
cwd: fixturesDir
},
{
stdout: /^world\s+A$/,
stderr,
trim: true,
}
);
}
}
testPreload('--require');
testPreload('--import');
// Test "type": "module" and "main" field in package.json, this is only for --require because
// --import does not support extension-less preloads.
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
'--require',
'./es-modules/package-type-module',
'./printA.js',
],
{
cwd: fixturesDir
},
{
stdout: /^package-type-module\s+A$/,
stderr: warningRE,
trim: true,
}
);
}