/
test.js
202 lines (168 loc) · 5.06 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import test from 'ava';
import {serializeError, deserializeError} from '.';
function deserializeNonError(t, value) {
const deserialized = deserializeError(value);
t.true(deserialized instanceof Error);
t.is(deserialized.constructor.name, 'NonError');
t.is(deserialized.message, JSON.stringify(value));
}
test('main', t => {
const serialized = serializeError(new Error('foo'));
t.true('name' in serialized);
t.true('stack' in serialized);
t.true('message' in serialized);
});
test('should destroy circular references', t => {
const object = {};
object.child = {parent: object};
const serialized = serializeError(object);
t.is(typeof serialized, 'object');
t.is(serialized.child.parent, '[Circular]');
});
test('should not affect the original object', t => {
const object = {};
object.child = {parent: object};
const serialized = serializeError(object);
t.not(serialized, object);
t.is(object.child.parent, object);
});
test('should only destroy parent references', t => {
const object = {};
const common = {thing: object};
object.one = {firstThing: common};
object.two = {secondThing: common};
const serialized = serializeError(object);
t.is(typeof serialized.one.firstThing, 'object');
t.is(typeof serialized.two.secondThing, 'object');
t.is(serialized.one.firstThing.thing, '[Circular]');
t.is(serialized.two.secondThing.thing, '[Circular]');
});
test('should work on arrays', t => {
const object = {};
const common = [object];
const x = [common];
const y = [['test'], common];
y[0][1] = y;
object.a = {x};
object.b = {y};
const serialized = serializeError(object);
t.true(Array.isArray(serialized.a.x));
t.is(serialized.a.x[0][0], '[Circular]');
t.is(serialized.b.y[0][0], 'test');
t.is(serialized.b.y[1][0], '[Circular]');
t.is(serialized.b.y[0][1], '[Circular]');
});
test('should discard nested functions', t => {
function a() {}
function b() {}
a.b = b;
const object = {a};
const serialized = serializeError(object);
t.deepEqual(serialized, {});
});
test('should replace top-level functions with a helpful string', t => {
function a() {}
function b() {}
a.b = b;
const serialized = serializeError(a);
t.is(serialized, '[Function: a]');
});
test('should drop functions', t => {
function a() {}
a.foo = 'bar;';
a.b = a;
const object = {a};
const serialized = serializeError(object);
t.deepEqual(serialized, {});
t.false(Object.prototype.hasOwnProperty.call(serialized, 'a'));
});
test('should not access deep non-enumerable properties', t => {
const error = new Error('some error');
const object = {};
Object.defineProperty(object, 'someProp', {
enumerable: false,
get() {
throw new Error('some other error');
}
});
error.object = object;
t.notThrows(() => serializeError(error));
});
test('should serialize nested errors', t => {
const error = new Error('outer error');
error.innerError = new Error('inner error');
const serialized = serializeError(error);
t.is(serialized.message, 'outer error');
t.is(serialized.innerError.message, 'inner error');
});
test('should handle top-level null values', t => {
const serialized = serializeError(null);
t.is(serialized, null);
});
test('should deserialize null', t => {
deserializeNonError(t, null);
});
test('should deserialize number', t => {
deserializeNonError(t, 1);
});
test('should deserialize boolean', t => {
deserializeNonError(t, true);
});
test('should deserialize string', t => {
deserializeNonError(t, '123');
});
test('should deserialize array', t => {
deserializeNonError(t, [1]);
});
test('should deserialize error', t => {
const deserialized = deserializeError(new Error('test'));
t.true(deserialized instanceof Error);
t.is(deserialized.message, 'test');
});
test('should deserialize and preserve existing properties', t => {
const deserialized = deserializeError({
message: 'foo',
customProperty: true
});
t.true(deserialized instanceof Error);
t.is(deserialized.message, 'foo');
t.true(deserialized.customProperty);
});
test('should deserialize plain object', t => {
const object = {
message: 'error message',
stack: 'at <anonymous>:1:13',
name: 'name',
code: 'code'
};
const deserialized = deserializeError(object);
t.is(deserialized instanceof Error, true);
t.is(deserialized.message, 'error message');
t.is(deserialized.stack, 'at <anonymous>:1:13');
t.is(deserialized.name, 'name');
t.is(deserialized.code, 'code');
});
test('name, stack and message should not be enumerable, other props should be', t => {
const object = {
message: 'error message',
stack: 'at <anonymous>:1:13',
name: 'name'
};
const nonEnumerableProps = Object.keys(object);
const enumerables = {
code: 'code',
path: './path',
errno: 1,
syscall: 'syscall',
randomProperty: 'random'
};
const enumerableProps = Object.keys(enumerables);
const deserialized = deserializeError({...object, ...enumerables});
const deserializedEnumerableProps = Object.keys(deserialized);
for (const prop of nonEnumerableProps) {
t.false(deserializedEnumerableProps.includes(prop));
}
for (const prop of enumerableProps) {
t.true(deserializedEnumerableProps.includes(prop));
}
});