Skip to content

Commit 4b23b42

Browse files
committed
test: improve multiple vm tests
PR-URL: #14458 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent d41423f commit 4b23b42

9 files changed

+41
-51
lines changed

test/parallel/test-vm-context-async-script.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const vm = require('vm');
2626

27-
const sandbox = { setTimeout: setTimeout };
27+
const sandbox = { setTimeout };
2828

2929
const ctx = vm.createContext(sandbox);
3030

3131
vm.runInContext('setTimeout(function() { x = 3; }, 0);', ctx);
32-
setTimeout(function() {
32+
setTimeout(common.mustCall(() => {
3333
assert.strictEqual(sandbox.x, 3);
3434
assert.strictEqual(ctx.x, 3);
35-
}, 1);
35+
}), 1);

test/parallel/test-vm-context.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,29 @@ const vm = require('vm');
2727
const Script = vm.Script;
2828
let script = new Script('"passed";');
2929

30-
console.error('run in a new empty context');
30+
// Run in a new empty context
3131
let context = vm.createContext();
3232
let result = script.runInContext(context);
3333
assert.strictEqual('passed', result);
3434

35-
console.error('create a new pre-populated context');
35+
// Create a new pre-populated context
3636
context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
3737
assert.strictEqual('bar', context.foo);
3838
assert.strictEqual('lala', context.thing);
3939

40-
console.error('test updating context');
40+
// Test updating context
4141
script = new Script('foo = 3;');
4242
result = script.runInContext(context);
4343
assert.strictEqual(3, context.foo);
4444
assert.strictEqual('lala', context.thing);
4545

4646
// Issue GH-227:
47-
assert.throws(function() {
47+
assert.throws(() => {
4848
vm.runInNewContext('', null, 'some.js');
4949
}, /^TypeError: sandbox must be an object$/);
5050

5151
// Issue GH-1140:
52-
console.error('test runInContext signature');
52+
// Test runInContext signature
5353
let gh1140Exception;
5454
try {
5555
vm.runInContext('throw new Error()', context, 'expected-filename.js');
@@ -77,7 +77,7 @@ const contextifiedSandboxErrorMsg =
7777
});
7878

7979
// Issue GH-693:
80-
console.error('test RegExp as argument to assert.throws');
80+
// Test RegExp as argument to assert.throws
8181
script = vm.createScript('const assert = require(\'assert\'); assert.throws(' +
8282
'function() { throw "hello world"; }, /hello/);',
8383
'some.js');
@@ -92,13 +92,13 @@ assert.strictEqual(script.runInContext(ctx), false);
9292

9393
// Error on the first line of a module should
9494
// have the correct line and column number
95-
assert.throws(function() {
95+
assert.throws(() => {
9696
vm.runInContext('throw new Error()', context, {
9797
filename: 'expected-filename.js',
9898
lineOffset: 32,
9999
columnOffset: 123
100100
});
101-
}, function(err) {
101+
}, (err) => {
102102
return /expected-filename\.js:33:130/.test(err.stack);
103103
}, 'Expected appearance of proper offset in Error stack');
104104

test/parallel/test-vm-create-and-run-in-context.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ const assert = require('assert');
2626

2727
const vm = require('vm');
2828

29-
console.error('run in a new empty context');
29+
// Run in a new empty context
3030
let context = vm.createContext();
3131
let result = vm.runInContext('"passed";', context);
3232
assert.strictEqual('passed', result);
3333

34-
console.error('create a new pre-populated context');
34+
// Create a new pre-populated context
3535
context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
3636
assert.strictEqual('bar', context.foo);
3737
assert.strictEqual('lala', context.thing);
3838

39-
console.error('test updating context');
39+
// Test updating context
4040
result = vm.runInContext('var foo = 3;', context);
4141
assert.strictEqual(3, context.foo);
4242
assert.strictEqual('lala', context.thing);
4343

4444
// https://github.com/nodejs/node/issues/5768
45-
console.error('run in contextified sandbox without referencing the context');
45+
// Run in contextified sandbox without referencing the context
4646
const sandbox = { x: 1 };
4747
vm.createContext(sandbox);
4848
global.gc();

test/parallel/test-vm-function-declaration.js

-2
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,3 @@ assert.strictEqual(res.name, 'b', 'res should be named b');
4242
assert.strictEqual(typeof o.a, 'function', 'a should be function');
4343
assert.strictEqual(typeof o.b, 'function', 'b should be function');
4444
assert.strictEqual(res, o.b, 'result should be global b function');
45-
46-
console.log('ok');

test/parallel/test-vm-new-script-new-context.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ const Script = require('vm').Script;
3636

3737
{
3838
const script = new Script('throw new Error(\'test\');');
39-
assert.throws(function() {
39+
assert.throws(() => {
4040
script.runInNewContext();
4141
}, /^Error: test$/);
4242
}
4343

4444
{
4545
const script = new Script('foo.bar = 5;');
46-
assert.throws(function() {
46+
assert.throws(() => {
4747
script.runInNewContext();
4848
}, /^ReferenceError: foo is not defined$/);
4949
}
@@ -94,14 +94,14 @@ const Script = require('vm').Script;
9494
script.runInNewContext({ f: f });
9595
assert.strictEqual(f.a, 2);
9696

97-
assert.throws(function() {
97+
assert.throws(() => {
9898
script.runInNewContext();
9999
}, /^ReferenceError: f is not defined$/);
100100
}
101101

102102
{
103103
const script = new Script('');
104-
assert.throws(function() {
104+
assert.throws(() => {
105105
script.runInNewContext.call('\'hello\';');
106106
}, /^TypeError: this\.runInContext is not a function$/);
107107
}

test/parallel/test-vm-new-script-this-context.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ const Script = require('vm').Script;
2626

2727
common.globalCheck = false;
2828

29-
console.error('run a string');
29+
// Run a string
3030
let script = new Script('\'passed\';');
3131
const result = script.runInThisContext(script);
3232
assert.strictEqual('passed', result);
3333

34-
console.error('thrown error');
34+
// Thrown error
3535
script = new Script('throw new Error(\'test\');');
36-
assert.throws(function() {
36+
assert.throws(() => {
3737
script.runInThisContext(script);
3838
}, /^Error: test$/);
3939

@@ -43,7 +43,7 @@ script.runInThisContext(script);
4343
assert.strictEqual(2, global.hello);
4444

4545

46-
console.error('pass values');
46+
// Pass values
4747
global.code = 'foo = 1;' +
4848
'bar = 2;' +
4949
'if (typeof baz !== "undefined") throw new Error("test fail");';
@@ -55,7 +55,7 @@ assert.strictEqual(0, global.obj.foo);
5555
assert.strictEqual(2, global.bar);
5656
assert.strictEqual(1, global.foo);
5757

58-
console.error('call a function');
58+
// Call a function
5959
global.f = function() { global.foo = 100; };
6060
script = new Script('f()');
6161
script.runInThisContext(script);

test/parallel/test-vm-run-in-new-context.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ assert.strictEqual(typeof global.gc, 'function',
3131

3232
common.globalCheck = false;
3333

34-
console.error('run a string');
34+
// Run a string
3535
const result = vm.runInNewContext('\'passed\';');
3636
assert.strictEqual('passed', result);
3737

38-
console.error('thrown error');
39-
assert.throws(function() {
38+
// Thrown error
39+
assert.throws(() => {
4040
vm.runInNewContext('throw new Error(\'test\');');
4141
}, /^Error: test$/);
4242

@@ -45,7 +45,7 @@ vm.runInNewContext('hello = 2');
4545
assert.strictEqual(5, global.hello);
4646

4747

48-
console.error('pass values in and out');
48+
// Pass values in and out
4949
global.code = 'foo = 1;' +
5050
'bar = 2;' +
5151
'if (baz !== 3) throw new Error(\'test fail\');';
@@ -58,17 +58,17 @@ assert.strictEqual(1, global.obj.foo);
5858
assert.strictEqual(2, global.obj.bar);
5959
assert.strictEqual(2, global.foo);
6060

61-
console.error('call a function by reference');
61+
// Call a function by reference
6262
function changeFoo() { global.foo = 100; }
6363
vm.runInNewContext('f()', { f: changeFoo });
6464
assert.strictEqual(global.foo, 100);
6565

66-
console.error('modify an object by reference');
66+
// Modify an object by reference
6767
const f = { a: 1 };
6868
vm.runInNewContext('f.a = 2', { f: f });
6969
assert.strictEqual(f.a, 2);
7070

71-
console.error('use function in context without referencing context');
71+
// Use function in context without referencing context
7272
const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} });
7373
global.gc();
7474
fn();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const child_process = require('child_process');
55

@@ -11,16 +11,12 @@ const p = child_process.spawn(process.execPath, [
1111
'catch (e) { console.log(e.message); }'
1212
]);
1313

14-
p.stderr.on('data', function(data) {
15-
assert.fail(`Unexpected stderr data: ${data}`);
16-
});
14+
p.stderr.on('data', common.mustNotCall());
1715

1816
let output = '';
1917

20-
p.stdout.on('data', function(data) {
21-
output += data;
22-
});
18+
p.stdout.on('data', (data) => output += data);
2319

24-
process.on('exit', function() {
20+
p.stdout.on('end', common.mustCall(() => {
2521
assert.strictEqual(output.replace(/[\r\n]+/g, ''), 'boo');
26-
});
22+
}));

test/parallel/test-vm-syntax-error-stderr.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ const p = child_process.spawn(process.execPath, [
1212
wrong_script
1313
]);
1414

15-
p.stdout.on('data', function(data) {
16-
assert.fail(`Unexpected stdout data: ${data}`);
17-
});
15+
p.stdout.on('data', common.mustNotCall());
1816

1917
let output = '';
2018

21-
p.stderr.on('data', function(data) {
22-
output += data;
23-
});
19+
p.stderr.on('data', (data) => output += data);
2420

25-
process.on('exit', function() {
21+
p.stderr.on('end', common.mustCall(() => {
2622
assert(/BEGIN CERT/.test(output));
2723
assert(/^\s+\^/m.test(output));
2824
assert(/Invalid left-hand side expression in prefix operation/.test(output));
29-
});
25+
}));

0 commit comments

Comments
 (0)