node/test/parallel/test-vm-module-dynamic-import.js
Gus Caplan 0993fbe5b2
vm: add modules
Adds vm.Module, which wraps around ModuleWrap to provide an interface
for developers to work with modules in a more reflective manner.

Co-authored-by: Timothy Gu <timothygu99@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/17560
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2018-01-30 17:00:57 -08:00

28 lines
688 B
JavaScript

'use strict';
// Flags: --experimental-vm-modules --experimental-modules --harmony-dynamic-import
const common = require('../common');
common.crashOnUnhandledRejection();
const assert = require('assert');
const { Module, createContext } = require('vm');
const finished = common.mustCall();
(async function() {
const m = new Module('import("foo")', { context: createContext() });
await m.link(common.mustNotCall());
m.instantiate();
const { result } = await m.evaluate();
let threw = false;
try {
await result;
} catch (err) {
threw = true;
assert.strictEqual(err.message, 'import() called outside of main context');
}
assert(threw);
finished();
}());