Skip to content

refactor: remove deprecated url.parse() in favor of WHATWG URL API#2470

Merged
sjinks merged 1 commit intotrunkfrom
fix/url-parse-deprecation
Jul 23, 2025
Merged

refactor: remove deprecated url.parse() in favor of WHATWG URL API#2470
sjinks merged 1 commit intotrunkfrom
fix/url-parse-deprecation

Conversation

@sjinks
Copy link
Copy Markdown
Member

@sjinks sjinks commented Jul 21, 2025

Description

This pull request refactors test files to replace the deprecated url.parse method with the modern WHATWG URL API for parsing and handling URLs. This change simplifies the code and ensures compatibility with the current JavaScript standards.

Refactoring to use the WHATWG URL API:

Changelog Description

Fixed

  • Replaced the deprecated ``url.parse()` with the WHATWG URL API

Pull request checklist

New release checklist

Steps to Test

CI must pass.

@sjinks sjinks requested review from ariskataoka and Copilot July 21, 2025 03:02
@sjinks sjinks self-assigned this Jul 21, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modernizes URL parsing by replacing the deprecated Node.js url.parse() method with the WHATWG URL API across test files. This change improves code maintainability and ensures compatibility with current JavaScript standards.

  • Replaced url.parse() with URL constructor for parsing URLs
  • Simplified URL property access using WHATWG URL API properties
  • Removed unnecessary test environment setup code in tracks test

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
__tests__/lib/validations/is-multisite-domain-mapped.js Updated URL parsing in test setup to use WHATWG URL API
__tests__/lib/analytics/clients/tracks.js Refactored URL parsing and simplified buildNock function, removed environment setup code
Comments suppressed due to low confidence (2)

tests/lib/analytics/clients/tracks.js:7

  • The variable name 'url' conflicts with the removed 'url' module import and may be confusing. Consider renaming to 'endpointUrl' or 'tracksUrl' for clarity.
	const url = new URL( Tracks.ENDPOINT );

tests/lib/validations/is-multisite-domain-mapped.js:82

  • The variable name 'url' conflicts with the removed 'url' module import and may be confusing. Consider renaming to 'apiUrl' or 'parsedUrl' for clarity.
			const url = new URL( API_URL );

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 21, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Comment on lines -32 to +31
const folder = arg.join(); // File comes in as an array as part of the args- turn it into a string
arg = url.parse( folder ); // Then parse the file to its URL parts
const filePath = arg.path; // Extract the path of the file
const filePath = arg.join(); // File comes in as an array as part of the args- turn it into a string
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ariskataoka, do you happen to remember why we used url.parse() here?

As far as I understand, folder is a path on a user's file system, such as ~/user/path/to/uploads; it is never a URL. Why do we attempt to parse it as a URL?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code appeared in d6ea0c0

Comment on lines -21 to -34
/**
* Allow overriding of process variables per test
* Adapted from https://stackoverflow.com/a/48042799
*/
const OLD_ENV = process.env;

beforeEach( () => {
jest.resetModules();
process.env = { ...OLD_ENV };
} );

afterEach( () => {
process.env = OLD_ENV;
} );
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment overrides are not used in tests.

@sjinks
Copy link
Copy Markdown
Member Author

sjinks commented Jul 21, 2025

/changelog

@github-actions
Copy link
Copy Markdown
Contributor

AI-Generated Changelog Entry

Improve compatibility and reliability by updating URL handling to use modern standards instead of outdated methods; this change has no visible effect for most users.

@sjinks sjinks force-pushed the fix/url-parse-deprecation branch from 6b24c85 to a32bed1 Compare July 21, 2025 03:07
@sonarqubecloud
Copy link
Copy Markdown

@sjinks sjinks merged commit 1c0d32d into trunk Jul 23, 2025
19 checks passed
@sjinks sjinks deleted the fix/url-parse-deprecation branch July 23, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants