Unable to change window.location using Object.defineProperty
See original GitHub issueDo you want to request a feature or report a bug? Report a bug
What is the current behavior?
Calling the following from inside a test suite:
Object.defineProperty(location, "hostname", {
value: "example.com",
writable: true
});
throws the following error:
TypeError: Cannot redefine property: hostname
at Function.defineProperty (<anonymous>)
What is the expected behavior?
The code should not throw an exception, and window.location.hostname === "example.com"
should evaluate true.
From the looks of it, jsdom
now sets window.location to be unforgeable. The only way to change the values within window.location
is to use reconfigure
, but (per #2460) Jest doesn’t expose jsdom
for tests to play around with.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest version: 22.0.1 Node version: 8.6.0 Yarn version: 1.2.0 OS: macOS High Sierra 10.13.2
Issue Analytics
- State:
- Created 6 years ago
- Reactions:118
- Comments:81 (4 by maintainers)
Top Results From Across the Web
Unable to change window.location using Object.defineProperty
Unable to change window.location using Object.defineProperty.
Read more >Change "window.location" value and objects within without ...
I am doing this so when a script is being proxied and uses window.location.path , It would output /example instead of /proxy/websiteurl/example ...
Read more >Object.defineProperty() - JavaScript - MDN Web Docs
This method allows a precise addition to or modification of a property on an object. Normal property addition through assignment creates ...
Read more >Error “Cannot redefine non-configurable property 'location'” on ...
defineProperty (obj, "location" . While googling I found that the now the window. location is readonly and not writable, so it cannot be...
Read more >Document.location is not configurable anymore?
It seems like Chrome doesn't allow accessing the getter, setter property of window.location Object. Is this part of any intentional changes? Am not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top 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
Since
location
cannot be overridden directly on the jsdomwindow
object, one possible approach is to override it on a derived object:Here’s a simple solution that works.