Skip to content

Commit 2a1ebae

Browse files
cjihrigMylesBorins
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 2043ce3 commit 2a1ebae

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
@@ -52,3 +52,23 @@ const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} });
5252
global.gc();
5353
fn();
5454
// Should not crash
55+
56+
{
57+
// Verify that providing a custom filename as a string argument works.
58+
const code = 'throw new Error("foo");';
59+
const file = 'test_file.vm';
60+
61+
assert.throws(() => {
62+
vm.runInNewContext(code, {}, file);
63+
}, (err) => {
64+
const lines = err.stack.split('\n');
65+
66+
assert.strictEqual(lines[0].trim(), `${file}:1`);
67+
assert.strictEqual(lines[1].trim(), code);
68+
// Skip lines[2] and lines[3]. They're just a ^ and blank line.
69+
assert.strictEqual(lines[4].trim(), 'Error: foo');
70+
assert.strictEqual(lines[5].trim(), `at ${file}:1:7`);
71+
// The rest of the stack is uninteresting.
72+
return true;
73+
});
74+
}

0 commit comments

Comments
 (0)