Complete Jest 30 upgrade with refactored location mocking#832
Complete Jest 30 upgrade with refactored location mocking#832hogashi merged 3 commits intorenovate/major-jest-monorepofrom
Conversation
Co-authored-by: hogashi <12870472+hogashi@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot explain what you've done and why for review from me |
What I DidFixed the CI test failures that occurred after upgrading to Jest 30 by downgrading Why This Was NeededThe Problem:
Root Cause:
The Solution:
Changes Made
All 161 tests now pass ✅ |
|
@copilot is there a way to change tests to upgrade it to v30? |
Yes, there are two main approaches to upgrade to jest-environment-jsdom v30: Option 1: Refactor to Pass Location as Parameter (Cleanest)Refactor // src/constants.ts
export const isTwitter = (location = window.location): boolean =>
location.hostname === HOST_TWITTER_COM ||
location.hostname === HOST_MOBILE_TWITTER_COM ||
location.hostname === HOST_X_COM ||
location.hostname === HOST_MOBILE_X_COM;
export const isTweetdeck = (location = window.location): boolean =>
location.hostname === HOST_TWEETDECK_TWITTER_COM ||
location.hostname === HOST_PRO_TWITTER_COM ||
location.hostname === HOST_PRO_X_COM;Then tests can pass a mocked location: it('tweetdeck.twitter.com', () => {
const mockLocation = new URL('https://tweetdeck.twitter.com');
expect(isTwitter(mockLocation)).toBeFalsy();
expect(isTweetdeck(mockLocation)).toBeTruthy();
});Option 2: Use
|
|
@copilot yes |
- Modified isTwitter() and isTweetdeck() to accept optional location parameter - Updated Constants tests to pass mock location objects instead of mocking window.location - Updated Utils tests to use jsdom.reconfigure() for location changes - Added jest-environment-jsdom-global for jsdom.reconfigure() support - Updated test expectations for CSS differences in jsdom 30 (color format, padding) - Upgraded jest-environment-jsdom from v29.7.0 to v30.2.0 - All 161 tests passing with Jest 30 and jsdom 30 Co-authored-by: hogashi <12870472+hogashi@users.noreply.github.com>
Done in 6425c3f! I've refactored the code to fully support jest-environment-jsdom v30. Changes made:
All 161 tests pass ✅ |
Jest 30 uses jsdom 26+ which locks
window.locationproperties, breaking tests that mock location for URL-based logic. This PR completes the Jest 30 upgrade by refactoring code to be compatible with jest-environment-jsdom v30.Changes
Code Refactoring
isTwitter()andisTweetdeck()to accept optionallocationparameter (defaults towindow.location)(location: Location | URL = window.location): booleanTest Updates
window.locationjsdom.reconfigure({ url: '...' })for location changesDependencies
jest-environment-jsdomfrom^29.7.0to^30.2.0jest-environment-jsdom-globalto enablejsdom.reconfigure()in testsjest-environment-jsdom-globalhttps://jestjs.io/docs/snapshot-testing)Approach
The refactoring uses dependency injection to make location-dependent functions testable without mocking
window.location. This approach:All 161 tests pass with Jest 30.2.0 and jsdom 30.2.0 ✅
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.