Skip to content

Commit

Permalink
feat(pretty-print): add option printBasicPrototype (#11441)
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing authored May 25, 2021
1 parent 2226742 commit b68d91b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- `[jest-worker]` Add support for custom task queues and adds a `PriorityQueue` implementation. ([#10921](https://github.com/facebook/jest/pull/10921))
- `[jest-worker]` Add in-order scheduling policy to jest worker ([10902](https://github.com/facebook/jest/pull/10902))
- `[pretty-format]` Better print for sparse arrays ([11326](https://github.com/facebook/jest/pull/11326))
- `[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))

### Fixes

Expand Down
25 changes: 13 additions & 12 deletions packages/pretty-format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ console.log(prettyFormat(onClick, options));
```

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

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

Expand Down
25 changes: 25 additions & 0 deletions packages/pretty-format/src/__tests__/prettyFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,31 @@ describe('prettyFormat()', () => {
});
});

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 = [
{
Expand Down
14 changes: 12 additions & 2 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ function printComplexValue(
if (isToStringedArrayType(toStringed)) {
return hitMaxDepth
? '[' + val.constructor.name + ']'
: (min ? '' : val.constructor.name + ' ') +
: (min
? ''
: !config.printBasicPrototype && val.constructor.name === 'Array'
? ''
: val.constructor.name + ' ') +
'[' +
printListItems(val, config, indentation, depth, refs, printer) +
']';
Expand Down Expand Up @@ -276,7 +280,11 @@ function printComplexValue(
// For example, not even relevant if window is prop of React element.
return hitMaxDepth || isWindow(val)
? '[' + getConstructorName(val) + ']'
: (min ? '' : getConstructorName(val) + ' ') +
: (min
? ''
: !config.printBasicPrototype && getConstructorName(val) === 'Object'
? ''
: getConstructorName(val) + ' ') +
'{' +
printObjectProperties(val, config, indentation, depth, refs, printer) +
'}';
Expand Down Expand Up @@ -395,6 +403,7 @@ const DEFAULT_OPTIONS: Options = {
maxDepth: Infinity,
min: false,
plugins: [],
printBasicPrototype: true,
printFunctionName: true,
theme: DEFAULT_THEME,
};
Expand Down Expand Up @@ -495,6 +504,7 @@ const getConfig = (options?: OptionsReceived): Config => ({
options && options.plugins !== undefined
? options.plugins
: DEFAULT_OPTIONS.plugins,
printBasicPrototype: options?.printBasicPrototype ?? true,
printFunctionName: getPrintFunctionName(options),
spacingInner: options && options.min ? ' ' : '\n',
spacingOuter: options && options.min ? '' : '\n',
Expand Down
3 changes: 3 additions & 0 deletions packages/pretty-format/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type Options = {
maxDepth: number;
min: boolean;
plugins: Plugins;
printBasicPrototype: boolean;
printFunctionName: boolean;
theme: Theme;
};
Expand All @@ -54,6 +55,7 @@ export type OptionsReceived = {
maxDepth?: number;
min?: boolean;
plugins?: Plugins;
printBasicPrototype?: boolean;
printFunctionName?: boolean;
theme?: ThemeReceived;
};
Expand All @@ -67,6 +69,7 @@ export type Config = {
maxDepth: number;
min: boolean;
plugins: Plugins;
printBasicPrototype: boolean;
printFunctionName: boolean;
spacingInner: string;
spacingOuter: string;
Expand Down

0 comments on commit b68d91b

Please sign in to comment.