From 43c03e884abc2fb8c179c5c72c4774fc95712a17 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 8 Mar 2024 22:04:23 -0500 Subject: [PATCH] add test for extending native errors w/o altering prototype --- test/test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test.js b/test/test.js index 0059d01..8fcfa8d 100644 --- a/test/test.js +++ b/test/test.js @@ -11,6 +11,24 @@ 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 an existing error with HTTP properties without altering its prototype', function () { + const nativeError = new Error('This is a test error') + + // Extend the error with HTTP semantics + const 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) + }) + }) + describe('when status 300', function () { before(function () { this.error = createError(300)