jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: `jest.mockWindowLocation`

Open everett1992 opened this issue 4 months ago • 7 comments

🚀 Feature Proposal

The jest 30 release notes acknowledge that jsdom no longer supports reassigning window.location. There are some jest issues closed as a jsdom issue, and jsdom's issue is closed as not planned. https://github.com/jestjs/jest/issues/15674 https://github.com/jestjs/jest/issues/5124 https://github.com/jsdom/jsdom/issues/3492

The release notes say @jest/environment-jsdom-abstract should help, but doesn't make it clear how. They also say jest may look for an alternative that is better suited for testing.

Scouring the linked issues I found some workarounds that involve getting a reference to the jsdom instance and calling reconfigure. This seems like a pretty good solution. Could jest expose that in the official jsdom environment or as a jest function?

Motivation

The activity on the issues makes this seem like a common use case. There's a lot of flack on the jsdom issue to support reconfiguring the window.loction, but it seems jsdom has a method, it just isn't exposed by jest.

Adding a mock function that calls jsdom.reconfigure seems like good UX and less effort than replacing jsdom, or asking packages to patch their installed jsdom.

Example

No response

Pitch

everett1992 avatar Aug 05 '25 23:08 everett1992

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Sep 05 '25 00:09 github-actions[bot]

Please don't auto-close this issue.

mikevoets avatar Sep 21 '25 20:09 mikevoets

I'd be happy to work on a PR if there was any indication that jest would merge it.

everett1992 avatar Sep 22 '25 16:09 everett1992

+1 maybe we could at least update the release note to clarify how @jest/environment-jsdom-abstract is intended to help?

jordan-cutler avatar Sep 27 '25 19:09 jordan-cutler

I use @jest/environment-jsdom-abstract to implement inject a mocked location object into JSDOM. While it works without jest, it doesn't work in jest. Maybe you'll find this helpful:

https://stackoverflow.com/questions/79784243/mock-jsdom-location-using-jest-environment-jsdom-abstract

htho avatar Oct 07 '25 06:10 htho

@everett1992 my PRs got merged steadily over the time, weekly-wise. So I say, go for it :D

hainenber avatar Oct 11 '25 04:10 hainenber

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 10 '25 05:11 github-actions[bot]

Hey folks! Any traction on this - is there a plan to implement or accept this feature?

kabirbaidhya avatar Dec 12 '25 16:12 kabirbaidhya