Prototype should is not added when code run in a new context
See original GitHub issueWhen 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:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
The output is:
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 innerconsole.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 outerObject
not the innerObject
. I believe this is due to howrequire
works when passed into the sandbox environment.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.