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: https://github.com/nodejs/node/pull/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: https://github.com/nodejs/node/pull/23572
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Andrew Eisenberg 2018-10-12 11:10:39 -07:00 committed by Ruben Bridgewater
parent c596bcc4ff
commit cac4909bf3
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 26 additions and 1 deletions

View File

@ -50,3 +50,16 @@ const { SourceTextModule, createContext } = require('vm');
await m.evaluate({ timeout: 500 })
.then(() => assert(false), () => {});
})();
// Check the generated url for each module
(async () => {
const context1 = createContext({ });
const context2 = createContext({ });
const m1 = new SourceTextModule('1', { context: context1 });
assert.strictEqual(m1.url, 'vm:module(0)');
const m2 = new SourceTextModule('2', { context: context1 });
assert.strictEqual(m2.url, 'vm:module(1)');
const m3 = new SourceTextModule('3', { context: context2 });
assert.strictEqual(m3.url, 'vm:module(0)');
})();

View File

@ -43,7 +43,8 @@ async function checkArgType() {
});
for (const invalidOptions of [
0, 1, null, true, 'str', () => {}, { url: 0 }, Symbol.iterator
0, 1, null, true, 'str', () => {}, { url: 0 }, Symbol.iterator,
{ context: null }, { context: 'hucairz' }, { context: {} }
]) {
common.expectsError(() => {
new SourceTextModule('', invalidOptions);
@ -231,6 +232,17 @@ async function checkLinking() {
});
}
common.expectsError(() => {
new SourceTextModule('', {
importModuleDynamically: 'hucairz'
});
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.importModuleDynamically"' +
' property must be of type function. Received type string'
});
// Check the JavaScript engine deals with exceptions correctly
async function checkExecution() {
await (async () => {