forked from mochajs/mocha
/
serializer.spec.js
543 lines (494 loc) · 17.2 KB
/
serializer.spec.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
'use strict';
const sinon = require('sinon');
const {
serialize,
deserialize,
SerializableEvent,
SerializableWorkerResult
} = require('../../lib/nodejs/serializer');
describe('serializer', function() {
afterEach(function() {
sinon.restore();
});
describe('function', function() {
describe('serialize', function() {
describe('when passed a non-object value', function() {
it('should return the value', function() {
expect(serialize('knees & toes'), 'to be', 'knees & toes');
});
});
describe('when passed an object value', function() {
describe('w/o a `serialize` method', function() {
it('should return the value', function() {
const obj = {};
expect(serialize(obj), 'to be', obj);
});
});
describe('having a `serialize` method', function() {
it('should return the result of the `serialize` method', function() {
const serializedObj = {foo: 'bar'};
const obj = {serialize: sinon.stub().returns(serializedObj)};
expect(serialize(obj), 'to be', serializedObj);
});
});
});
describe('when not passed anything', function() {
it('should return `undefined`', function() {
expect(serialize(), 'to be undefined');
});
});
});
describe('deserialize', function() {
describe('when passed nothing', function() {
it('should return `undefined`', function() {
expect(deserialize(), 'to be undefined');
});
});
describe('when passed a non-object value', function() {
it('should return the value', function() {
expect(deserialize(500), 'to be', 500);
});
});
describe('when passed an object value which is not a SerializedWorkerResult', function() {
it('should return the value', function() {
const obj = {};
expect(deserialize(obj), 'to be', obj);
});
});
describe('when passed a SerializedWorkerResult object', function() {
// note that SerializedWorkerResult is an interface (typedef), not a class.
it('should return the result of `SerializableWorkerResult.deserialize` called on the value', function() {
const obj = Object.assign({}, SerializableWorkerResult.create());
sinon.stub(SerializableWorkerResult, 'deserialize').returns('butts');
deserialize(obj);
expect(
SerializableWorkerResult.deserialize,
'to have a call satisfying',
{
args: [obj],
returned: 'butts'
}
);
});
});
});
});
describe('SerializableEvent', function() {
describe('constructor', function() {
describe('when called without `eventName`', function() {
it('should throw "invalid arg value" error', function() {
expect(() => new SerializableEvent(), 'to throw', {
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
});
});
});
describe('when called with a non-object `rawObject`', function() {
it('should throw "invalid arg type" error', function() {
expect(() => new SerializableEvent('blub', 'glug'), 'to throw', {
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
});
});
});
});
describe('instance method', function() {
describe('serialize', function() {
it('should mutate the instance in-place', function() {
const evt = SerializableEvent.create('foo');
expect(evt.serialize(), 'to be', evt);
});
it('should freeze the instance', function() {
expect(
Object.isFrozen(SerializableEvent.create('foo').serialize()),
'to be true'
);
});
describe('when passed an object with a `serialize` method', function() {
it('should call the `serialize` method', function() {
const obj = {
serialize: sinon.stub()
};
SerializableEvent.create('some-event', obj).serialize();
expect(obj.serialize, 'was called once');
});
});
describe('when passed an object containing an object with a `serialize` method', function() {
it('should call the `serialize` method', function() {
const stub = sinon.stub();
const obj = {
nested: {
serialize: stub
}
};
SerializableEvent.create('some-event', obj).serialize();
expect(stub, 'was called once');
});
});
describe('when passed an object containing a non-`serialize` method', function() {
it('should remove the method', function() {
const obj = {
func: () => {}
};
expect(
SerializableEvent.create('some-event', obj).serialize(),
'to satisfy',
{
data: expect.it('not to have property', 'func')
}
);
});
});
describe('when passed an object containing an array', function() {
it('should serialize the array', function() {
const obj = {
list: [{herp: 'derp'}, {bing: 'bong'}]
};
expect(
SerializableEvent.create('some-event', obj).serialize(),
'to satisfy',
{data: {list: [{herp: 'derp'}, {bing: 'bong'}]}}
);
});
});
describe('when passed an error', function() {
it('should serialize the error', function() {
const obj = {};
const err = new Error('monkeypants');
expect(
SerializableEvent.create('some-event', obj, err).serialize(),
'to satisfy',
{
eventName: 'some-event',
error: {
message: 'monkeypants',
stack: /^Error: monkeypants/,
__type: 'Error'
},
data: obj
}
);
});
it('should retain own props', function() {
const obj = {};
const err = new Error('monkeypants');
err.code = 'MONKEY';
expect(
SerializableEvent.create('some-event', obj, err).serialize(),
'to satisfy',
{
eventName: 'some-event',
error: {
code: 'MONKEY',
message: 'monkeypants',
stack: /^Error: monkeypants/,
__type: 'Error'
},
data: obj
}
);
});
it('should not retain not-own props', function() {
const obj = {};
const err = new Error('monkeypants');
// eslint-disable-next-line no-proto
err.__proto__.code = 'MONKEY';
expect(
SerializableEvent.create('some-event', obj, err).serialize(),
'to satisfy',
{
eventName: 'some-event',
error: {
message: 'monkeypants',
stack: /^Error: monkeypants/,
__type: 'Error'
},
data: obj
}
);
});
});
describe('when passed an object containing a top-level prop with an Error value', function() {
it('should serialize the Error', function() {
const obj = {
monkeyError: new Error('pantsmonkey')
};
const evt = SerializableEvent.create('some-event', obj);
expect(evt.serialize(), 'to satisfy', {
eventName: 'some-event',
data: {
monkeyError: {
message: 'pantsmonkey',
stack: /^Error: pantsmonkey/,
__type: 'Error'
}
}
});
});
});
describe('when passed an object containing a nested prop with an Error value', function() {
it('should serialize the Error', function() {
const obj = {
nestedObj: {
monkeyError: new Error('pantsmonkey')
}
};
const evt = SerializableEvent.create('some-event', obj);
expect(evt.serialize(), 'to satisfy', {
eventName: 'some-event',
data: {
nestedObj: {
monkeyError: {
message: 'pantsmonkey',
stack: /^Error: pantsmonkey/,
__type: 'Error'
}
}
}
});
});
});
});
});
describe('static method', function() {
describe('deserialize', function() {
describe('when passed a falsy parameter', function() {
it('should throw "invalid arg type" error', function() {
expect(SerializableEvent.deserialize, 'to throw', {
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
});
});
});
it('should return a new object w/ null prototype', function() {
const obj = {bob: 'bob'};
expect(SerializableEvent.deserialize(obj), 'to satisfy', obj)
.and('not to equal', obj)
.and('not to have property', 'constructor');
});
describe('when passed value contains `data` prop', function() {
it('should ignore __proto__', function() {
const obj = {
data: Object.create(null)
};
// eslint-disable-next-line no-proto
obj.data.__proto__ = {peaches: 'prunes'};
const expected = Object.assign(Object.create(null), {
data: Object.create(null)
});
expect(SerializableEvent.deserialize(obj), 'to equal', expected);
});
describe('when `data` prop contains a nested serialized Error prop', function() {
it('should create an Error instance from the nested serialized Error prop', function() {
const message = 'problems!';
const stack = 'problem instructions';
const code = 'EIEIO';
const expected = Object.assign(Object.create(null), {
data: {
whoops: Object.assign(new Error(message), {
stack,
code
})
}
});
expect(
SerializableEvent.deserialize({
data: {
whoops: {
message,
stack,
code,
__type: 'Error'
}
}
}),
'to equal',
expected
);
});
});
});
describe('when passed value contains an `error` prop', function() {
it('should create an Error instance from the prop', function() {
const message = 'problems!';
const stack = 'problem instructions';
const code = 'EIEIO';
const expected = Object.assign(Object.create(null), {
error: Object.assign(new Error(message), {
stack,
code
})
});
expect(
SerializableEvent.deserialize({
error: {
message,
stack,
code,
__type: 'Error'
}
}),
'to equal',
expected
);
});
});
describe('when passed value data contains a prop beginning with "$$"', function() {
let result;
beforeEach(function() {
result = SerializableEvent.deserialize({data: {$$foo: 'bar'}});
});
it('should create a new prop having a function value', function() {
expect(result, 'to satisfy', {
data: {
foo: expect.it('to be a function')
}
});
});
it('should create a new prop returning the original value', function() {
expect(result.data.foo(), 'to equal', 'bar');
});
it('should remove the prop with the "$$" prefix', function() {
expect(result, 'not to have property', '$$foo');
});
});
describe('when the value data contains a prop with an array value', function() {
beforeEach(function() {
sinon.spy(SerializableEvent, '_deserializeObject');
});
it('should deserialize each prop', function() {
const obj = {data: {foo: [{bar: 'baz'}]}};
SerializableEvent.deserialize(obj);
expect(
SerializableEvent._deserializeObject,
'to have a call satisfying',
{
args: [obj.data.foo, 0]
}
);
});
});
});
describe('create', function() {
it('should instantiate a SerializableEvent', function() {
expect(
SerializableEvent.create('some-event'),
'to be a',
SerializableEvent
);
});
});
});
});
describe('SerializableWorkerResult', function() {
describe('static method', function() {
describe('create', function() {
it('should return a new SerializableWorkerResult instance', function() {
expect(
SerializableWorkerResult.create(),
'to be a',
SerializableWorkerResult
);
});
});
describe('isSerializedWorkerResult', function() {
describe('when passed an instance', function() {
it('should return `true`', function() {
expect(
SerializableWorkerResult.isSerializedWorkerResult(
new SerializableWorkerResult()
),
'to be true'
);
});
});
describe('when passed an object with an appropriate `__type` prop', function() {
it('should return `true`', function() {
// this is the most likely use-case, as the object is transmitted over IPC
// and loses its prototype
const original = new SerializableWorkerResult();
const clone = Object.assign({}, original);
expect(
SerializableWorkerResult.isSerializedWorkerResult(clone),
'to be true'
);
});
});
describe('when passed an object without an appropriate `__type` prop', function() {
it('should return `false`', function() {
expect(
SerializableWorkerResult.isSerializedWorkerResult({
mister: 'mister'
}),
'to be false'
);
});
});
});
describe('deserialize', function() {
beforeEach(function() {
sinon.stub(SerializableEvent, 'deserialize');
});
it('should call SerializableEvent#deserialize on each item in its `events` prop', function() {
const result = Object.assign(
{},
SerializableWorkerResult.create([
{eventName: 'foo'},
{eventName: 'bar'}
])
);
SerializableWorkerResult.deserialize(result);
expect(SerializableEvent.deserialize, 'to have calls satisfying', [
{args: [{eventName: 'foo'}]},
{args: [{eventName: 'bar'}]}
]);
});
it('should return the deserialized value', function() {
const result = Object.assign(
{},
SerializableWorkerResult.create([
{eventName: 'foo'},
{eventName: 'bar'}
])
);
expect(
SerializableWorkerResult.deserialize(result),
'to equal',
result
);
});
});
});
describe('instance method', function() {
describe('serialize', function() {
it('should return a read-only value', function() {
expect(
Object.isFrozen(SerializableWorkerResult.create().serialize()),
'to be true'
);
});
it('should call `SerializableEvent#serialize` of each of its events', function() {
sinon.spy(SerializableEvent.prototype, 'serialize');
const events = [
SerializableEvent.create('foo'),
SerializableEvent.create('bar')
];
SerializableWorkerResult.create(events).serialize();
expect(
SerializableEvent.prototype.serialize,
'to have calls satisfying',
[{thisValue: events[0]}, {thisValue: events[1]}]
);
});
});
});
describe('constructor', function() {
// the following two tests should be combined into one, but not sure how to express
// as a single assertion
it('should add a readonly `__type` prop', function() {
expect(
new SerializableWorkerResult(),
'to have readonly property',
'__type'
);
});
});
});
});