Skip to content

Commit 457d81c

Browse files
committed
test: augment tests for SourceTextModule
Adds tests for a few error conditions. Also, adds tests to make sure the dynamically generated url is correct.
1 parent 714c1b8 commit 457d81c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

test/parallel/test-vm-module-basic.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,16 @@ const { SourceTextModule, createContext } = require('vm');
5050
await m.evaluate({ timeout: 500 })
5151
.then(() => assert(false), () => {});
5252
})();
53+
54+
// Check the generated url for each module
55+
(async () => {
56+
const context1 = createContext({ });
57+
const context2 = createContext({ });
58+
59+
const m1 = new SourceTextModule('1', { context: context1 });
60+
assert.strictEqual(m1.url, 'vm:module(0)');
61+
const m2 = new SourceTextModule('2', { context: context1 });
62+
assert.strictEqual(m2.url, 'vm:module(1)');
63+
const m3 = new SourceTextModule('3', { context: context2 });
64+
assert.strictEqual(m3.url, 'vm:module(0)');
65+
})();

test/parallel/test-vm-module-errors.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ async function checkArgType() {
4343
});
4444

4545
for (const invalidOptions of [
46-
0, 1, null, true, 'str', () => {}, { url: 0 }, Symbol.iterator
46+
0, 1, null, true, 'str', () => {}, { url: 0 }, Symbol.iterator,
47+
{ context: null }, { context: 'hucairz' }, { context: {} }
4748
]) {
4849
common.expectsError(() => {
4950
new SourceTextModule('', invalidOptions);
@@ -222,6 +223,17 @@ async function checkLinking() {
222223
});
223224
}
224225

226+
common.expectsError(() => {
227+
new SourceTextModule('', {
228+
importModuleDynamically: 'hucairz'
229+
});
230+
}, {
231+
code: 'ERR_INVALID_ARG_TYPE',
232+
type: TypeError,
233+
message: 'The "options.importModuleDynamically"' +
234+
' property must be of type function. Received type string'
235+
});
236+
225237
// Check the JavaScript engine deals with exceptions correctly
226238
async function checkExecution() {
227239
await (async () => {

0 commit comments

Comments
 (0)