Skip to content

Commit 53ee9ca

Browse files
aeisenbergBethGriggs
authored andcommitted
test: augment tests for SourceTextModule
Adds tests for a few error conditions. Also, adds tests to make sure the dynamically generated url is correct. PR-URL: #23573 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> PR-URL: #23572 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 4a4e1f4 commit 53ee9ca

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

+13
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

+13-1
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);
@@ -231,6 +232,17 @@ async function checkLinking() {
231232
});
232233
}
233234

235+
common.expectsError(() => {
236+
new SourceTextModule('', {
237+
importModuleDynamically: 'hucairz'
238+
});
239+
}, {
240+
code: 'ERR_INVALID_ARG_TYPE',
241+
type: TypeError,
242+
message: 'The "options.importModuleDynamically"' +
243+
' property must be of type function. Received type string'
244+
});
245+
234246
// Check the JavaScript engine deals with exceptions correctly
235247
async function checkExecution() {
236248
await (async () => {

0 commit comments

Comments
 (0)