question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to change window.location using Object.defineProperty

See original GitHub issue

Do 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:closed
  • Created 6 years ago
  • Reactions:118
  • Comments:81 (4 by maintainers)

github_iconTop GitHub Comments

83reactions
RubenVerborghcommented, Aug 23, 2018

Since location cannot be overridden directly on the jsdom window object, one possible approach is to override it on a derived object:

global.window = Object.create(window);
Object.defineProperty(window, 'location', {
  value: {
    href: 'http://example.org/'
  }
});
70reactions
vastuscommented, Jul 26, 2018

Here’s a simple solution that works.

describe('changing location', () => {
  const testURL = location.href;

  beforeEach(() => history.replaceState({}, 'Login', '/login'));
  afterEach(() => history.replaceState({}, 'Home', '/'));

  it('works', () => {
    expect(location.pathname).toBe('/login');
  });
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found