Skip to content

Commit d9a716b

Browse files
committed
add inheritance test suite
1 parent ee8fc92 commit d9a716b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/inheritance.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const createError = require('../index')
2+
var assert = require('assert')
3+
4+
function getProtoChain (object) {
5+
const chain = []
6+
let proto = Object.getPrototypeOf(object)
7+
while (proto) {
8+
chain.push(proto.constructor.name)
9+
proto = Object.getPrototypeOf(proto)
10+
}
11+
return chain
12+
}
13+
14+
function getProtoString (object) {
15+
return getProtoChain(object).join('.')
16+
}
17+
18+
describe('Inheritance', () => {
19+
it('Subclasses are instances of themselves', () => {
20+
class CustomError extends createError.NotFound {}
21+
const err = new CustomError()
22+
23+
assert.ok(err instanceof CustomError, `Subclass not instanceof itself: looking for CustomError ${getProtoString(err)}`)
24+
})
25+
it('Subclasses are instances of their ancestors', () => {
26+
class CustomError extends createError.NotFound {}
27+
class MoreCustom extends CustomError {}
28+
const err = new MoreCustom()
29+
30+
assert.ok(err instanceof Error, `Subclass not instanceof its ancestor: looking for Error ${getProtoString(err)}`)
31+
assert.ok(err instanceof createError.HttpError, `Subclass not instanceof its grandparent: looking for HttpError ${getProtoString(err)}`)
32+
assert.ok(err instanceof createError.NotFound, `Subclass not instanceof its grandparent: looking for NotFound ${getProtoString(err)}`)
33+
assert.ok(err instanceof CustomError, `Subclass not instanceof its parent: looking for CustomError ${getProtoString(err)}`)
34+
})
35+
})
36+
37+
describe('Inheritance without new keyword', () => {
38+
it('createError returns errors that are instances of the associated status\' constructor', () => {
39+
const err = createError(404, 'Custom message', { customProperty: 'custom value' })
40+
41+
assert.ok(err.customProperty === 'custom value', `Custom property not set: looking "for custom value", found ${err.customProperty}`)
42+
assert.ok(err instanceof createError.NotFound, `Instance not instanceof itself: looking for NotFound ${getProtoString(err)}`)
43+
44+
const serverError = createError({ customProperty: 'custom value' })
45+
46+
assert.ok(serverError.customProperty === 'custom value', `Custom property not set: looking "for custom value", found ${err.customProperty}`)
47+
assert.ok(serverError instanceof createError.InternalServerError, `Instance not instanceof itself: looking for InternalServerError ${getProtoString(err)}`)
48+
})
49+
50+
it('Dynamic constructor factory functions return instances of themselves', () => {
51+
const err = createError.NotFound('Not Found')
52+
53+
assert.ok(err instanceof createError.NotFound, `Instance not instanceof itself: looking for NotFound ${getProtoString(err)}`)
54+
})
55+
56+
it('Dynamic constructor factory functions return instances of their ancestors', () => {
57+
const err = createError.NotFound('Not Found')
58+
59+
assert.ok(err instanceof Error, `Subclass not instanceof its ancestor: looking for Error ${getProtoString(err)}`)
60+
assert.ok(err instanceof createError.HttpError, `Subclass not instanceof its grandparent: looking for HttpError ${getProtoString(err)}`)
61+
})
62+
})
63+

0 commit comments

Comments
 (0)