forked from jestjs/jest
/
prettyFormat.test.ts
918 lines (818 loc) · 24.5 KB
/
prettyFormat.test.ts
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable local/prefer-rest-params-eventually */
import prettyFormat from '../';
function returnArguments(..._args: Array<unknown>) {
return arguments;
}
class MyArray<T> extends Array<T> {}
function MyObject(value: unknown) {
// @ts-expect-error
this.name = value;
}
describe('prettyFormat()', () => {
it('prints empty arguments', () => {
const val = returnArguments();
expect(prettyFormat(val)).toEqual('Arguments []');
});
it('prints arguments', () => {
const val = returnArguments(1, 2, 3);
expect(prettyFormat(val)).toEqual('Arguments [\n 1,\n 2,\n 3,\n]');
});
it('prints an empty array', () => {
const val: Array<never> = [];
expect(prettyFormat(val)).toEqual('Array []');
});
it('prints an array with items', () => {
const val = [1, 2, 3];
expect(prettyFormat(val)).toEqual('Array [\n 1,\n 2,\n 3,\n]');
});
it('prints a sparse array with only holes', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [, , ,];
expect(prettyFormat(val)).toEqual('Array [\n ,\n ,\n ,\n]');
});
it('prints a sparse array with items', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [1, , , 4];
expect(prettyFormat(val)).toEqual('Array [\n 1,\n ,\n ,\n 4,\n]');
});
it('prints a sparse array with value surrounded by holes', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [, 5, ,];
expect(prettyFormat(val)).toEqual('Array [\n ,\n 5,\n ,\n]');
});
it('prints a sparse array also containing undefined values', () => {
// eslint-disable-next-line no-sparse-arrays
const val = [1, , undefined, undefined, , 4];
expect(prettyFormat(val)).toEqual(
'Array [\n 1,\n ,\n undefined,\n undefined,\n ,\n 4,\n]',
);
});
it('prints a empty typed array', () => {
const val = new Uint32Array(0);
expect(prettyFormat(val)).toEqual('Uint32Array []');
});
it('prints a typed array with items', () => {
const val = new Uint32Array(3);
expect(prettyFormat(val)).toEqual('Uint32Array [\n 0,\n 0,\n 0,\n]');
});
it('prints an array buffer', () => {
const val = new ArrayBuffer(3);
expect(prettyFormat(val)).toEqual('ArrayBuffer []');
});
it('prints a nested array', () => {
const val = [[1, 2, 3]];
expect(prettyFormat(val)).toEqual(
'Array [\n Array [\n 1,\n 2,\n 3,\n ],\n]',
);
});
it('prints true', () => {
const val = true;
expect(prettyFormat(val)).toEqual('true');
});
it('prints false', () => {
const val = false;
expect(prettyFormat(val)).toEqual('false');
});
it('prints an error', () => {
const val = new Error();
expect(prettyFormat(val)).toEqual('[Error]');
});
it('prints a typed error with a message', () => {
const val = new TypeError('message');
expect(prettyFormat(val)).toEqual('[TypeError: message]');
});
it('prints a function constructor', () => {
/* eslint-disable no-new-func */
const val = new Function();
/* eslint-enable no-new-func */
// In Node >=8.1.4: val.name === 'anonymous'
expect(prettyFormat(val)).toEqual('[Function anonymous]');
});
it('prints an anonymous callback function', () => {
let val;
function f(cb: () => void) {
val = cb;
}
f(() => {});
// In Node >=8.1.4: val.name === ''
expect(prettyFormat(val)).toEqual('[Function anonymous]');
});
it('prints an anonymous assigned function', () => {
const val = () => {};
const formatted = prettyFormat(val);
// Node 6.5 infers function names
expect(
formatted === '[Function anonymous]' || formatted === '[Function val]',
).toBeTruthy();
});
it('prints a named function', () => {
const val = function named() {};
expect(prettyFormat(val)).toEqual('[Function named]');
});
it('prints a named generator function', () => {
const val = function* generate() {
yield 1;
yield 2;
yield 3;
};
expect(prettyFormat(val)).toEqual('[Function generate]');
});
it('can customize function names', () => {
const val = function named() {};
expect(
prettyFormat(val, {
printFunctionName: false,
}),
).toEqual('[Function]');
});
it('prints Infinity', () => {
const val = Infinity;
expect(prettyFormat(val)).toEqual('Infinity');
});
it('prints -Infinity', () => {
const val = -Infinity;
expect(prettyFormat(val)).toEqual('-Infinity');
});
it('prints an empty map', () => {
const val = new Map();
expect(prettyFormat(val)).toEqual('Map {}');
});
it('prints a map with values', () => {
const val = new Map();
val.set('prop1', 'value1');
val.set('prop2', 'value2');
expect(prettyFormat(val)).toEqual(
'Map {\n "prop1" => "value1",\n "prop2" => "value2",\n}',
);
});
it('prints a map with non-string keys', () => {
const val = new Map<unknown, unknown>([
[false, 'boolean'],
['false', 'string'],
[0, 'number'],
['0', 'string'],
[null, 'null'],
['null', 'string'],
[undefined, 'undefined'],
['undefined', 'string'],
[Symbol('description'), 'symbol'],
['Symbol(description)', 'string'],
[['array', 'key'], 'array'],
[{key: 'value'}, 'object'],
]);
const expected = [
'Map {',
' false => "boolean",',
' "false" => "string",',
' 0 => "number",',
' "0" => "string",',
' null => "null",',
' "null" => "string",',
' undefined => "undefined",',
' "undefined" => "string",',
' Symbol(description) => "symbol",',
' "Symbol(description)" => "string",',
' Array [',
' "array",',
' "key",',
' ] => "array",',
' Object {',
' "key": "value",',
' } => "object",',
'}',
].join('\n');
expect(prettyFormat(val)).toEqual(expected);
});
it('prints NaN', () => {
const val = NaN;
expect(prettyFormat(val)).toEqual('NaN');
});
it('prints null', () => {
const val = null;
expect(prettyFormat(val)).toEqual('null');
});
it('prints a positive number', () => {
const val = 123;
expect(prettyFormat(val)).toEqual('123');
});
it('prints a negative number', () => {
const val = -123;
expect(prettyFormat(val)).toEqual('-123');
});
it('prints zero', () => {
const val = 0;
expect(prettyFormat(val)).toEqual('0');
});
it('prints negative zero', () => {
const val = -0;
expect(prettyFormat(val)).toEqual('-0');
});
it('prints a positive bigint', () => {
const val = BigInt(123);
expect(prettyFormat(val)).toEqual('123n');
});
it('prints a negative bigint', () => {
const val = BigInt(-123);
expect(prettyFormat(val)).toEqual('-123n');
});
it('prints zero bigint', () => {
const val = BigInt(0);
expect(prettyFormat(val)).toEqual('0n');
});
it('prints negative zero bigint', () => {
const val = BigInt(-0);
expect(prettyFormat(val)).toEqual('0n');
});
it('prints a date', () => {
const val = new Date(10e11);
expect(prettyFormat(val)).toEqual('2001-09-09T01:46:40.000Z');
});
it('prints an invalid date', () => {
const val = new Date(Infinity);
expect(prettyFormat(val)).toEqual('Date { NaN }');
});
it('prints an empty object', () => {
const val = {};
expect(prettyFormat(val)).toEqual('Object {}');
});
it('prints an object with properties', () => {
const val = {prop1: 'value1', prop2: 'value2'};
expect(prettyFormat(val)).toEqual(
'Object {\n "prop1": "value1",\n "prop2": "value2",\n}',
);
});
it('prints an object with properties and symbols', () => {
const val: any = {};
val[Symbol('symbol1')] = 'value2';
val[Symbol('symbol2')] = 'value3';
val.prop = 'value1';
expect(prettyFormat(val)).toEqual(
'Object {\n "prop": "value1",\n Symbol(symbol1): "value2",\n Symbol(symbol2): "value3",\n}',
);
});
it('prints an object without non-enumerable properties which have string key', () => {
const val: unknown = {
enumerable: true,
};
const key = 'non-enumerable';
Object.defineProperty(val, key, {
enumerable: false,
value: false,
});
expect(prettyFormat(val)).toEqual('Object {\n "enumerable": true,\n}');
});
it('prints an object without non-enumerable properties which have symbol key', () => {
const val: unknown = {
enumerable: true,
};
const key = Symbol('non-enumerable');
Object.defineProperty(val, key, {
enumerable: false,
value: false,
});
expect(prettyFormat(val)).toEqual('Object {\n "enumerable": true,\n}');
});
it('prints an object with sorted properties', () => {
/* eslint-disable sort-keys */
const val = {b: 1, a: 2};
/* eslint-enable sort-keys */
expect(prettyFormat(val)).toEqual('Object {\n "a": 2,\n "b": 1,\n}');
});
it('prints an object with keys in their original order', () => {
/* eslint-disable sort-keys */
const val = {b: 1, a: 2};
/* eslint-enable sort-keys */
const compareKeys = () => 0;
expect(prettyFormat(val, {compareKeys})).toEqual(
'Object {\n "b": 1,\n "a": 2,\n}',
);
});
it('prints an object with keys sorted in reverse order', () => {
const val = {a: 1, b: 2};
const compareKeys = (a: string, b: string) => (a > b ? -1 : 1);
expect(prettyFormat(val, {compareKeys})).toEqual(
'Object {\n "b": 2,\n "a": 1,\n}',
);
});
it('prints regular expressions from constructors', () => {
const val = new RegExp('regexp');
expect(prettyFormat(val)).toEqual('/regexp/');
});
it('prints regular expressions from literals', () => {
const val = /regexp/gi;
expect(prettyFormat(val)).toEqual('/regexp/gi');
});
it('prints regular expressions {escapeRegex: false}', () => {
const val = /regexp\d/gi;
expect(prettyFormat(val)).toEqual('/regexp\\d/gi');
});
it('prints regular expressions {escapeRegex: true}', () => {
const val = /regexp\d/gi;
expect(prettyFormat(val, {escapeRegex: true})).toEqual('/regexp\\\\d/gi');
});
it('escapes regular expressions nested inside object', () => {
const obj = {test: /regexp\d/gi};
expect(prettyFormat(obj, {escapeRegex: true})).toEqual(
'Object {\n "test": /regexp\\\\d/gi,\n}',
);
});
it('prints an empty set', () => {
const val = new Set();
expect(prettyFormat(val)).toEqual('Set {}');
});
it('prints a set with values', () => {
const val = new Set();
val.add('value1');
val.add('value2');
expect(prettyFormat(val)).toEqual('Set {\n "value1",\n "value2",\n}');
});
it('prints a string', () => {
const val = 'string';
expect(prettyFormat(val)).toEqual('"string"');
});
it('prints and escape a string', () => {
const val = '"\'\\';
expect(prettyFormat(val)).toEqual('"\\"\'\\\\"');
});
it("doesn't escape string with {excapeString: false}", () => {
const val = '"\'\\n';
expect(prettyFormat(val, {escapeString: false})).toEqual('""\'\\n"');
});
it('prints a string with escapes', () => {
expect(prettyFormat('"-"')).toEqual('"\\"-\\""');
expect(prettyFormat('\\ \\\\')).toEqual('"\\\\ \\\\\\\\"');
});
it('prints a multiline string', () => {
const val = ['line 1', 'line 2', 'line 3'].join('\n');
expect(prettyFormat(val)).toEqual('"' + val + '"');
});
it('prints a multiline string as value of object property', () => {
const polyline = {
props: {
id: 'J',
points: ['0.5,0.460', '0.5,0.875', '0.25,0.875'].join('\n'),
},
type: 'polyline',
};
const val = {
props: {
children: polyline,
},
type: 'svg',
};
expect(prettyFormat(val)).toEqual(
[
'Object {',
' "props": Object {',
' "children": Object {',
' "props": Object {',
' "id": "J",',
' "points": "0.5,0.460',
'0.5,0.875',
'0.25,0.875",',
' },',
' "type": "polyline",',
' },',
' },',
' "type": "svg",',
'}',
].join('\n'),
);
});
it('prints a symbol', () => {
const val = Symbol('symbol');
expect(prettyFormat(val)).toEqual('Symbol(symbol)');
});
it('prints undefined', () => {
const val = undefined;
expect(prettyFormat(val)).toEqual('undefined');
});
it('prints a WeakMap', () => {
const val = new WeakMap();
expect(prettyFormat(val)).toEqual('WeakMap {}');
});
it('prints a WeakSet', () => {
const val = new WeakSet();
expect(prettyFormat(val)).toEqual('WeakSet {}');
});
it('prints deeply nested objects', () => {
const val = {prop: {prop: {prop: 'value'}}};
expect(prettyFormat(val)).toEqual(
'Object {\n "prop": Object {\n "prop": Object {\n "prop": "value",\n },\n },\n}',
);
});
it('prints circular references', () => {
const val: any = {};
val.prop = val;
expect(prettyFormat(val)).toEqual('Object {\n "prop": [Circular],\n}');
});
it('prints parallel references', () => {
const inner = {};
const val = {prop1: inner, prop2: inner};
expect(prettyFormat(val)).toEqual(
'Object {\n "prop1": Object {},\n "prop2": Object {},\n}',
);
});
describe('indent option', () => {
const val = [
{
id: '8658c1d0-9eda-4a90-95e1-8001e8eb6036',
text: 'Add alternative serialize API for pretty-format plugins',
type: 'ADD_TODO',
},
{
id: '8658c1d0-9eda-4a90-95e1-8001e8eb6036',
type: 'TOGGLE_TODO',
},
];
const expected = [
'Array [',
' Object {',
' "id": "8658c1d0-9eda-4a90-95e1-8001e8eb6036",',
' "text": "Add alternative serialize API for pretty-format plugins",',
' "type": "ADD_TODO",',
' },',
' Object {',
' "id": "8658c1d0-9eda-4a90-95e1-8001e8eb6036",',
' "type": "TOGGLE_TODO",',
' },',
']',
].join('\n');
test('default implicit: 2 spaces', () => {
expect(prettyFormat(val)).toEqual(expected);
});
test('default explicit: 2 spaces', () => {
expect(prettyFormat(val, {indent: 2})).toEqual(expected);
});
// Tests assume that no strings in val contain multiple adjacent spaces!
test('non-default: 0 spaces', () => {
const indent = 0;
expect(prettyFormat(val, {indent})).toEqual(
expected.replace(/ {2}/g, ' '.repeat(indent)),
);
});
test('non-default: 4 spaces', () => {
const indent = 4;
expect(prettyFormat(val, {indent})).toEqual(
expected.replace(/ {2}/g, ' '.repeat(indent)),
);
});
});
it('can omit basic prototypes', () => {
const val = {
deeply: {nested: {object: {}}},
'empty array': {},
'empty object': {},
'nested array': [[[]]],
'typed array': new Uint8Array(),
};
expect(prettyFormat(val, {maxDepth: 2, printBasicPrototype: false})).toBe(
[
'{',
' "deeply": {',
' "nested": [Object],',
' },',
' "empty array": {},',
' "empty object": {},',
' "nested array": [',
' [Array],',
' ],',
' "typed array": Uint8Array [],',
'}',
].join('\n'),
);
});
it('can customize the max depth', () => {
const val = [
{
'arguments empty': returnArguments(),
'arguments non-empty': returnArguments('arg'),
'array literal empty': [],
'array literal non-empty': ['item'],
'extended array empty': new MyArray(),
'map empty': new Map(),
'map non-empty': new Map([['name', 'value']]),
'object literal empty': {},
'object literal non-empty': {name: 'value'},
// @ts-expect-error
'object with constructor': new MyObject('value'),
'object without constructor': Object.create(null),
'set empty': new Set(),
'set non-empty': new Set(['value']),
},
];
expect(prettyFormat(val, {maxDepth: 2})).toEqual(
[
'Array [',
' Object {',
' "arguments empty": [Arguments],',
' "arguments non-empty": [Arguments],',
' "array literal empty": [Array],',
' "array literal non-empty": [Array],',
' "extended array empty": [MyArray],',
' "map empty": [Map],',
' "map non-empty": [Map],',
' "object literal empty": [Object],',
' "object literal non-empty": [Object],',
' "object with constructor": [MyObject],',
' "object without constructor": [Object],',
' "set empty": [Set],',
' "set non-empty": [Set],',
' },',
']',
].join('\n'),
);
});
it('throws on invalid options', () => {
expect(() => {
prettyFormat({}, {invalidOption: true});
}).toThrow();
});
it('supports plugins', () => {
class Foo {}
expect(
prettyFormat(new Foo(), {
plugins: [
{
print: () => 'class Foo',
test(object) {
return object.constructor.name === 'Foo';
},
},
],
}),
).toEqual('class Foo');
});
it('supports plugins that return empty string', () => {
const val = {
payload: '',
};
const options = {
plugins: [
{
print(val: any) {
return val.payload;
},
test(val: any) {
return val && typeof val.payload === 'string';
},
},
],
};
expect(prettyFormat(val, options)).toEqual('');
});
it('throws if plugin does not return a string', () => {
const val = 123;
const options = {
plugins: [
{
print(val: unknown) {
return val;
},
test() {
return true;
},
},
],
};
expect(() => {
prettyFormat(val, options);
}).toThrow();
});
it('throws PrettyFormatPluginError if test throws an error', () => {
expect.hasAssertions();
const options = {
plugins: [
{
print: () => '',
test() {
throw new Error('Where is the error?');
},
},
],
};
try {
prettyFormat('', options);
} catch (error: any) {
expect(error.name).toBe('PrettyFormatPluginError');
}
});
it('throws PrettyFormatPluginError if print throws an error', () => {
expect.hasAssertions();
const options = {
plugins: [
{
print: () => {
throw new Error('Where is the error?');
},
test: () => true,
},
],
};
try {
prettyFormat('', options);
} catch (error: any) {
expect(error.name).toBe('PrettyFormatPluginError');
}
});
it('throws PrettyFormatPluginError if serialize throws an error', () => {
expect.hasAssertions();
const options = {
plugins: [
{
serialize: () => {
throw new Error('Where is the error?');
},
test: () => true,
},
],
};
try {
prettyFormat('', options);
} catch (error: any) {
expect(error.name).toBe('PrettyFormatPluginError');
}
});
it('supports plugins with deeply nested arrays (#24)', () => {
const val = [
[1, 2],
[3, 4],
];
expect(
prettyFormat(val, {
plugins: [
{
print(val: Array<unknown>, print: any) {
return val.map(item => print(item)).join(' - ');
},
test(val: unknown) {
return Array.isArray(val);
},
},
],
}),
).toEqual('1 - 2 - 3 - 4');
});
it('should call plugins on nested basic values', () => {
const val = {prop: 42};
expect(
prettyFormat(val, {
plugins: [
{
print(_val, _print) {
return '[called]';
},
test(val) {
return typeof val === 'string' || typeof val === 'number';
},
},
],
}),
).toEqual('Object {\n [called]: [called],\n}');
});
it('prints objects with no constructor', () => {
expect(prettyFormat(Object.create(null))).toEqual('Object {}');
});
it('prints identity-obj-proxy with string constructor', () => {
const val = Object.create(null);
val.constructor = 'constructor'; // mock the mock object :)
const expected = [
'Object {', // Object instead of undefined
' "constructor": "constructor",',
'}',
].join('\n');
expect(prettyFormat(val)).toEqual(expected);
});
it('calls toJSON and prints its return value', () => {
expect(
prettyFormat({
toJSON: () => ({value: false}),
value: true,
}),
).toEqual('Object {\n "value": false,\n}');
});
it('calls toJSON and prints an internal representation.', () => {
expect(
prettyFormat({
toJSON: () => '[Internal Object]',
value: true,
}),
).toEqual('"[Internal Object]"');
});
it('calls toJSON only on functions', () => {
expect(
prettyFormat({
toJSON: false,
value: true,
}),
).toEqual('Object {\n "toJSON": false,\n "value": true,\n}');
});
it('does not call toJSON recursively', () => {
expect(
prettyFormat({
toJSON: () => ({toJSON: () => ({value: true})}),
value: false,
}),
).toEqual('Object {\n "toJSON": [Function toJSON],\n}');
});
it('calls toJSON on Sets', () => {
const set = new Set([1]);
(set as any).toJSON = () => 'map';
expect(prettyFormat(set)).toEqual('"map"');
});
it('disables toJSON calls through options', () => {
const value = {apple: 'banana', toJSON: jest.fn(() => '1')};
const name = value.toJSON.name || 'anonymous';
const set = new Set([value]);
(set as any).toJSON = jest.fn(() => 'map');
expect(
prettyFormat(set, {
callToJSON: false,
}),
).toEqual(
'Set {\n Object {\n "apple": "banana",\n "toJSON": [Function ' +
name +
'],\n },\n}',
);
expect((set as any).toJSON).not.toBeCalled();
expect(value.toJSON).not.toBeCalled();
});
describe('min', () => {
it('prints some basic values in min mode', () => {
const val = {
boolean: [false, true],
null: null,
number: [0, -0, 123, -123, Infinity, -Infinity, NaN],
string: ['', 'non-empty'],
undefined,
};
expect(
prettyFormat(val, {
min: true,
}),
).toEqual(
'{' +
[
'"boolean": [false, true]',
'"null": null',
'"number": [0, -0, 123, -123, Infinity, -Infinity, NaN]',
'"string": ["", "non-empty"]',
'"undefined": undefined',
].join(', ') +
'}',
);
});
it('prints some complex values in min mode', () => {
const val = {
'arguments empty': returnArguments(),
'arguments non-empty': returnArguments('arg'),
'array literal empty': [],
'array literal non-empty': ['item'],
'extended array empty': new MyArray(),
'map empty': new Map(),
'map non-empty': new Map([['name', 'value']]),
'object literal empty': {},
'object literal non-empty': {name: 'value'},
// @ts-expect-error
'object with constructor': new MyObject('value'),
'object without constructor': Object.create(null),
'set empty': new Set(),
'set non-empty': new Set(['value']),
};
expect(
prettyFormat(val, {
min: true,
}),
).toEqual(
'{' +
[
'"arguments empty": []',
'"arguments non-empty": ["arg"]',
'"array literal empty": []',
'"array literal non-empty": ["item"]',
'"extended array empty": []',
'"map empty": Map {}',
'"map non-empty": Map {"name" => "value"}',
'"object literal empty": {}',
'"object literal non-empty": {"name": "value"}',
'"object with constructor": {"name": "value"}',
'"object without constructor": {}',
'"set empty": Set {}',
'"set non-empty": Set {"value"}',
].join(', ') +
'}',
);
});
it('does not allow indent !== 0 in min mode', () => {
expect(() => {
prettyFormat(1, {indent: 1, min: true});
}).toThrow();
});
});
});