From 2d86696f350646fca18772f2aed08707579662ae Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Fri, 12 Oct 2018 11:10:39 -0700 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig PR-URL: https://github.com/nodejs/node/pull/23572 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- test/parallel/test-vm-module-basic.js | 13 +++++++++++++ test/parallel/test-vm-module-errors.js | 14 +++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-vm-module-basic.js b/test/parallel/test-vm-module-basic.js index ffe376ccc2b1b1..8cf687c2bb23a8 100644 --- a/test/parallel/test-vm-module-basic.js +++ b/test/parallel/test-vm-module-basic.js @@ -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)'); +})(); diff --git a/test/parallel/test-vm-module-errors.js b/test/parallel/test-vm-module-errors.js index a6b2a6e797a1bb..05022160832a53 100644 --- a/test/parallel/test-vm-module-errors.js +++ b/test/parallel/test-vm-module-errors.js @@ -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 () => {