Skip to content

refactor: replace node-fetch/nock with undici#2837

Open
sjinks wants to merge 4 commits into
trunkfrom
migrate/undici
Open

refactor: replace node-fetch/nock with undici#2837
sjinks wants to merge 4 commits into
trunkfrom
migrate/undici

Conversation

@sjinks
Copy link
Copy Markdown
Member

@sjinks sjinks commented May 11, 2026

Description

nock is unreliable and does not play well with Jest. There is an unresolved issue: nock/nock#2802, and we're fighting with nock in Automattic/vip-go-api#7153 and #2821.

We remove nock and node-fetch in favor of undici. The best thing about undici is that it comes with built-in testing capabilities, and we don't need another dependency that may or may not work with our test setup.

  • Remove node-fetch, nock, and fetch-retry dependencies
  • Add undici as the sole fetch/mock runtime
  • Migrate all runtime fetch calls (api/http, analytics, dev-environment-core, client-file-uploader) to undici fetch
  • Replace CommonJS fetch-retry wrapper with a local exponential backoff helper in client-file-uploader
  • Use node:timers/promises setTimeout in retry.ts and client-file-uploader (eliminates manual Promise wrapping)
  • Add src/lib/http/proxy-dispatcher.ts for undici-compatible proxy support (ProxyAgent)
  • Replace jest.setup.js nock setup with undici MockAgent as a global dispatcher; add test-utils/undici-mock.js helper
  • Migrate all nock-based tests to undici MockPool interceptors
  • Remove nock lifecycle calls from DevEnv E2E specs

Pull request checklist

New release checklist

Steps to Test

Surprisingly, CI must pass.

@sjinks sjinks self-assigned this May 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 11, 2026

Dependency Review

The following issues were found:
  • ✅ 0 vulnerable package(s)
  • ✅ 0 package(s) with incompatible licenses
  • ✅ 0 package(s) with invalid SPDX license definitions
  • ⚠️ 1 package(s) with unknown licenses.
See the Details below.

License Issues

package.json

PackageVersionLicenseIssue Type
undici^7.0.0NullUnknown License

OpenSSF Scorecard

PackageVersionScoreDetails
npm/undici ^7.0.0 UnknownUnknown

Scanned Files

  • package.json

Comment thread src/lib/client-file-uploader.ts Fixed
Comment thread src/lib/client-file-uploader.ts Fixed
- Remove node-fetch, nock, and fetch-retry dependencies
- Add undici as the sole fetch/mock runtime
- Migrate all runtime fetch calls (api/http, analytics,
  dev-environment-core, client-file-uploader) to undici fetch
- Replace CommonJS fetch-retry wrapper with a local exponential
  backoff helper in client-file-uploader
- Use node:timers/promises setTimeout in retry.ts and
  client-file-uploader (eliminates manual Promise wrapping)
- Add src/lib/http/proxy-dispatcher.ts for undici-compatible
  proxy support (ProxyAgent)
- Replace jest.setup.js nock setup with undici MockAgent as
  global dispatcher; add test-utils/undici-mock.js helper
- Migrate all nock-based tests to undici MockPool interceptors
- Remove nock lifecycle calls from devenv e2e specs
@sjinks sjinks marked this pull request as ready for review May 11, 2026 23:46
Copilot AI review requested due to automatic review settings May 11, 2026 23:46
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 migrates VIP CLI’s HTTP stack from node-fetch + nock to undici for both runtime fetches and test-time request mocking, and introduces an undici-compatible proxy dispatcher.

Changes:

  • Replace node-fetch usage across runtime codepaths with undici.fetch, including proxy support via a new createProxyDispatcher().
  • Replace nock-based Jest test mocking with an undici MockAgent global dispatcher and MockPool interceptors.
  • Remove fetch-retry and implement a local retry helper (plus node:timers/promises usage) for upload operations.

Reviewed changes

Copilot reviewed 29 out of 30 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
test-utils/undici-mock.js Adds helpers to access/reset the global undici mock dispatcher/pools in tests.
src/lib/tracker.ts Removes node-fetch import while keeping analytics tracking plumbing.
src/lib/retry.ts Switches delay implementation to node:timers/promises setTimeout.
src/lib/http/proxy-dispatcher.ts New undici ProxyAgent-based dispatcher factory for proxy support.
src/lib/dev-environment/dev-environment-core.ts Migrates WordPress versions manifest fetch to undici + dispatcher proxy support.
src/lib/client-file-uploader.ts Migrates to undici fetch and replaces fetch-retry with a local retry loop.
src/lib/api/http.ts Migrates API HTTP wrapper from node-fetch to undici + proxy dispatcher.
src/lib/api.ts Removes FetchError dependency and adjusts retry predicate for connection-refused errors.
src/lib/analytics/index.ts Removes node-fetch type import while keeping analytics aggregation logic.
src/lib/analytics/clients/tracks.ts Migrates Tracks client to undici fetch and keeps Response typing local.
src/lib/analytics/clients/pendo.ts Removes node-fetch type import for Pendo client.
src/lib/analytics/clients/client.ts Removes node-fetch type import from the analytics client interface.
package.json Drops node-fetch, nock, fetch-retry; adds undici.
npm-shrinkwrap.json Updates lockfile to reflect dependency removals/additions (including undici).
jest.setup.js Replaces nock global setup with undici MockAgent dispatcher + proxy env cleanup.
tests/lib/validations/sql.js Removes node-fetch mocking previously used in this suite.
tests/lib/validations/is-multisite-domain-mapped.js Migrates GraphQL request mocking from nock to undici MockPool.
tests/lib/search-and-replace.js Removes node-fetch mocking previously used in this suite.
tests/lib/dev-environment/dev-environment-cli.js Replaces nock-based versions manifest mocking with a getVersionList spy.
tests/lib/api-retry.ts Updates retry tests to no longer depend on FetchError.
tests/lib/analytics/clients/tracks.js Migrates Tracks client tests from nock to undici MockPool assertions.
tests/devenv-e2e/002-destroy.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/003-start.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/004-stop.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/006-list.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/007-info.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/008-exec.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/010-import-sql.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/011-logs.spec.js Removes nock lifecycle calls from dev-env E2E spec.
tests/devenv-e2e/012-shell.spec.js Removes nock lifecycle calls from dev-env E2E spec.
Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread src/lib/analytics/clients/client.ts
Comment thread package.json Outdated
Comment thread npm-shrinkwrap.json Outdated
Comment thread src/lib/http/proxy-dispatcher.ts
Comment thread src/lib/api/http.ts Outdated
Comment thread src/lib/analytics/index.ts
Comment thread src/lib/tracker.ts
Comment thread src/lib/analytics/clients/pendo.ts
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

Copilot reviewed 29 out of 30 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread src/lib/client-file-uploader.ts
Comment thread src/lib/http/proxy-dispatcher.ts Outdated
Comment thread src/lib/http/proxy-dispatcher.ts
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

Copilot reviewed 30 out of 31 changed files in this pull request and generated 4 comments.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread src/lib/http/proxy-dispatcher.ts
Comment thread src/lib/http/proxy-dispatcher.ts
Comment thread src/lib/client-file-uploader.ts
Comment thread src/lib/client-file-uploader.ts Outdated
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

Copilot reviewed 31 out of 32 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread jest.setup.js
Comment thread src/lib/http/proxy-dispatcher.ts Outdated
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

Copilot reviewed 33 out of 34 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread src/lib/http/proxy-dispatcher.ts
Comment thread src/lib/http/proxy-dispatcher.ts
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

Copilot reviewed 33 out of 34 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread src/lib/api/http.ts
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

Copilot reviewed 33 out of 34 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

Comment thread src/lib/api/http.ts
Comment thread src/lib/http/download-file.ts
Comment thread src/lib/media-import/status.ts
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

Copilot reviewed 33 out of 34 changed files in this pull request and generated no new comments.

Files not reviewed (1)
  • npm-shrinkwrap.json: Language not supported

@sjinks
Copy link
Copy Markdown
Member Author

sjinks commented May 12, 2026

This will close #2821

@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants