Skip to content

test(utils): cover async handler rejected promises after headers are sent and error normalization - #2218

Open
maxx-GENIUS wants to merge 1 commit into
LabsCrypt:mainfrom
maxx-GENIUS:test/async-handler-rejected-promises-2090
Open

maxx-GENIUS wants to merge 1 commit into
LabsCrypt:mainfrom
maxx-GENIUS:test/async-handler-rejected-promises-2090

Conversation

@maxx-GENIUS

Copy link
Copy Markdown

Summary

This PR adds comprehensive unit test coverage for backend/src/utils/asyncHandler.ts ensuring that unhandled rejections occurring after response headers have been sent (res.headersSent = true) are properly caught and forwarded to next(), while asserting complete normalization contracts across non-Error rejections.

Closes #2090

Key Test Coverage Added

  1. Rejected Promises After Headers Are Sent:
    • Catches and forwards rejected promise when headers are committed by res.writeHead().
    • Catches and forwards deferred background rejections after res.send().
    • Normalizes non-Error rejections thrown after res.headersSent is set to true.
    • Handles promises rejecting when res.headersSent is already true upon entry.
  2. Error Forwarding & Property Preservation:
    • Forwards genuine Error instances as-is to next(err).
    • Preserves custom properties on errors (such as statusCode, errorCode).
    • Preserves error subclasses (TypeError, RangeError, etc.) without mutation.
  3. Non-Error Rejection Normalization:
    • Asserts string rejections normalize to Error('Non-Error rejection: <val>') with cause attached.
    • Asserts number, boolean, null, undefined, plain object, and symbol rejections normalize predictably with root value preserved in err.cause.
  4. Execution Flow Contracts:
    • Catches synchronously thrown errors inside async handlers.
    • Ensures successful async functions do not call next(err).
    • Supports explicit next() invocations in middleware chains.
    • Verifies faithful forwarding of req, res, and next references.

Verification

  • npm test -- src/utils/__tests__/asyncHandler.test.ts (18/18 tests passing, 100% green)
  • npm run build (0 TypeScript compilation errors)

@github-actions github-actions Bot added the security:malware-suspected Automated malware indicator found in a pull request label Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

Malware detection scan flagged this pull request

The scan found 1 indicator(s) in the PR diff. This PR is blocked until a maintainer reviews the flagged files.

  • backend/src/utils/__tests__/asyncHandler.test.ts — hidden-appended-config-payload (high)

This is an automated triage signal; maintainers must verify whether the finding is malicious or intentional.

This branch has not been deployed

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

Labels

security:malware-suspected Automated malware indicator found in a pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[test] Cover async handler rejected promises

1 participant