node/test/parallel/test-vm-module-import-meta.js
Gus Caplan 5e7946fe79 vm: refactor SourceTextModule
- Removes redundant `instantiate` method
- Refactors `link` to match the spec linking steps more accurately
- Removes URL validation from SourceTextModule specifiers
- DRYs some dynamic import logic

Closes: https://github.com/nodejs/node/issues/29030

Co-Authored-By: Michaël Zasso <targos@protonmail.com>

PR-URL: https://github.com/nodejs/node/pull/29776
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
2019-10-02 15:54:35 -07:00

43 lines
1.0 KiB
JavaScript

'use strict';
// Flags: --experimental-vm-modules
const common = require('../common');
const assert = require('assert');
const { SourceTextModule } = require('vm');
async function testBasic() {
const m = new SourceTextModule('import.meta;', {
initializeImportMeta: common.mustCall((meta, module) => {
assert.strictEqual(module, m);
meta.prop = 42;
})
});
await m.link(common.mustNotCall());
const { result } = await m.evaluate();
assert.strictEqual(typeof result, 'object');
assert.strictEqual(Object.getPrototypeOf(result), null);
assert.strictEqual(result.prop, 42);
assert.deepStrictEqual(Reflect.ownKeys(result), ['prop']);
}
async function testInvalid() {
for (const invalidValue of [
null, {}, 0, Symbol.iterator, [], 'string', false
]) {
common.expectsError(() => {
new SourceTextModule('', {
initializeImportMeta: invalidValue
});
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
}
}
(async () => {
await testBasic();
await testInvalid();
})();