Skip to content

chore: fix test breakage in error-reporting - #8966

Merged
pearigee merged 3 commits into
mainfrom
pearigee-fix-error-reporting
Jul 24, 2026
Merged

chore: fix test breakage in error-reporting#8966
pearigee merged 3 commits into
mainfrom
pearigee-fix-error-reporting

Conversation

@pearigee

@pearigee pearigee commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Why I am addressing this

I am trying to fix all of the current outstanding test breakages blocking the merge of our new Bun CI (#8904). To verify this PR, we are running ALL unit tests in the monorepo. This has revealed a handful of existing breakages, including this one.


The problem

In @hapi/hapi (and modern @types/node / HTTP header type definitions), req.headers elements are typed as unknown (e.g. Record<string, unknown>). Passing header values like req.headers['user-agent'], req.headers.referrer, or extractRemoteAddressFromRequest(req) directly into getSingleHeader(val: string | string[] | undefined) caused TS2345: Argument of type 'unknown' is not assignable to parameter of type 'string | string[] | undefined'.


The fix

Update the internal implementation to correctly reflect the real types and add a new test to verify functionality.

I don't love the use of as casting in the tests, but mocking a type-compliant hapi.Request object is surprisingly difficult (it appears to require a running server to inject dependencies). I spoke with Gemini about this and it recommended casting a bare JS object (like the existing tests). An alternative would be to update the implementation to have a less strict type, but I would prefer to keep the minimum diff.

@pearigee
pearigee requested a review from a team as a code owner July 24, 2026 20:54
@github-actions
github-actions Bot requested a review from feywind July 24, 2026 20:54

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Hapi request extractor to support both hapi.Request and plain object types, adding robust type and null checks for headers, URLs, and status codes. It also includes a new unit test to verify correct handling of array headers. The reviewer suggested replacing the check req.info?.toString() === '[object Object]' with a more robust object check to prevent false positives from single-element arrays of objects.

I am having trouble creating individual review comments. Click here to see my feedback.

handwritten/error-reporting/src/request-extractors/hapi.ts (57-67)

medium

Avoid using req.info?.toString() === '[object Object]' to verify if a variable is a plain object, because single-element arrays of objects (e.g., [{}]) will also evaluate to "[object Object]". Use typeof req.info === 'object' && req.info !== null && !Array.isArray(req.info) instead.

function extractRemoteAddressFromRequest(
  req: any,
) {
  if (
    req.headers &&
    typeof req.headers === 'object' &&
    'x-forwarded-for' in req.headers
  ) {
    return req.headers['x-forwarded-for'];
  }
  if (
    req.info &&
    typeof req.info === 'object' &&
    !Array.isArray(req.info)
  ) {
    return req.info.remoteAddress;
  }
References
  1. Do not use value?.toString() === '[object Object]' to verify if a variable is a plain object, because single-element arrays of objects (e.g., [{}]) will also evaluate to "[object Object]". Use typeof value === 'object' && value !== null && !Array.isArray(value) or a similar robust check instead.

@pearigee

Copy link
Copy Markdown
Contributor Author

Note, in response to the Gemini feedback, the lines it's reporting on are not modified by the PR. My preference here is to maintain the minimum possible diff to resolve the test failure.

@shivanee-p shivanee-p left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it appears to require a running server to inject dependencies <-- that's wild for a mock 👀

Ty for updating the unit tests to reflect this

@pearigee
pearigee merged commit 580e36e into main Jul 24, 2026
37 checks passed
@pearigee
pearigee deleted the pearigee-fix-error-reporting branch July 24, 2026 22:30
shivanee-p pushed a commit that referenced this pull request Jul 27, 2026
* chore: fix test breakage in error-reporting

* rollback to minimum set of changes required

* add test whitespace for readability
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.

2 participants