index.js
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var equal = require('assert').equal;
var deepEqual = require('assert').deepEqual;
var createError = require('../create-error');
describe('create-error', function() {
describe('error creation', function() {
it('should create a new error', function() {
var TestingError = createError('TestingError');
var a = new TestingError('msgA');
var b = new TestingError('msgB');
equal((a instanceof TestingError), true);
equal((a instanceof Error), true);
equal(a.message, 'msgA');
equal(b.message, 'msgB');
equal((a.stack.length > 0), true);
});
it('should attach properties in the second argument', function() {
var TestingError = createError('TestingError', {anArray: []});
var a = new TestingError('Test the array');
deepEqual(a.anArray, []);
});
it('should give the name "CustomError" if the name is omitted', function() {
var TestingError = createError();
var a = new TestingError("msg");
equal(a.name, 'CustomError');
});
it('should not reference the same property in subsequent errors', function() {
var TestingError = createError('TestingError', {anArray: []});
var a = new TestingError('Test the array');
a.anArray.push('a');
var b = new TestingError('');
deepEqual(b.anArray, []);
});
it('should allow for empty objects on the cloned hash', function() {
var TestingError = createError('TestingError', {anEmptyObj: Object.create(null)});
var a = new TestingError('Test the array');
deepEqual(a.anEmptyObj, Object.create(null));
});
it('attaches attrs in the second arg of the error ctor, #3', function() {
var RequestError = createError('RequestError', {status: 400});
var reqErr = new RequestError('404 Error', {status: 404});
equal(reqErr.status, 404);
equal(reqErr.message, '404 Error');
equal(reqErr.name, 'RequestError');
});
});
describe('subclassing errors', function() {
it('takes an object in the first argument', function() {
var TestingError = createError('TestingError');
var SubTestingError = createError(TestingError, 'SubTestingError');
var x = new SubTestingError();
equal((x instanceof SubTestingError), true);
equal((x instanceof TestingError), true);
equal((x instanceof Error), true);
});
it('attaches the properties appropriately.', function() {
var TestingError = createError('TestingError');
var SubTestingError = createError(TestingError, 'SubTestingError', {key: []});
var x = new SubTestingError();
deepEqual(x.key, []);
});
it('allows for a default message, #4', function() {
var TestingError = createError('TestingError', {message: 'Error with testing'});
var x = new TestingError();
equal(x.message, 'Error with testing');
});
});
describe('invalid values sent to the second argument', function() {
it('should ignore falsy values', function() {
var TestingError = createError('TestingError', '');
var TestingError2 = createError('TestingError', null);
var TestingError3 = createError('TestingError', void 0);
var a = new TestingError('Test the array');
var b = new TestingError2('Test the array');
var c = new TestingError3('Test the array');
});
it('should ignore arrays', function() {
var TestingError = createError('TestingError', [{anArray: []}]);
var a = new TestingError('Test the array');
equal(a.anArray, void 0);
});
});
});