node/test/parallel/test-vm-module-evaluate-synthethic-module-rejection.js
Joyee Cheung 38bf955937 vm: make vm.Module.evaluate() conditionally synchronous
- Make sure that the vm.Module.evaluate() method is conditionally
  synchronous based on the specification. Previously, the returned
  promise is unconditionally pending (even for synthetic modules and
  source text modules without top-level await) instead of immediately
  fulfilled, making it harder to debug as it deviates from the
  specified behavior.
- Clarify the synchronicity of this method in the documentation
- Add more tests for the synchronicity of this method.

PR-URL: https://github.com/nodejs/node/pull/60205
Refs: https://github.com/nodejs/node/issues/59656
Refs: https://github.com/nodejs/node/issues/37648
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2025-10-18 08:20:47 +00:00

43 lines
1.6 KiB
JavaScript

// Flags: --experimental-vm-modules
'use strict';
// This tests the result of evaluating a vm.SyntheticModule with an async rejection
// in the evaluation step.
const common = require('../common');
const assert = require('assert');
// To make testing easier we just use the public inspect API. If the output format
// changes, update this test accordingly.
const { inspect } = require('util');
const vm = require('vm');
// The promise _synchronously_ resolves to undefined, because for a synthethic module,
// the evaluation operation can only either resolve or reject immediately.
// In this case, the asynchronously rejected promise can't be handled from the outside,
// so we'll catch it with the isolate-level unhandledRejection handler.
// See https://tc39.es/ecma262/#sec-smr-Evaluate
process.on('unhandledRejection', common.mustCall((err) => {
assert.strictEqual(err.message, 'asynchronous source text module');
}));
const mod = new vm.SyntheticModule(['a'], common.mustCall(async () => {
throw new Error('asynchronous source text module');
}));
const promise = mod.evaluate();
assert.match(inspect(promise), /Promise { undefined }/);
// Accessing the uninitialized export of a synthetic module returns undefined.
assert.strictEqual(mod.namespace.a, undefined);
promise.then(common.mustCall((value) => {
assert.strictEqual(value, undefined);
}));
// Calling evaluate() again results in a promise _synchronously_ resolved to undefined again.
const promise2 = mod.evaluate();
assert.match(inspect(promise2), /Promise { undefined }/);
promise2.then(common.mustCall((value) => {
assert.strictEqual(value, undefined);
}));