To set the value of window.location.href
for a single test in Jest, replace the window global object:
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
href: `https://my-test-url.com/some-path`
},
writable: true,
});
The writable property is important to be able to set this value again in a separate test within the same suite.
Note: This solution seems verison dependent. See this discussion on Github.