forked from sindresorhus/serialize-error
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
292 lines (250 loc) · 7.02 KB
/
Copy pathtest.js
File metadata and controls
292 lines (250 loc) · 7.02 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import test from 'ava';
import {serializeError, deserializeError} from './index.js';
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'));
const properties = Object.keys(serialized);
t.true(properties.includes('name'));
t.true(properties.includes('stack'));
t.true(properties.includes('message'));
});
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 discard buffers', t => {
const object = {a: Buffer.alloc(1)};
const serialized = serializeError(object);
t.deepEqual(serialized, {a: '[object Buffer]'});
});
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('deserialized 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));
}
});
test('should serialize Date as ISO string', t => {
const date = {date: new Date(0)};
const serialized = serializeError(date);
t.deepEqual(serialized, {date: '1970-01-01T00:00:00.000Z'});
});
test('should serialize custom error with `.toJSON`', t => {
class CustomError extends Error {
constructor() {
super('foo');
this.name = this.constructor.name;
this.value = 10;
}
toJSON() {
return {
message: this.message,
amount: `$${this.value}`
};
}
}
const error = new CustomError();
const serialized = serializeError(error);
t.deepEqual(serialized, {
message: 'foo',
amount: '$10'
});
t.true(serialized.stack === undefined);
});
test('should serialize custom error with a property having `.toJSON`', t => {
class CustomError extends Error {
constructor(value) {
super('foo');
this.name = this.constructor.name;
this.value = value;
}
}
const value = {
amount: 20,
toJSON() {
return {
amount: `$${this.amount}`
};
}
};
const error = new CustomError(value);
const serialized = serializeError(error);
const {stack, ...rest} = serialized;
t.deepEqual(rest, {
message: 'foo',
name: 'CustomError',
value: {
amount: '$20'
}
});
t.not(stack, undefined);
});
test('should serialize custom error with `.toJSON` defined with `serializeError`', t => {
class CustomError extends Error {
constructor() {
super('foo');
this.name = this.constructor.name;
this.value = 30;
}
toJSON() {
return serializeError(this);
}
}
const error = new CustomError();
const serialized = serializeError(error);
const {stack, ...rest} = serialized;
t.deepEqual(rest, {
message: 'foo',
name: 'CustomError',
value: 30
});
t.not(stack, undefined);
});