Skip to content
Prev Previous commit
Next Next commit
test: improve test-vm-context
  • Loading branch information
jasnell committed Aug 1, 2017
commit 1556d3b52b98492e67341509124cb52197cbb363
16 changes: 8 additions & 8 deletions test/parallel/test-vm-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ const vm = require('vm');
const Script = vm.Script;
let script = new Script('"passed";');

console.error('run in a new empty context');
// Run in a new empty context
let context = vm.createContext();
let result = script.runInContext(context);
assert.strictEqual('passed', result);

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

console.error('test updating context');
// Test updating context
script = new Script('foo = 3;');
result = script.runInContext(context);
assert.strictEqual(3, context.foo);
assert.strictEqual('lala', context.thing);

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

// Issue GH-1140:
console.error('test runInContext signature');
// Test runInContext signature
let gh1140Exception;
try {
vm.runInContext('throw new Error()', context, 'expected-filename.js');
Expand Down Expand Up @@ -77,7 +77,7 @@ const contextifiedSandboxErrorMsg =
});

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

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

Expand Down