question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prototype should is not added when code run in a new context

See original GitHub issue

When running the code in a new context, should is not added to the prototype. This code fail with “Cannot read property ‘be’ of undefined”

const vm = require("vm");

const code = "const chai = require(\"chai\");\n" +
	"chai.should();\n" +
	"const test = \"test\";\n" +
	"test.should.be.equal(\"test\");\n";
vm.runInNewContext(code, { global, require }, { filename: "test.js"});

A reproduction is available in this repository

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
meebercommented, Oct 3, 2017

Disclaimer: I’m not an expert on Node’s VM module.

I don’t think what you’re doing is going to work. Consider this example without Chai:

const vm = require("vm");

Object.prototype.meow = 42;
global.Object.prototype.purr = 43;

console.log('Outer', {}.meow, {}.purr);

const code = "console.log('Inner', {}.meow, {}.purr)\n";
vm.runInNewContext(code, { console, global, require }, { filename: "test.js"});

The output is:

Outer 42 43 Inner undefined undefined

Even if you move Object.prototype.meow = 42; global.Object.prototype.purr = 43; into a separate file, and then require that file from within the sandbox, the inner console.log will still display “Inner undefined undefined”.

In terms of Chai: Essentially what’s happening is that calls to Object.prototype inside of Chai’s code are referencing the outer Object not the inner Object. I believe this is due to how require works when passed into the sandbox environment.

0reactions
keithamuscommented, Jun 9, 2018

Hey @CodeTroopers thanks for the issue.

We’ve added this to our Roadmap https://github.com/chaijs/chai/projects/2! We’ll be releasing chai 5 soon, but for now I’ll close this issue because it is tracked on our roadmap.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should we add methods only to the prototypes of our objects?
I'm currently studying Javascript and saw that we can define members of an object in its prototype instead of in the object's constructor...
Read more >
Javascript Prototype & Scope Chains: What You Need to Know
Avoid performance ramifications that can result from using JavaScript prototypes and scopes in this thorough guide with examples.
Read more >
What to do when “this” loses context - freeCodeCamp
A way to fix the problem is to create new functions in the constructor using bind(this) . constructor(){ super(); this.todos = []; this....
Read more >
this - JavaScript | MDN
In most cases, the value of this is determined by how a function is called (runtime binding). It can't be set by assignment...
Read more >
Cannot extend global prototypes in the REPL #771 - GitHub
When running a regular node script via node script.js , other modules can extend the global prototype's of the built-in JavaScript classes (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found