node/test/es-module/test-esm-loader.mjs
Jacob df22736d80 esm: consolidate ESM loader hooks
doc: update ESM hook examples

esm: fix unsafe primordial

doc: fix ESM example linting

esm: allow source of type ArrayBuffer

doc: update ESM hook changelog to include resolve format

esm: allow all ArrayBuffers and TypedArrays for load hook source

doc: tidy code & API docs

doc: convert ESM source table header from Title Case to Sentence case

doc: add detailed explanation for getPackageType

esm: add caveat that ESMLoader::import() must NOT be renamed

esm: tidy code declaration of getFormat protocolHandlers

doc: correct ESM doc link (bad conflict resolution)

doc: update ESM hook limitation for CJS

esm: tweak preload description

doc: update ESM getPackageType() example explanation

PR-URL: https://github.com/nodejs/node/pull/37468
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
2021-09-11 18:08:35 -07:00

83 lines
2.5 KiB
JavaScript

// Flags: --experimental-loader ./test/fixtures/es-module-loaders/hooks-custom.mjs
import '../common/index.mjs';
import assert from 'assert';
await assert.rejects(
import('nonexistent/file.mjs'),
{ code: 'ERR_MODULE_NOT_FOUND' },
);
await assert.rejects(
import('../fixtures/es-modules/file.unknown'),
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' },
);
await assert.rejects(
import('esmHook/badReturnVal.mjs'),
{ code: 'ERR_INVALID_RETURN_VALUE' },
);
// Nullish values are allowed; booleans are not
await assert.rejects(
import('esmHook/format.false'),
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' },
);
await assert.rejects(
import('esmHook/format.true'),
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' },
);
await assert.rejects(
import('esmHook/badReturnFormatVal.mjs'),
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' },
);
await assert.rejects(
import('esmHook/unsupportedReturnFormatVal.mjs'),
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' },
);
await assert.rejects(
import('esmHook/badReturnSourceVal.mjs'),
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' },
);
await import('../fixtures/es-module-loaders/js-as-esm.js')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(parsedModule.namedExport, 'named-export');
});
// The custom loader tells node .ext files are MJS
await import('../fixtures/es-modules/file.ext')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
const { default: defaultExport } = parsedModule;
assert.strictEqual(typeof defaultExport, 'function');
assert.strictEqual(defaultExport.name, 'iAmReal');
assert.strictEqual(defaultExport(), true);
});
// The custom loader's resolve hook predetermines the format
await import('esmHook/preknownFormat.pre')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(parsedModule.default, 'hello world');
});
// The custom loader provides source even though file does not actually exist
await import('esmHook/virtual.mjs')
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(typeof parsedModule.default, 'undefined');
assert.strictEqual(parsedModule.message, 'WOOHOO!');
});
// Ensure custom loaders have separate context from userland
// hooks-custom.mjs also increments count (which starts at 0)
// if both share context, count here would be 2
await import('../fixtures/es-modules/stateful.mjs')
.then(({ default: count }) => {
assert.strictEqual(count(), 1);
});