From 37353413388cfb75423e846b0f6f044751add893 Mon Sep 17 00:00:00 2001 From: Andrew Kuzmenko Date: Wed, 18 Dec 2019 18:43:22 +0300 Subject: [PATCH 1/2] 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 --- test/parallel/test-vm-module-basic.js | 41 ++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-vm-module-basic.js b/test/parallel/test-vm-module-basic.js index 30c666b6c2949c..e9f0b1cb549211 100644 --- a/test/parallel/test-vm-module-basic.js +++ b/test/parallel/test-vm-module-basic.js @@ -4,7 +4,7 @@ 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 +107,42 @@ const util = require('util'); assert.notStrictEqual(dep, undefined); assert.strictEqual(dep, m.dependencySpecifiers); } + +// Check the impossibility of creating an abstract instance of the Module. +{ + assert.throws( + () => { + new Module(); + }, + /^TypeError: Module is not a constructor$/ + ); + +} + +// Check to throws invalid exportNames +{ + assert.throws( + () => { + new SyntheticModule(undefined, () => {}, {}); + }, + ); +} + +// Check to throws invalid evaluateCallback +{ + assert.throws( + () => { + new SyntheticModule([], undefined, {}); + }, + ); +} + +// Check to throws invalid options +{ + assert.throws( + () => { + new SyntheticModule([], () => {}, null); + }, + ); +} + From d2ff9fa0a5e6906dd065dde73410b751c9941107 Mon Sep 17 00:00:00 2001 From: Andrew Kuzmenko Date: Thu, 19 Dec 2019 17:41:28 +0300 Subject: [PATCH 2/2] test: fix eslint and use common module for expectsError Fix 'max-len','no-restricted-syntax' (assert.throws must be envoken with at least two args) and useless blank lines at the end of file --- test/parallel/test-vm-module-basic.js | 49 ++++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/test/parallel/test-vm-module-basic.js b/test/parallel/test-vm-module-basic.js index e9f0b1cb549211..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 { Module, SourceTextModule, SyntheticModule, createContext } = require('vm'); +const { + Module, + SourceTextModule, + SyntheticModule, + createContext +} = require('vm'); const util = require('util'); (async function test1() { @@ -110,39 +115,35 @@ const util = require('util'); // Check the impossibility of creating an abstract instance of the Module. { - assert.throws( - () => { - new Module(); - }, - /^TypeError: Module is not a constructor$/ - ); - + common.expectsError(() => new Module(), { + message: 'Module is not a constructor', + type: TypeError + }); } // Check to throws invalid exportNames { - assert.throws( - () => { - new SyntheticModule(undefined, () => {}, {}); - }, - ); + common.expectsError(() => new SyntheticModule(undefined, () => {}, {}), { + message: 'The "exportNames" argument must be an Array of strings.' + + ' Received undefined', + type: TypeError + }); } // Check to throws invalid evaluateCallback { - assert.throws( - () => { - new SyntheticModule([], undefined, {}); - }, - ); + common.expectsError(() => new SyntheticModule([], undefined, {}), { + message: 'The "evaluateCallback" argument must be of type function.' + + ' Received undefined', + type: TypeError + }); } // Check to throws invalid options { - assert.throws( - () => { - new SyntheticModule([], () => {}, null); - }, - ); + common.expectsError(() => new SyntheticModule([], () => {}, null), { + message: 'The "options" argument must be of type object.' + + ' Received null', + type: TypeError + }); } -