forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-vm-module-dynamic-import.js
117 lines (103 loc) · 3.09 KB
/
test-vm-module-dynamic-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
// Flags: --experimental-vm-modules --harmony-import-assertions
const common = require('../common');
const assert = require('assert');
const { Script, SourceTextModule } = require('vm');
async function testNoCallback() {
const m = new SourceTextModule(`
globalThis.importResult = import("foo");
globalThis.importResult.catch(() => {});
`);
await m.link(common.mustNotCall());
await m.evaluate();
let threw = false;
try {
await globalThis.importResult;
} catch (err) {
threw = true;
assert.strictEqual(err.code, 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING');
}
delete globalThis.importResult;
assert(threw);
}
async function test() {
const foo = new SourceTextModule('export const a = 1;');
await foo.link(common.mustNotCall());
await foo.evaluate();
{
const s = new Script('import("foo")', {
importModuleDynamically: common.mustCall((specifier, wrap) => {
assert.strictEqual(specifier, 'foo');
assert.strictEqual(wrap, s);
return foo;
}),
});
const result = s.runInThisContext();
assert.strictEqual(foo.namespace, await result);
}
{
const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
importModuleDynamically: common.mustCall((specifier, wrap) => {
assert.strictEqual(specifier, 'foo');
assert.strictEqual(wrap, m);
return foo;
}),
});
await m.link(common.mustNotCall());
await m.evaluate();
assert.strictEqual(foo.namespace, await globalThis.fooResult);
delete globalThis.fooResult;
}
{
const s = new Script('import("foo", { assert: { key: "value" } })', {
importModuleDynamically: common.mustCall((specifier, wrap, assertion) => {
assert.strictEqual(specifier, 'foo');
assert.strictEqual(wrap, s);
assert.deepStrictEqual(assertion, { __proto__: null, key: 'value' });
return foo;
}),
});
const result = s.runInThisContext();
assert.strictEqual(foo.namespace, await result);
}
}
async function testInvalid() {
const m = new SourceTextModule('globalThis.fooResult = import("foo")', {
importModuleDynamically: common.mustCall((specifier, wrap) => {
return 5;
}),
});
await m.link(common.mustNotCall());
await m.evaluate();
await globalThis.fooResult.catch(common.mustCall((e) => {
assert.strictEqual(e.code, 'ERR_VM_MODULE_NOT_MODULE');
}));
delete globalThis.fooResult;
const s = new Script('import("bar")', {
importModuleDynamically: common.mustCall((specifier, wrap) => {
return undefined;
}),
});
let threw = false;
try {
await s.runInThisContext();
} catch (e) {
threw = true;
assert.strictEqual(e.code, 'ERR_VM_MODULE_NOT_MODULE');
}
assert(threw);
}
async function testInvalidimportModuleDynamically() {
assert.throws(
() => new Script(
'import("foo")',
{ importModuleDynamically: false }),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
}
(async function() {
await testNoCallback();
await test();
await testInvalid();
await testInvalidimportModuleDynamically();
}()).then(common.mustCall());