Skip to content

Commit 7f7dec8

Browse files
cjihrigevanlucas
authored andcommitted
test: cover vm.runInNewContext()
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>
1 parent 8311561 commit 7f7dec8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,23 @@ const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} });
7373
global.gc();
7474
fn();
7575
// Should not crash
76+
77+
{
78+
// Verify that providing a custom filename as a string argument works.
79+
const code = 'throw new Error("foo");';
80+
const file = 'test_file.vm';
81+
82+
assert.throws(() => {
83+
vm.runInNewContext(code, {}, file);
84+
}, (err) => {
85+
const lines = err.stack.split('\n');
86+
87+
assert.strictEqual(lines[0].trim(), `${file}:1`);
88+
assert.strictEqual(lines[1].trim(), code);
89+
// Skip lines[2] and lines[3]. They're just a ^ and blank line.
90+
assert.strictEqual(lines[4].trim(), 'Error: foo');
91+
assert.strictEqual(lines[5].trim(), `at ${file}:1:7`);
92+
// The rest of the stack is uninteresting.
93+
return true;
94+
});
95+
}

0 commit comments

Comments
 (0)