Skip to content

test(logger): add e2e test for ENOTFOUND error log#1233

Open
chimurai wants to merge 1 commit into
masterfrom
test-logger-e2e-ENOTFOUND
Open

test(logger): add e2e test for ENOTFOUND error log#1233
chimurai wants to merge 1 commit into
masterfrom
test-logger-e2e-ENOTFOUND

Conversation

@chimurai
Copy link
Copy Markdown
Owner

@chimurai chimurai commented May 14, 2026

add missing e2e logger test for ENOTFOUND

related: unjs/httpxy#135

Summary by CodeRabbit

  • Refactor

    • Improved type safety in proxy error logging to more precisely handle undefined targets and ensure consistent log output.
  • Tests

    • Added an end-to-end test that exercises a failing proxy target, verifies a 504 response, and asserts the logger records the connection error details.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

📝 Walkthrough

Walkthrough

Narrows a cast in the logger plugin so targetHref reads from (target as URL)?.href and adds an end-to-end Vitest that asserts a 504 response and a single logger.error call with a snapshoted error message for an unreachable proxy target.

Changes

Proxy Error Logger

Layer / File(s) Summary
Proxy error logging type safety and E2E validation
src/plugins/default/logger-plugin.ts, test/e2e/logger.spec.ts
Type cast for targetHref is narrowed from any to URL for type precision. E2E test verifies the logger formats error messages with request and target metadata when proxy targets do not exist, asserting a single logger.error call and snapshot validation.

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • chimurai/http-proxy-middleware#1215: Both PRs modify src/plugins/default/logger-plugin.ts error/proxy logging logic that formats the logged target URL (this PR adjusts targetHref casting; related PR changes URL construction to use createUrl and avoid undefined/invalid URL parts).

Poem

A rabbit logs with gentle paw,
From fuzzy any to URL law.
When targets vanish in the mist,
Tests catch errors on the list.
Hop, hop — the logs persist! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding an e2e test for ENOTFOUND error logging, which is the primary focus of the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test-logger-e2e-ENOTFOUND

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/plugins/default/logger-plugin.ts`:
- Line 29: The log builds targetHref unsafely by casting target to URL, causing
the string "undefined" when target is absent; change the derivation to safely
check the payload: if target is an instance of URL use target.href, else if
target is an object with a href property coerce that to string, otherwise use an
empty string (or "unknown") — update the expression where targetHref is defined
(the const targetHref in logger-plugin.ts) to perform these runtime checks
instead of an unchecked cast.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2df43af5-0677-4198-906f-a9c5db46e73d

📥 Commits

Reviewing files that changed from the base of the PR and between ffbf5bd and 39cb266.

📒 Files selected for processing (2)
  • src/plugins/default/logger-plugin.ts
  • test/e2e/logger.spec.ts

Comment thread src/plugins/default/logger-plugin.ts Outdated
@chimurai chimurai force-pushed the test-logger-e2e-ENOTFOUND branch from 39cb266 to abacb50 Compare May 14, 2026 11:38
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
src/plugins/default/logger-plugin.ts (1)

29-29: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Test failures confirm target is undefined during ENOTFOUND errors.

The pipeline failures show the test receives "undefined" instead of "http://does-not-exist.invalid" in the logged error message. This confirms the existing review comment: when target is undefined, the cast to URL and optional chaining produce undefined, which stringifies to "undefined" in the template literal. The fix should fall back to options.target when the target parameter is absent.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/plugins/default/logger-plugin.ts` at line 29, The logged targetHref is
becoming the string "undefined" because (target as unknown as URL)?.href yields
undefined when target is missing; change construction of targetHref to use
options.target as a fallback (e.g., derive a string from target?.href ||
options.target) so that when target is undefined the log uses options.target
instead; update the code that defines targetHref (referencing the target
variable and options.target) to coalesce to a sensible string rather than
allowing "undefined".
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@src/plugins/default/logger-plugin.ts`:
- Line 29: The logged targetHref is becoming the string "undefined" because
(target as unknown as URL)?.href yields undefined when target is missing; change
construction of targetHref to use options.target as a fallback (e.g., derive a
string from target?.href || options.target) so that when target is undefined the
log uses options.target instead; update the code that defines targetHref
(referencing the target variable and options.target) to coalesce to a sensible
string rather than allowing "undefined".

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d0844273-3afa-4756-ae64-7ae490c8dfad

📥 Commits

Reviewing files that changed from the base of the PR and between 39cb266 and abacb50.

📒 Files selected for processing (2)
  • src/plugins/default/logger-plugin.ts
  • test/e2e/logger.spec.ts

@chimurai chimurai force-pushed the test-logger-e2e-ENOTFOUND branch from abacb50 to 0ef8c6f Compare May 19, 2026 19:16
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/e2e/logger.spec.ts`:
- Around line 33-37: The test fails because the logger's targetHref is becoming
undefined for string targets; update the logger code that computes targetHref
(used by format(message, requestHref, targetHref, errorCode, errorReference)) so
it properly resolves string targets into a concrete URL: detect when the proxy
target is a string and coerce it to a full URL (e.g., use new URL(targetString,
requestHref) or targetString.toString()) or otherwise serialize target objects
into an absolute href, then pass that resulting string into targetHref so the
snapshot assertion shows the real URL instead of undefined.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0a8f164a-6728-4336-ba91-cf8d4d6015a9

📥 Commits

Reviewing files that changed from the base of the PR and between abacb50 and 0ef8c6f.

📒 Files selected for processing (2)
  • src/plugins/default/logger-plugin.ts
  • test/e2e/logger.spec.ts
✅ Files skipped from review due to trivial changes (1)
  • src/plugins/default/logger-plugin.ts

Comment thread test/e2e/logger.spec.ts
Comment on lines +33 to +37
expect(
format(message, requestHref, targetHref, errorCode, errorReference),
).toMatchInlineSnapshot(
`"[HPM] Error occurred while proxying request example.test/my-path to http://does-not-exist.invalid/ [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)"`,
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

CI is red because targetHref resolves to undefined; fix logger target URL resolution before merge.

This assertion is currently failing across runtimes (to undefined), which indicates the logger path is not deriving targetHref correctly for this proxy target. Keep this expectation (it captures the intended behavior) and fix the logger implementation so string targets log a concrete URL.

🧰 Tools
🪛 GitHub Check: Coverage

[failure] 35-35: test/e2e/logger.spec.ts > logger > should log target ENOTFOUND errors
Error: Snapshot logger > should log target ENOTFOUND errors 1 mismatched

Expected: ""[HPM] Error occurred while proxying request example.test/my-path to http://does-not-exist.invalid/ [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""
Received: ""[HPM] Error occurred while proxying request example.test/my-path to undefined [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""

❯ test/e2e/logger.spec.ts:35:7

🪛 GitHub Check: Test (22.x)

[failure] 35-35: test/e2e/logger.spec.ts > logger > should log target ENOTFOUND errors
Error: Snapshot logger > should log target ENOTFOUND errors 1 mismatched

Expected: ""[HPM] Error occurred while proxying request example.test/my-path to http://does-not-exist.invalid/ [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""
Received: ""[HPM] Error occurred while proxying request example.test/my-path to undefined [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""

❯ test/e2e/logger.spec.ts:35:7

🪛 GitHub Check: Test (24.x)

[failure] 35-35: test/e2e/logger.spec.ts > logger > should log target ENOTFOUND errors
Error: Snapshot logger > should log target ENOTFOUND errors 1 mismatched

Expected: ""[HPM] Error occurred while proxying request example.test/my-path to http://does-not-exist.invalid/ [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""
Received: ""[HPM] Error occurred while proxying request example.test/my-path to undefined [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""

❯ test/e2e/logger.spec.ts:35:7

🪛 GitHub Check: Test (26.x)

[failure] 35-35: test/e2e/logger.spec.ts > logger > should log target ENOTFOUND errors
Error: Snapshot logger > should log target ENOTFOUND errors 1 mismatched

Expected: ""[HPM] Error occurred while proxying request example.test/my-path to http://does-not-exist.invalid/ [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""
Received: ""[HPM] Error occurred while proxying request example.test/my-path to undefined [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)""

❯ test/e2e/logger.spec.ts:35:7

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/e2e/logger.spec.ts` around lines 33 - 37, The test fails because the
logger's targetHref is becoming undefined for string targets; update the logger
code that computes targetHref (used by format(message, requestHref, targetHref,
errorCode, errorReference)) so it properly resolves string targets into a
concrete URL: detect when the proxy target is a string and coerce it to a full
URL (e.g., use new URL(targetString, requestHref) or targetString.toString()) or
otherwise serialize target objects into an absolute href, then pass that
resulting string into targetHref so the snapshot assertion shows the real URL
instead of undefined.

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.

1 participant