Skip to content

Commit 565e1a0

Browse files
committed
add tests for subclasses and inherittance of custom errors
1 parent accaf39 commit 565e1a0

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

test/test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,35 @@ describe('createError.isHttpError(val)', function () {
178178
})
179179
})
180180

181+
describe('Subclass Instantiation', function () {
182+
it('should allow instantiation of ClientError subclasses', function () {
183+
assert.doesNotThrow(() => {
184+
// eslint-disable-next-line no-new
185+
new createError.NotFound()
186+
})
187+
})
188+
189+
it('should allow instantiation of ServerError subclasses', function () {
190+
assert.doesNotThrow(() => {
191+
// eslint-disable-next-line no-new
192+
new createError.InternalServerError()
193+
})
194+
})
195+
})
196+
197+
describe('Prototype Chain Adjustments', function () {
198+
it('CustomError instances should have the correct prototype chain', function () {
199+
class CustomError extends createError.NotFound {}
200+
const err = new CustomError()
201+
assert(err instanceof CustomError)
202+
assert(err instanceof createError.NotFound)
203+
// we don't export ClientError currently
204+
// assert(err instanceof createError.ClientError)
205+
assert(err instanceof createError.HttpError)
206+
assert(err instanceof Error)
207+
})
208+
})
209+
181210
describe('HTTP Errors', function () {
182211
it('createError(status, props)', function () {
183212
var err = createError(404, {
@@ -336,7 +365,7 @@ describe('HTTP Errors', function () {
336365
assert.strictEqual(err.expose, false)
337366
})
338367

339-
it('new createError.HttpError()', function () {
368+
it('should throw when directly instantiating HttpError', function () {
340369
assert.throws(function () {
341370
new createError.HttpError() // eslint-disable-line no-new
342371
}, /cannot construct abstract class/)

0 commit comments

Comments
 (0)