node/test/parallel/test-vm-module-import-meta.js
Rich Trott 330f25ef82 test: prepare for consistent comma-dangle lint rule
Make changes so that tests will pass when the comma-dangle settings
applied to the rest of the code base are also applied to tests.

PR-URL: https://github.com/nodejs/node/pull/37930
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Derek Lewis <DerekNonGeneric@inf.is>
2021-04-01 23:14:29 -07:00

45 lines
1.1 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('globalThis.importMeta = import.meta;', {
initializeImportMeta: common.mustCall((meta, module) => {
assert.strictEqual(module, m);
meta.prop = 42;
})
});
await m.link(common.mustNotCall());
await m.evaluate();
const result = globalThis.importMeta;
delete globalThis.importMeta;
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,
]) {
assert.throws(() => {
new SourceTextModule('', {
initializeImportMeta: invalidValue
});
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
}
}
(async () => {
await testBasic();
await testInvalid();
})().then(common.mustCall());