Skip to content

Commit a56fde9

Browse files
authored
Add test for extending native errors w/o altering prototype (#106)
1 parent a18ca45 commit a56fde9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ describe('createError(status)', function () {
1111
assert.ok(util.isError(createError(500))) // eslint-disable-line node/no-deprecated-api
1212
})
1313

14+
describe('Extending Existing Errors with HTTP Properties', function () {
15+
it('should extend existing error without altering its prototype or replacing the object', function () {
16+
var nativeError = new Error('This is a test error')
17+
18+
// Extend the error with HTTP semantics
19+
var httpError = createError(404, nativeError, { expose: false })
20+
21+
assert.strictEqual(httpError.status, 404)
22+
assert.strictEqual(httpError.expose, false)
23+
24+
assert.strictEqual(httpError.message, 'This is a test error')
25+
26+
assert(httpError instanceof Error)
27+
28+
assert.strictEqual(Object.getPrototypeOf(httpError), Error.prototype)
29+
30+
assert.strictEqual(httpError, nativeError)
31+
})
32+
})
33+
1434
describe('when status 300', function () {
1535
before(function () {
1636
this.error = createError(300)

0 commit comments

Comments
 (0)