From ee14a645379e4eff58ab106f1eae7abc70a01d9e Mon Sep 17 00:00:00 2001 From: Andrew Kuzmenko Date: Wed, 18 Dec 2019 18:43:22 +0300 Subject: [PATCH] test: cover vm with negative tests Test the impossibility of creating an abstract instance of the Module. Test of SyntheticModule to throw exception if invalid params in constructor PR-URL: https://github.com/nodejs/node/pull/31028 Reviewed-By: Anna Henningsen Reviewed-By: Gus Caplan Reviewed-By: Ruben Bridgewater Reviewed-By: David Carlier Reviewed-By: Luigi Pinca Reviewed-By: Yongsheng Zhang Reviewed-By: Rich Trott --- test/parallel/test-vm-module-basic.js | 42 ++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-vm-module-basic.js b/test/parallel/test-vm-module-basic.js index 30c666b6c2949c..d45147cefd1f72 100644 --- a/test/parallel/test-vm-module-basic.js +++ b/test/parallel/test-vm-module-basic.js @@ -4,7 +4,12 @@ const common = require('../common'); const assert = require('assert'); -const { SourceTextModule, SyntheticModule, createContext } = require('vm'); +const { + Module, + SourceTextModule, + SyntheticModule, + createContext +} = require('vm'); const util = require('util'); (async function test1() { @@ -107,3 +112,38 @@ const util = require('util'); assert.notStrictEqual(dep, undefined); assert.strictEqual(dep, m.dependencySpecifiers); } + +// Check the impossibility of creating an abstract instance of the Module. +{ + common.expectsError(() => new Module(), { + message: 'Module is not a constructor', + type: TypeError + }); +} + +// Check to throws invalid exportNames +{ + common.expectsError(() => new SyntheticModule(undefined, () => {}, {}), { + message: 'The "exportNames" argument must be an Array of strings.' + + ' Received undefined', + type: TypeError + }); +} + +// Check to throws invalid evaluateCallback +{ + common.expectsError(() => new SyntheticModule([], undefined, {}), { + message: 'The "evaluateCallback" argument must be of type function.' + + ' Received undefined', + type: TypeError + }); +} + +// Check to throws invalid options +{ + common.expectsError(() => new SyntheticModule([], () => {}, null), { + message: 'The "options" argument must be of type object.' + + ' Received null', + type: TypeError + }); +}