This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
137 lines (116 loc) · 2.91 KB
/
test.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import tinyError from './module/index';
const test = require('tape-catch');
const assign = require('object-assign');
test('The API is in good shape', (is) => {
is.equal(
typeof tinyError,
'function',
'Exports a function'
);
is.equal(
typeof tinyError('message'),
'object',
'…returning an error object when given a string,'
);
is.equal(
typeof tinyError({message: 'message'}),
'object',
'…or an args object with the key `message`.'
);
const curriedError = tinyError({any: 'other', keys: 'and values'});
is.equal(
typeof curriedError,
'function',
'When given a different args object it returns a curried function,'
);
is.equal(
typeof curriedError({message: 'and', other: 'stuff'}),
'object',
'…which again returns an error object,'
);
is.equal(
typeof curriedError({}),
'function',
'…or curries the function further.'
);
is.deepEqual(
tinyError({three: 3})({two: 2})({one: 1})('go!'),
tinyError({three: 3, two: 2, one: 1, message: 'go!'}),
'Returns the same results whether curried or not'
);
is.end();
});
test('Creates a lightweight error object', (is) => {
is.equal(
tinyError('message').__proto__,
Error.prototype,
'Returns an object based on `Error.prototype`,'
);
const customProto = {};
is.equal(
tinyError({
prototype: customProto,
message: 'message',
}).__proto__,
customProto,
'…except when a custom `prototype` is given.'
);
is.notOk(
tinyError({prototype: customProto, message: ''})
.hasOwnProperty('prototype')
,
'Doesn’t copy the property `prototype`.'
);
is.equal(
tinyError({message: 'My message'}).message,
'My message',
'Copies over the property `message`,'
);
is.equal(
tinyError({
prefix: '[my label] ',
message: 'My message',
}).message,
'[my label] My message',
'…extending it when `prefix`'
);
is.equal(
tinyError({
prefix: '[my label] ',
message: 'My message',
suffix: '. Good one, ain’ it?',
}).message,
'[my label] My message. Good one, ain’ it?',
'…or `suffix` is given.'
);
is.deepEqual(
tinyError({
prefix: '[prefix] ',
message: 'My message',
customKey: 'anything',
name: 'CustomError',
type: /anything/,
}),
assign(Object.create(Error.prototype), {
message: '[prefix] My message',
customKey: 'anything',
name: 'CustomError',
type: /anything/,
}),
'Copies over all other args.'
);
is.end();
});
test('Fails gracefully', (is) => {
is.throws(
() => tinyError(),
/expected `{Object} args` or `{String} message`/i,
'throws an error when called without arguments'
);
is.throws(
() => tinyError(true),
/expected `{Object} args` or `{String} message`/i,
'throws an error when called with an argument of a wrong type'
);
is.end();
});