Skip to content

Commit

Permalink
Add test for extending native errors w/o altering prototype (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchurch authored Feb 5, 2025
1 parent a18ca45 commit a56fde9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ describe('createError(status)', function () {
assert.ok(util.isError(createError(500))) // eslint-disable-line node/no-deprecated-api
})

describe('Extending Existing Errors with HTTP Properties', function () {
it('should extend existing error without altering its prototype or replacing the object', function () {
var nativeError = new Error('This is a test error')

// Extend the error with HTTP semantics
var httpError = createError(404, nativeError, { expose: false })

assert.strictEqual(httpError.status, 404)
assert.strictEqual(httpError.expose, false)

assert.strictEqual(httpError.message, 'This is a test error')

assert(httpError instanceof Error)

assert.strictEqual(Object.getPrototypeOf(httpError), Error.prototype)

assert.strictEqual(httpError, nativeError)
})
})

describe('when status 300', function () {
before(function () {
this.error = createError(300)
Expand Down

0 comments on commit a56fde9

Please sign in to comment.