Skip to content

Commit b68d91b

Browse files
authored
feat(pretty-print): add option printBasicPrototype (#11441)
1 parent 2226742 commit b68d91b

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
- `[jest-worker]` Add support for custom task queues and adds a `PriorityQueue` implementation. ([#10921](https://github.com/facebook/jest/pull/10921))
5252
- `[jest-worker]` Add in-order scheduling policy to jest worker ([10902](https://github.com/facebook/jest/pull/10902))
5353
- `[pretty-format]` Better print for sparse arrays ([11326](https://github.com/facebook/jest/pull/11326))
54+
- `[pretty-print]` Add option `printBasicPrototype` which determines whether or not the prototype should be printed for raw objects or arrays ([#11441](https://github.com/facebook/jest/pull/11441))
5455

5556
### Fixes
5657

packages/pretty-format/README.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ console.log(prettyFormat(onClick, options));
6666
```
6767

6868
<!-- prettier-ignore -->
69-
| key | type | default | description |
70-
| :------------------ | :-------- | :--------- | :------------------------------------------------------ |
71-
| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
72-
| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
73-
| `escapeString` | `boolean` | `true` | escape special characters in strings |
74-
| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
75-
| `indent` | `number` | `2` | spaces in each level of indentation |
76-
| `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on |
77-
| `min` | `boolean` | `false` | minimize added space: no indentation nor line breaks |
78-
| `plugins` | `array` | `[]` | plugins to serialize application-specific data types |
79-
| `printFunctionName` | `boolean` | `true` | include or omit the name of a function |
80-
| `theme` | `object` | | colors to highlight syntax in terminal |
69+
| key | type | default | description |
70+
| :-------------------- | :-------- | :--------- | :------------------------------------------------------ |
71+
| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
72+
| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
73+
| `escapeString` | `boolean` | `true` | escape special characters in strings |
74+
| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
75+
| `indent` | `number` | `2` | spaces in each level of indentation |
76+
| `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on |
77+
| `min` | `boolean` | `false` | minimize added space: no indentation nor line breaks |
78+
| `plugins` | `array` | `[]` | plugins to serialize application-specific data types |
79+
| `printBasicPrototype` | `boolean` | `false` | print the prototype for plain objects and arrays |
80+
| `printFunctionName` | `boolean` | `true` | include or omit the name of a function |
81+
| `theme` | `object` | | colors to highlight syntax in terminal |
8182

8283
Property values of `theme` are from [ansi-styles colors](https://github.com/chalk/ansi-styles#colors)
8384

packages/pretty-format/src/__tests__/prettyFormat.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,31 @@ describe('prettyFormat()', () => {
524524
});
525525
});
526526

527+
it('can omit basic prototypes', () => {
528+
const val = {
529+
deeply: {nested: {object: {}}},
530+
'empty array': {},
531+
'empty object': {},
532+
'nested array': [[[]]],
533+
'typed array': new Uint8Array(),
534+
};
535+
expect(prettyFormat(val, {maxDepth: 2, printBasicPrototype: false})).toBe(
536+
[
537+
'{',
538+
' "deeply": {',
539+
' "nested": [Object],',
540+
' },',
541+
' "empty array": {},',
542+
' "empty object": {},',
543+
' "nested array": [',
544+
' [Array],',
545+
' ],',
546+
' "typed array": Uint8Array [],',
547+
'}',
548+
].join('\n'),
549+
);
550+
});
551+
527552
it('can customize the max depth', () => {
528553
const val = [
529554
{

packages/pretty-format/src/index.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ function printComplexValue(
237237
if (isToStringedArrayType(toStringed)) {
238238
return hitMaxDepth
239239
? '[' + val.constructor.name + ']'
240-
: (min ? '' : val.constructor.name + ' ') +
240+
: (min
241+
? ''
242+
: !config.printBasicPrototype && val.constructor.name === 'Array'
243+
? ''
244+
: val.constructor.name + ' ') +
241245
'[' +
242246
printListItems(val, config, indentation, depth, refs, printer) +
243247
']';
@@ -276,7 +280,11 @@ function printComplexValue(
276280
// For example, not even relevant if window is prop of React element.
277281
return hitMaxDepth || isWindow(val)
278282
? '[' + getConstructorName(val) + ']'
279-
: (min ? '' : getConstructorName(val) + ' ') +
283+
: (min
284+
? ''
285+
: !config.printBasicPrototype && getConstructorName(val) === 'Object'
286+
? ''
287+
: getConstructorName(val) + ' ') +
280288
'{' +
281289
printObjectProperties(val, config, indentation, depth, refs, printer) +
282290
'}';
@@ -395,6 +403,7 @@ const DEFAULT_OPTIONS: Options = {
395403
maxDepth: Infinity,
396404
min: false,
397405
plugins: [],
406+
printBasicPrototype: true,
398407
printFunctionName: true,
399408
theme: DEFAULT_THEME,
400409
};
@@ -495,6 +504,7 @@ const getConfig = (options?: OptionsReceived): Config => ({
495504
options && options.plugins !== undefined
496505
? options.plugins
497506
: DEFAULT_OPTIONS.plugins,
507+
printBasicPrototype: options?.printBasicPrototype ?? true,
498508
printFunctionName: getPrintFunctionName(options),
499509
spacingInner: options && options.min ? ' ' : '\n',
500510
spacingOuter: options && options.min ? '' : '\n',

packages/pretty-format/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type Options = {
4141
maxDepth: number;
4242
min: boolean;
4343
plugins: Plugins;
44+
printBasicPrototype: boolean;
4445
printFunctionName: boolean;
4546
theme: Theme;
4647
};
@@ -54,6 +55,7 @@ export type OptionsReceived = {
5455
maxDepth?: number;
5556
min?: boolean;
5657
plugins?: Plugins;
58+
printBasicPrototype?: boolean;
5759
printFunctionName?: boolean;
5860
theme?: ThemeReceived;
5961
};
@@ -67,6 +69,7 @@ export type Config = {
6769
maxDepth: number;
6870
min: boolean;
6971
plugins: Plugins;
72+
printBasicPrototype: boolean;
7073
printFunctionName: boolean;
7174
spacingInner: string;
7275
spacingOuter: string;

0 commit comments

Comments
 (0)