11cb0ef41Sopenharmony_ci// Flags: --experimental-loader ./test/fixtures/es-module-loaders/hooks-custom.mjs 21cb0ef41Sopenharmony_ciimport '../common/index.mjs'; 31cb0ef41Sopenharmony_ciimport assert from 'assert'; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciawait assert.rejects( 71cb0ef41Sopenharmony_ci import('nonexistent/file.mjs'), 81cb0ef41Sopenharmony_ci { code: 'ERR_MODULE_NOT_FOUND' }, 91cb0ef41Sopenharmony_ci); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciawait assert.rejects( 121cb0ef41Sopenharmony_ci import('../fixtures/es-modules/file.unknown'), 131cb0ef41Sopenharmony_ci { code: 'ERR_UNKNOWN_FILE_EXTENSION' }, 141cb0ef41Sopenharmony_ci); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciawait assert.rejects( 171cb0ef41Sopenharmony_ci import('esmHook/badReturnVal.mjs'), 181cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_RETURN_VALUE' }, 191cb0ef41Sopenharmony_ci); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci// Nullish values are allowed; booleans are not 221cb0ef41Sopenharmony_ciawait assert.rejects( 231cb0ef41Sopenharmony_ci import('esmHook/format.false'), 241cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, 251cb0ef41Sopenharmony_ci); 261cb0ef41Sopenharmony_ciawait assert.rejects( 271cb0ef41Sopenharmony_ci import('esmHook/format.true'), 281cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, 291cb0ef41Sopenharmony_ci); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciawait assert.rejects( 321cb0ef41Sopenharmony_ci import('esmHook/badReturnFormatVal.mjs'), 331cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, 341cb0ef41Sopenharmony_ci); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciawait assert.rejects( 371cb0ef41Sopenharmony_ci import('esmHook/unsupportedReturnFormatVal.mjs'), 381cb0ef41Sopenharmony_ci { code: 'ERR_UNKNOWN_MODULE_FORMAT' }, 391cb0ef41Sopenharmony_ci); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciawait assert.rejects( 421cb0ef41Sopenharmony_ci import('esmHook/badReturnSourceVal.mjs'), 431cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, 441cb0ef41Sopenharmony_ci); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciawait import('../fixtures/es-module-loaders/js-as-esm.js') 471cb0ef41Sopenharmony_ci.then((parsedModule) => { 481cb0ef41Sopenharmony_ci assert.strictEqual(typeof parsedModule, 'object'); 491cb0ef41Sopenharmony_ci assert.strictEqual(parsedModule.namedExport, 'named-export'); 501cb0ef41Sopenharmony_ci}); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci// The custom loader tells node .ext files are MJS 531cb0ef41Sopenharmony_ciawait import('../fixtures/es-modules/file.ext') 541cb0ef41Sopenharmony_ci.then((parsedModule) => { 551cb0ef41Sopenharmony_ci assert.strictEqual(typeof parsedModule, 'object'); 561cb0ef41Sopenharmony_ci const { default: defaultExport } = parsedModule; 571cb0ef41Sopenharmony_ci assert.strictEqual(typeof defaultExport, 'function'); 581cb0ef41Sopenharmony_ci assert.strictEqual(defaultExport.name, 'iAmReal'); 591cb0ef41Sopenharmony_ci assert.strictEqual(defaultExport(), true); 601cb0ef41Sopenharmony_ci}); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci// The custom loader's resolve hook predetermines the format 631cb0ef41Sopenharmony_ciawait import('esmHook/preknownFormat.pre') 641cb0ef41Sopenharmony_ci.then((parsedModule) => { 651cb0ef41Sopenharmony_ci assert.strictEqual(typeof parsedModule, 'object'); 661cb0ef41Sopenharmony_ci assert.strictEqual(parsedModule.default, 'hello world'); 671cb0ef41Sopenharmony_ci}); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci// The custom loader provides source even though file does not actually exist 701cb0ef41Sopenharmony_ciawait import('esmHook/virtual.mjs') 711cb0ef41Sopenharmony_ci.then((parsedModule) => { 721cb0ef41Sopenharmony_ci assert.strictEqual(typeof parsedModule, 'object'); 731cb0ef41Sopenharmony_ci assert.strictEqual(typeof parsedModule.default, 'undefined'); 741cb0ef41Sopenharmony_ci assert.strictEqual(parsedModule.message, 'WOOHOO!'); 751cb0ef41Sopenharmony_ci}); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci// Ensure custom loaders have separate context from userland 781cb0ef41Sopenharmony_ci// hooks-custom.mjs also increments count (which starts at 0) 791cb0ef41Sopenharmony_ci// if both share context, count here would be 2 801cb0ef41Sopenharmony_ciawait import('../fixtures/es-modules/stateful.mjs') 811cb0ef41Sopenharmony_ci.then(({ default: count }) => { 821cb0ef41Sopenharmony_ci assert.strictEqual(count(), 1); 831cb0ef41Sopenharmony_ci}); 84