• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Flags: --experimental-vm-modules
4
5const common = require('../common');
6const assert = require('assert');
7const { SourceTextModule } = require('vm');
8
9async function testBasic() {
10  const m = new SourceTextModule('globalThis.importMeta = import.meta;', {
11    initializeImportMeta: common.mustCall((meta, module) => {
12      assert.strictEqual(module, m);
13      meta.prop = 42;
14    })
15  });
16  await m.link(common.mustNotCall());
17  await m.evaluate();
18  const result = globalThis.importMeta;
19  delete globalThis.importMeta;
20  assert.strictEqual(typeof result, 'object');
21  assert.strictEqual(Object.getPrototypeOf(result), null);
22  assert.strictEqual(result.prop, 42);
23  assert.deepStrictEqual(Reflect.ownKeys(result), ['prop']);
24}
25
26async function testInvalid() {
27  for (const invalidValue of [
28    null, {}, 0, Symbol.iterator, [], 'string', false,
29  ]) {
30    assert.throws(() => {
31      new SourceTextModule('', {
32        initializeImportMeta: invalidValue
33      });
34    }, {
35      code: 'ERR_INVALID_ARG_TYPE',
36      name: 'TypeError'
37    });
38  }
39}
40
41(async () => {
42  await testBasic();
43  await testInvalid();
44})().then(common.mustCall());
45