-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
instanceof not working on object returned by Rewire #8446
Comments
Confirmed. Probably const vm =require('vm')
vm.runInNewContext(`
const rewire = require("rewire");
const code = rewire("./rewired");
const here = {};
const there = code.__get__("another");
console.log(there.__proto__ === here.__proto__);
`, {require,console}) |
Thank you for the analysis. An issue (#27632) has been submitted to node.js. |
Engineers from node.js think it is a feature that all built-in objects such as
|
Here are some test codes to simulate this issue. const vm = require('vm')
const code = `
const rewire = require("rewire");
const code = rewire("./rewired")
const there = code.__get__("there");
const obj = code.__get__("Object");
console.log(there instanceof obj);
console.log(there instanceof Object);
`;
eval(code)
vm.runInNewContext(code, {require, console})
vm.runInNewContext(code, {require, console, Object}) Result is true
true
true
false
true
true It seems we can have two workarounds for this issue before it can be fixed. One is to use the base classes from |
In that case, this is actually the same issue as #2549 |
Yes #2549 looks like the same issue. I changed the code again and removed the const vm = require('vm')
const code = `console.log(require("./rewired").there instanceof Object);`;
eval(code)
vm.runInNewContext(code, {require, console})
vm.runInNewContext(code, {require, console, Object})
exports.there = {} The result is true
false
true Obviously this issue has spreaded to the any code reference to the tested source file. This is inevitable in any kind of test. Issue will always happen when If Node is not willing to change this behaviour, I believe Jest need to do something here. Either discard the way of starting new vm context, or allow the user to pass in the base objects when needed. |
Got confirmation from Node developer that objects should use the context of their definition, not execution. So they don't think this is a problem. But for Jest this is a problem. |
Let's close this to keep any discussion centralized in #2549. It's a long standing issue and there's a lot of info and a bounty, but it's a complex topic that might not be properly fixed in the near future :| |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
馃悰 Bug Report
instanceof
doesn't work as expected in Jest when being used on an object returned from Rewire. Please check out codes below.I thought it was the problem of Rewire. However this issue can't be reproduced under pure node.js with Rewire.
I checked the
prototype
of the remote object. It just looks like a normal object. Don't understand what made it unrecognizable byinstanceof
.To Reproduce
file
rewired.test.js
file
rewired.js
Run
npx envinfo --preset jest
Paste the results here:
System: OS: macOS 10.14.4 CPU: (8) x64 Intel(R) Core(TM) i7-4960HQ CPU @ 2.60GHz Binaries: Node: 12.1.0 - /usr/local/bin/node Yarn: 1.7.0 - /usr/local/bin/yarn npm: 6.9.0 - /usr/local/bin/npm npmPackages: jest: ^24.7.1 => 24.7.1
The text was updated successfully, but these errors were encountered: