-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
instanceof Object fails? #1769
Comments
Sadly, prototype chains are screwed up due to sandboxing, which we can't really fix (without making jsdom a whole lot slower). The Object in the HTMLInputElement chain is the "real" node Object outside of the sandbox. The Object you're referencing directly is the Object inside the sandbox. |
Is it really? that's not what I see from a debugger:
Edited because I'm stupid |
It's uuuh... very weird. |
In my debugger:
|
Everything looks consistent in my debugger. Simply the end of the proto chain of |
Yeah, we've had this problem with a few other things, namely |
Hmmm.... maybe that's the problem actually, not the solution! Strangely:
So |
I am very confused now, but if I set |
If you do that then |
@Sebmaster I can't make sense of what I have observed in the debugger but anyway... You said the prototype chain of What does JSDOM do to make |
It would require setting up a whole new prototype chain for every jsdom window. That latter point would happen if you overwrite window.Object. |
Not necessarily. Instead of sharing the global
I got that but why? What hackery enables you to change the prototype of a |
Yeah, that would also work. We thought about this a bit. By extending it to also overwrite Function prototypes and similar you could even get the sandbox somewhat safe which would be nice. No one's done the work though.
It's not changing the prototype of {} (that's defined by the sandbox), it's changing what is referenced by Object. |
just thinking... or do you want non-shared Another idea: we could mitigate that problem by using |
hasInstance is a kinda shitty patch though, it just hides the problem a bit better. Walking the prototype chain would still not work for example.
That'd be the optimal solution. But we already have issues with Startup time, both in the require and in the actual document creation, so making that worse is not really an option, which leads to the though that this has to be a config option. Either for all 3 modes, or just 2 of them (shared node-object + separate objects per window). |
Agreed, but it would still be better than today. At least |
Something that works for me is this in module.exports = {
// ...
globals: {
Object: Object,
},
// ...
} thanks to some code in jest-environment-jsdom-fifteen |
is this the same issue ?
|
This help? copy the HTML*Element (etc.) types from the jsdom object and inject them into the global namespace before const { JSDOM } = require('jsdom')
const { instanceofCheck } = require('./instanceofCheck')
// const instanceofCheck = x => x instanceof window.HTMLElement
describe('JSDOM tests', () => {
let jsDomInstance
beforeEach(() => {
jsDomInstance = new JSDOM()
global.HTMLElement = jsDomInstance.window.HTMLElement
})
it('passes instanceof check', () => {
expect(
instanceofCheck(
jsDomInstance.window.document.createElement('div')
)
).toBe(true)
})
}) |
Running a webapp inside JSDOM, I observe the following test failing:
Does it make any sense?
I observe that the prototype chain of the "fake"
HTMLInputElement
goes to a fakeObject
and not the real Node one, but shouldn't theObject
in the code above also point to the same fake object?To be clear: this code is running inside the webapp+JSDOM, not in the main Node code.
The text was updated successfully, but these errors were encountered: