Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add others vm hooks to allow mocking dependencies #465

Closed
wants to merge 8 commits into from

Conversation

danieljoppi
Copy link

Add vm.runInNewContext: the same implementation of @laurentgoudet in #294 (fixes #293)
Add vm.runInContext: its very similar a vm.runInNewContext
and
Add support to Script.runInContext to fixes #254

@danieljoppi
Copy link
Author

@gotwarlost I was think ... We can remove this hooks: vm.runInContext, vm.runInNewContext and vm.runInThisContext .

All there functions using vm.createScript - look:

exports.runInContext = function(code, contextifiedSandbox, options) {
  var script = new Script(code, options);
  return script.runInContext(contextifiedSandbox, options);
};
exports.runInNewContext = function(code, sandbox, options) {
  var script = new Script(code, options);
  return script.runInNewContext(sandbox, options);
};
exports.runInThisContext = function(code, options) {
  var script = new Script(code, options);
  return script.runInThisContext(options);
};

We only needed hook the vm.createScript.

@gotwarlost
Copy link
Owner

Not true - those other functions use the script constructor directly - they don't use createScript

@@ -139,6 +149,22 @@ function unhookRequire() {
function hookCreateScript(matcher, transformer, opts) {
opts = opts || {};
var fn = transformFn(matcher, transformer, opts.verbose);

var orgRunInContext = originalScript.prototype.runInContext,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these assignments to top-level variables so that they are initialized early - otherwise by the time someone calls hookCreateScript - the original values could have been re-assigned.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@geovanisouza92
Copy link

What's the state about this?

I'm trying to test scripts that will be run on a embedded v8 context. So, I'm using Jest using Script.runInContext with mocked globals. The tests works, but no coverage is collected.

@danieljoppi danieljoppi closed this Nov 1, 2017
@danieljoppi danieljoppi deleted the vm-hook branch November 1, 2017 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Coverage not generated when mocking dependencies using vm.runInNewContext script.runInContext support
3 participants