forked from sindresorhus/serialize-error
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
385 lines (329 loc) · 9.85 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import {Buffer} from 'node:buffer';
import Stream from 'node:stream';
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 discard streams', t => {
t.deepEqual(serializeError({s: new Stream.Stream()}), {s: '[object Stream]'}, 'Stream.Stream');
t.deepEqual(serializeError({s: new Stream.Readable()}), {s: '[object Stream]'}, 'Stream.Readable');
t.deepEqual(serializeError({s: new Stream.Writable()}), {s: '[object Stream]'}, 'Stream.Writable');
t.deepEqual(serializeError({s: new Stream.Duplex()}), {s: '[object Stream]'}, 'Stream.Duplex');
t.deepEqual(serializeError({s: new Stream.Transform()}), {s: '[object Stream]'}, 'Stream.Transform');
t.deepEqual(serializeError({s: new Stream.PassThrough()}), {s: '[object Stream]'}, 'Stream.PassThrough');
});
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 serialize the cause property', t => {
const error = new Error('outer error');
error.cause = new Error('inner error');
const serialized = serializeError(error);
t.is(serialized.message, 'outer error');
t.is(serialized.cause.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('should deserialize the cause property', t => {
const object = {
message: 'error message',
stack: 'at <anonymous>:1:13',
name: 'name',
code: 'code',
cause: {
message: 'source error message',
stack: 'at <anonymous>:3:14',
name: 'name',
code: 'code',
},
};
const {cause} = deserializeError(object);
t.is(cause.message, 'source error message');
t.is(cause.stack, 'at <anonymous>:3:14');
t.is(cause.name, 'name');
t.is(cause.code, 'code');
});
test('deserialized name, stack, cause an message should not be enumerable, other props should be', t => {
const object = {
message: 'error message',
stack: 'at <anonymous>:1:13',
name: 'name',
cause: {
message: 'cause error message',
stack: 'at <anonymous>:4:20',
name: 'name',
},
};
const enumerables = {
code: 'code',
path: './path',
errno: 1,
syscall: 'syscall',
randomProperty: 'random',
};
const deserialized = deserializeError({...object, ...enumerables});
t.deepEqual(
Object.keys(enumerables),
Object.keys(deserialized),
);
});
test('should deserialize properties up to `Options.maxDepth` levels deep', t => {
const error = new Error('errorMessage');
const object = {
message: error.message,
name: error.name,
stack: error.stack,
one: {
two: {
three: {},
},
},
};
const levelZero = deserializeError(object, {maxDepth: 0});
const emptyError = new Error('test');
emptyError.message = '';
t.is(levelZero instanceof Error, true);
t.deepEqual(levelZero, emptyError);
const levelOne = deserializeError(object, {maxDepth: 1});
error.one = {};
t.is(levelOne instanceof Error, true);
t.deepEqual(levelOne, error);
const levelTwo = deserializeError(object, {maxDepth: 2});
error.one = {two: {}};
t.is(levelTwo instanceof Error, true);
t.deepEqual(levelTwo, error);
const levelThree = deserializeError(object, {maxDepth: 3});
error.one = {two: {three: {}}};
t.is(levelThree instanceof Error, true);
t.deepEqual(levelThree, error);
});
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);
});
test('should serialize properties up to `Options.maxDepth` levels deep', t => {
const error = new Error('errorMessage');
error.one = {two: {three: {}}};
const {message, name, stack} = error;
const levelZero = serializeError(error, {maxDepth: 0});
t.deepEqual(levelZero, {});
const levelOne = serializeError(error, {maxDepth: 1});
t.deepEqual(levelOne, {message, name, stack, one: {}});
const levelTwo = serializeError(error, {maxDepth: 2});
t.deepEqual(levelTwo, {message, name, stack, one: {two: {}}});
const levelThree = serializeError(error, {maxDepth: 3});
t.deepEqual(levelThree, {message, name, stack, one: {two: {three: {}}}});
});