MCPcopy Index your code
hub / github.com/nodejs/node / test

Function test

test/parallel/test-vm-module-dynamic-import.js:28–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26}
27
28async function test() {
29 const foo = new SourceTextModule('export const a = 1;');
30 await foo.link(common.mustNotCall());
31 await foo.evaluate();
32
33 {
34 const s = new Script('import("foo")', {
35 importModuleDynamically: common.mustCall((specifier, wrap) => {
36 assert.strictEqual(specifier, 'foo');
37 assert.strictEqual(wrap, s);
38 return foo;
39 }),
40 });
41
42 const result = s.runInThisContext();
43 assert.strictEqual(await result, foo.namespace);
44 }
45
46 {
47 const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
48 importModuleDynamically: common.mustCall((specifier, wrap) => {
49 assert.strictEqual(specifier, 'foo');
50 assert.strictEqual(wrap, m);
51 return foo;
52 }),
53 });
54 await m.link(common.mustNotCall());
55 await m.evaluate();
56 assert.strictEqual(await globalThis.fooResult, foo.namespace);
57 delete globalThis.fooResult;
58 }
59
60 {
61 const s = new Script('import("foo", { with: { key: "value" } })', {
62 importModuleDynamically: common.mustCall((specifier, wrap, attributes, phase) => {
63 assert.strictEqual(specifier, 'foo');
64 assert.strictEqual(wrap, s);
65 assert.deepStrictEqual(attributes, { __proto__: null, key: 'value' });
66 assert.strictEqual(phase, 'evaluation');
67 return foo;
68 }),
69 });
70
71 const result = s.runInThisContext();
72 assert.strictEqual(await result, foo.namespace);
73 }
74}
75
76async function testInvalid() {
77 const m = new SourceTextModule('globalThis.fooResult = import("foo")', {

Calls 3

runInThisContextMethod · 0.95
linkMethod · 0.45
evaluateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…