0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 12:10:08 +01:00
nodejs/test/parallel/test-shadow-realm-import-value-resolve.js
Chengzhong Wu fc2862b7f5
module: bootstrap module loaders in shadow realm
This bootstraps ESM loaders in the ShadowRealm with
`ShadowRealm.prototype.importValue` as its entry point and enables
loading ESM and CJS modules in the ShadowRealm. The module is imported
without a parent URL and resolved with the current process's working
directory.

PR-URL: https://github.com/nodejs/node/pull/48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-11-13 22:09:47 +08:00

29 lines
992 B
JavaScript

// Flags: --experimental-shadow-realm
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
common.skipIfWorker('process.chdir is not supported in workers.');
async function main() {
const realm = new ShadowRealm();
const dirname = __dirname;
// Set process cwd to the parent directory of __dirname.
const cwd = path.dirname(dirname);
process.chdir(cwd);
// Hardcode the relative path to ensure the string is still a valid relative
// URL string.
const relativePath = './fixtures/es-module-shadow-realm/re-export-state-counter.mjs';
// Make sure that the module can not be resolved relative to __filename.
assert.throws(() => require.resolve(relativePath), { code: 'MODULE_NOT_FOUND' });
// Resolve relative to the current working directory.
const getCounter = await realm.importValue(relativePath, 'getCounter');
assert.strictEqual(typeof getCounter, 'function');
}
main().then(common.mustCall());