Skip to content

Commit

Permalink
test: cover vm.runInNewContext()
Browse files Browse the repository at this point in the history
There is currently one if branch missing coverage in lib/vm.js.
This commit adds a test to cover the case where the third
argument to runInNewContext() is a string.

PR-URL: #16906
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Nov 17, 2017
1 parent 6c57399 commit 26d529e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/parallel/test-vm-run-in-new-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} });
global.gc();
fn();
// Should not crash

{
// Verify that providing a custom filename as a string argument works.
const code = 'throw new Error("foo");';
const file = 'test_file.vm';

assert.throws(() => {
vm.runInNewContext(code, {}, file);
}, (err) => {
const lines = err.stack.split('\n');

assert.strictEqual(lines[0].trim(), `${file}:1`);
assert.strictEqual(lines[1].trim(), code);
// Skip lines[2] and lines[3]. They're just a ^ and blank line.
assert.strictEqual(lines[4].trim(), 'Error: foo');
assert.strictEqual(lines[5].trim(), `at ${file}:1:7`);
// The rest of the stack is uninteresting.
return true;
});
}

0 comments on commit 26d529e

Please sign in to comment.