-
-
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
Allow overwrite of window.top #1208
Comments
You should be able to do so using |
(Alternately, jsdom's iframe support is pretty solid, so you might be able to use that directly) |
Let me know if anything isn't working with defineProperty! Otherwise, closing for now. |
Sorry for taking so long to get back to you, it seems like defineProperty doesn't work in this case, which is why I opened this issue in the first place (although, I did forget whether I tried this before). Unfortunately for now I can't use the iframe support in jsdom – I need my code to run in an iframe-like environment and I'm not ready yet to enable env switching in jest. I tried this: if (!isTop) {
console.log('top is set before?', !!window.top);
}
Object.defineProperty(window, 'top', {
writable: false,
value: null,
});
if (!isTop) {
console.log('top is set?', !!window.top);
} And |
Investigating. This appears to be an io.js bug with the global/global proxy. |
Commenting out the line this._ownerDocument._defaultView._globalProxy = vm.runInContext("this", this._ownerDocument._global); in level1/core.js "fixes" it, but I have no idea why... |
woah, no idea what is going on here. |
Yeah just kind of giving live updates as I debug... unfortunately this is not as easy as I'd hoped. I'm hoping to find a workaround at the very least though. |
OK, I think I see the issue. Any "contextified" object, whose getters/setters are handled via the V8 named property callbacks, can only have data properties, it seems. That is, when you ask it what properties it has, it will always tell you it has a data property. Even if we've put a getter on the object. Chrome gets this wrong too. It has data properties only on OK, now about that workaround... |
When you say data property, do you mean a regular property using |
Both are the same? |
yes, sorry, I mean whether "data property" is what this kind of value on an object is called. |
By data property I mean any property which reports itself as { value, enumerable, configurable, writable } when queried using Object.getOwnPropertyDescriptor. |
👍 got it. |
OK, the following workaround works. Now I need to figure out how to translate it into something that doesn't use private state and preferably doesn't expose this ridiculous global/global proxy mess to the world: Object.defineProperty(document._global, "top", {
get() {
return "foo";
}
});
console.log("changed?", window.top === "foo");
|
Filed a Node bug. But it's not looking easy to fix. The best bet may be for jsdom to stop using getters for these and go back to data properties. I want a day to think it over, but that seems likely to work. |
OKKKKK another awesome factoid: the So that argues for a There's still the separate problem of |
Sorry for digging this out again. Ist there a similar solution for |
You can just do |
That's what I thought, but it doesn't seem to work that way
Might be because I trying to do it in |
Ah, right, we don't correctly implement [Replaceable]. I'll file another issue for that. In the meantime, Object.defineProperty should work: https://runkit.com/embed/rwmx7ykc0xrq |
@domenic I actually received |
Hello, found this thread while troubleshooting |
I get Object.defineProperty(global, 'top', {
get() {
return {}
},
set() {
return {}
}
}) The setter is necessary else I get this error:
It's also possible to simulate being inside a restricted cross-origin iframe (to avoid nasty surprises in production if someone forgot to do describe('in iframe', () => {
const originalWindowTop = window.top as Window
beforeAll(() => {
Object.defineProperty(global, 'top', {
get() {
throw new Error('Simulating being inside a cross-domain iframe')
},
set() {
return null
}
});
})
afterAll(() => {
// Bypass the getter and setter when restoring the property
Object.defineProperty(global, 'top', originalWindowTop)
})
// do some tests |
Hi! I also received |
I have to downgrade to v20 to overwrite some properties... refer to: https://github.com/jsdom/jsdom/releases/tag/21.0.0 |
After downgrading to v20, new features are not supported and result in new problems... So I have to fork the lastest version of jsdom and publish a new package. |
We have a few tests in jest that attempt to simulate a parent-child relationship between a window and the window of an iframe. With recent versions of jsdom it seems like it isn't possible to overwrite
window.top
any longer. Are you guys interested in supporting this use case? :)The text was updated successfully, but these errors were encountered: