Skip to content

Stubbing/patching window.location.href in JSDom 8? #1388

Description

@dylanpyle

We've got some code in use in tests that worked in jsdom 7 but appears not to work in jsdom 8.

Our JSDom setup:

  const doc = jsdom.jsdom('<html><body></body></html>', {
    referrer: 'https://example.com/baz',
    url: 'https://example.com/foo'
  });

  global.document = doc;
  global.window = doc.defaultView;

And the test itself:

      window.location.href = 'https://newurl.com/foo';
      // some assertions that depend on window.location

Adding some logging immediately after setting window.location.href shows that the value wasn't actually updated or changed, and other means overriding (e.g. stubbing with sinon) don't seem to work either since it's implemented via getters/setters rather than an actual property. What's the best way to override this value?

It looks like setting is supposed to do something based on https://github.com/tmpvar/jsdom/blob/master/lib/jsdom/living/window/Location-impl.js#L46 but I can't figure out what the final effect of that is supposed to be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions