Skip to content

test: add edge case coverage for createError factory#154

Open
mahmoodhamdi wants to merge 1 commit intojshttp:masterfrom
mahmoodhamdi:test/improve-edge-case-coverage
Open

test: add edge case coverage for createError factory#154
mahmoodhamdi wants to merge 1 commit intojshttp:masterfrom
mahmoodhamdi:test/improve-edge-case-coverage

Conversation

@mahmoodhamdi
Copy link
Copy Markdown

What

Added 16 new test cases covering several untested edge cases in the createError factory and named constructors.

Why

I use http-errors extensively in my Express middleware and noticed some scenarios that lack test coverage while reading through the source. These tests document and verify existing behavior for edge cases that users commonly encounter.

New test cases

createError() with no arguments

  • Defaults to status 500, message "Internal Server Error", expose false

err.statusCode extraction

  • When an error has statusCode but not status, statusCode is used
  • When both status and statusCode exist, status takes precedence

expose property

  • Verified true for 4xx errors via both factory and named constructors
  • Verified false for 5xx errors via both factory and named constructors
  • Tests multiple status codes in each category (400, 404, 499, 500, 503, 599)

Custom headers via properties

  • Headers from the properties object are correctly attached to the error
  • Headers are undefined by default

Argument type validation

  • TypeError thrown for boolean arguments
  • TypeError thrown for function arguments

Named constructors with custom messages

  • Client error (BadRequest) with custom message
  • Server error (ServiceUnavailable) with custom message
  • null message falls back to default status message

Testing

68 passing (7ms)
1 pending

All existing tests continue to pass. Linting passes.

Add tests for several untested scenarios:

- createError() with no arguments defaults to 500
- err.statusCode extraction when err.status is not set
- err.status takes precedence over err.statusCode
- expose property is true for 4xx, false for 5xx (factory and constructors)
- custom headers attachment via properties object
- TypeError thrown for unsupported argument types (boolean, function)
- named constructors with custom messages and null message
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