node/test/parallel/test-vm-function-declaration.js
James M Snell f35f06d04c
test: improve multiple vm tests
PR-URL: https://github.com/nodejs/node/pull/14458
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
2017-09-20 10:48:20 -04:00

23 lines
870 B
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const vm = require('vm');
const o = vm.createContext({ console: console });
// Function declaration and expression should both be copied to the
// sandboxed context.
let code = 'var a = function () {};\n';
code += 'function b(){}\n';
// Grab the global b function as the completion value, to ensure that
// we are getting the global function, and not some other thing
code += '(function(){return this})().b;\n';
const res = vm.runInContext(code, o, 'test');
assert.strictEqual(typeof res, 'function', 'result should be function');
assert.strictEqual(res.name, 'b', 'res should be named b');
assert.strictEqual(typeof o.a, 'function', 'a should be function');
assert.strictEqual(typeof o.b, 'function', 'b should be function');
assert.strictEqual(res, o.b, 'result should be global b function');