From 42038988a87e31415ed8d04c90e62aca107b81ac Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Thu, 31 Oct 2024 11:54:02 +0100 Subject: [PATCH] Cleanup --- lib/index.js | 6 +++-- test/index.js | 70 ++++++++++++++++++--------------------------------- 2 files changed, 29 insertions(+), 47 deletions(-) diff --git a/lib/index.js b/lib/index.js index 83fa569..1355d4a 100755 --- a/lib/index.js +++ b/lib/index.js @@ -119,8 +119,10 @@ exports.Boom = class Boom extends Error { } static { - Object.defineProperty(this.prototype, 'name', { value: 'Boom', writable: true, configurable: true }); - Object.defineProperty(this.prototype, 'isBoom', { value: true, writable: true, configurable: true }); + Object.defineProperties(this.prototype, { + name: { value: 'Boom', writable: true, configurable: true }, + isBoom: { value: true, writable: true, configurable: true } + }); } }; diff --git a/test/index.js b/test/index.js index 809f311..832af91 100755 --- a/test/index.js +++ b/test/index.js @@ -425,6 +425,17 @@ describe('Boom', () => { }); }); + const utilities = ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed', + 'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict', + 'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge', + 'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed', + 'badData', 'preconditionRequired', 'tooManyRequests', + + // 500s + 'internal', 'notImplemented', 'badGateway', 'serverUnavailable', + 'gatewayTimeout', 'badImplementation' + ]; + describe('badRequest()', () => { it('returns a 400 error statusCode', () => { @@ -1055,25 +1066,22 @@ describe('Boom', () => { } }); }); - }); - describe('stack trace', () => { + it('uses data with Error as cause', () => { - const helpers = ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed', - 'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict', - 'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge', - 'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed', - 'badData', 'preconditionRequired', 'tooManyRequests', + const insideErr = new Error('inside'); + const err = Boom.badImplementation('my message', insideErr); + expect(err.data).to.not.exist(); + expect(err.cause).to.shallow.equal(insideErr); + }); + }); - // 500s - 'internal', 'notImplemented', 'badGateway', 'serverUnavailable', - 'gatewayTimeout', 'badImplementation' - ]; + describe('stack trace', () => { it('should omit lib', () => { - for (const helper of helpers) { - const err = Boom[helper](); + for (const name of utilities) { + const err = Boom[name](); expect(err.stack).to.not.match(/(\/|\\)lib(\/|\\)index\.js/); } }); @@ -1082,10 +1090,10 @@ describe('Boom', () => { const captureStackTrace = Error.captureStackTrace; - for (const helper of helpers) { + for (const name of utilities) { try { Error.captureStackTrace = undefined; - var err = Boom[helper](); + var err = Boom[name](); } finally { Error.captureStackTrace = captureStackTrace; @@ -1098,35 +1106,7 @@ describe('Boom', () => { describe('method with error object instead of message', () => { - [ - 'badRequest', - 'unauthorized', - 'forbidden', - 'notFound', - 'methodNotAllowed', - 'notAcceptable', - 'proxyAuthRequired', - 'clientTimeout', - 'conflict', - 'resourceGone', - 'lengthRequired', - 'preconditionFailed', - 'entityTooLarge', - 'uriTooLong', - 'unsupportedMediaType', - 'rangeNotSatisfiable', - 'expectationFailed', - 'badData', - 'preconditionRequired', - 'tooManyRequests', - 'internal', - 'notImplemented', - 'badGateway', - 'serverUnavailable', - 'gatewayTimeout', - 'badImplementation' - ].forEach((name) => { - + for (const name of utilities) { it(`uses stringified error as message`, () => { const error = new Error('An example mongoose validation error'); @@ -1135,7 +1115,7 @@ describe('Boom', () => { expect(err.cause).to.not.exist(); expect(err.message).to.equal(error.toString()); }); - }); + } }); describe('reformat()', () => {