diff --git a/doc/api/vm.md b/doc/api/vm.md index 30988ff53d3b6e..1ee32518da1280 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -101,7 +101,7 @@ changes: **Default:** `false`. * `importModuleDynamically` {Function|vm.constants.USE\_MAIN\_CONTEXT\_DEFAULT\_LOADER} - Used to specify the how the modules should be loaded during the evaluation + Used to specify how the modules should be loaded during the evaluation of this script when `import()` is called. This option is part of the experimental modules API. We do not recommend using it in a production environment. For detailed information, see @@ -1044,7 +1044,7 @@ added: REPLACEME * {Object} -Returns an object containing commonly used constants for vm operations. +Returns an object containing commonly used constants for VM operations. ### `vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER` @@ -1052,7 +1052,7 @@ Returns an object containing commonly used constants for vm operations. added: REPLACEME --> -> Stability: 1 - Experimental +> Stability: 1.1 - Active development A constant that can be used as the `importModuleDynamically` option to `vm.Script` and `vm.compileFunction()` so that Node.js uses the default @@ -1632,7 +1632,7 @@ using it in a production environment. ### When the `importModuleDynamically` option is not specified or undefined -If this option is not specified, or if it's undefined, code containing +If this option is not specified, or if it's `undefined`, code containing `import()` can still be compiled by the vm APIs, but when the compiled code is executed and it actually calls `import()`, the result will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. diff --git a/test/es-module/test-vm-main-context-default-loader.js b/test/es-module/test-vm-main-context-default-loader.js index bd18fcc9e65104..29274568ca93f0 100644 --- a/test/es-module/test-vm-main-context-default-loader.js +++ b/test/es-module/test-vm-main-context-default-loader.js @@ -21,35 +21,18 @@ assert( !process.execArgv.includes('--experimental-vm-modules'), 'This test must be run without --experimental-vm-modules'); -assert(typeof USE_MAIN_CONTEXT_DEFAULT_LOADER, 'symbol'); +assert.strictEqual(typeof USE_MAIN_CONTEXT_DEFAULT_LOADER, 'symbol'); async function testNotFoundErrors(options) { // Import user modules. const script = new Script('import("./message.mjs")', options); // Use try-catch for better async stack traces in the logs. - let result = 'uncaught-fallback'; - try { - result = await script.runInThisContext(); - } catch (error) { - assert.strictEqual(error.code, 'ERR_MODULE_NOT_FOUND'); - } finally { - assert.strictEqual( - result, 'uncaught-fallback', - `import from vm.Script with ${inspect(options)} should throw ERR_MODULE_NOT_FOUND`); - } + await assert.rejects(script.runInThisContext(), { code: 'ERR_MODULE_NOT_FOUND' }); result = 'uncaught-fallback'; const imported = compileFunction('return import("./message.mjs")', [], options)(); // Use try-catch for better async stack traces in the logs. - try { - result = await imported; - } catch (error) { - assert.strictEqual(error.code, 'ERR_MODULE_NOT_FOUND'); - } finally { - assert.strictEqual( - result, 'uncaught-fallback', - `import from vm.compileFunction with ${inspect(options)} should throw ERR_MODULE_NOT_FOUND`); - } + await assert.rejects(imported, { code: 'ERR_MODULE_NOT_FOUND' }); } async function testLoader(options) { @@ -135,17 +118,7 @@ async function main() { const s = new Script('Promise.resolve("import(\'./message.mjs\')").then(eval)', { importModuleDynamically: common.mustNotCall(), }); - // Use try-catch for better async stack traces in the logs. - let result = 'uncaught-fallback'; - try { - result = await s.runInContext(ctx); - } catch (error) { - assert.strictEqual(error.code, 'ERR_MODULE_NOT_FOUND'); - } finally { - assert.strictEqual( - result, 'uncaught-fallback', - `import from vm.createContext() with ${inspect(options)} should throw ERR_MODULE_NOT_FOUND`); - } + await assert.rejects(s.runInContext(ctx), { code: 'ERR_MODULE_NOT_FOUND' }); } await testLoader(undefinedOptions);