|
| 1 | +# @vitest/pretty-format |
| 2 | + |
| 3 | +Vitest's fork of Jest's [`pretty-format`](https://npmx.dev/package/pretty-format), published as an ESM-only package. |
| 4 | + |
| 5 | +This package powers several formatting paths in Vitest: |
| 6 | + |
| 7 | +- snapshot serialization |
| 8 | +- assertion diff rendering |
| 9 | +- matcher and error messages |
| 10 | +- browser `prettyDOM` output |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +```ts |
| 15 | +import { format } from '@vitest/pretty-format' |
| 16 | + |
| 17 | +const value = { |
| 18 | + user: 'Ada', |
| 19 | + items: [1, 2, 3], |
| 20 | +} |
| 21 | + |
| 22 | +console.log(format(value)) |
| 23 | +/* |
| 24 | +-- output -- |
| 25 | +Object { |
| 26 | + "items": Array [ |
| 27 | + 1, |
| 28 | + 2, |
| 29 | + 3, |
| 30 | + ], |
| 31 | + "user": "Ada", |
| 32 | +} |
| 33 | +*/ |
| 34 | +``` |
| 35 | + |
| 36 | +## Options |
| 37 | + |
| 38 | +| key | type | default | notes | |
| 39 | +| :-------------------- | :--------------- | :---------- | :------------------------------------------------------------------- | |
| 40 | +| `callToJSON` | `boolean` | `true` | Call `toJSON` if present | |
| 41 | +| `compareKeys` | `function\|null` | `undefined` | Compare function for sorting object keys. Use `null` to skip sorting | |
| 42 | +| `escapeRegex` | `boolean` | `false` | Escape special characters in regular expressions | |
| 43 | +| `escapeString` | `boolean` | `true` | Escape special characters in strings | |
| 44 | +| `highlight` | `boolean` | `false` | Highlight syntax with terminal colors | |
| 45 | +| `indent` | `number` | `2` | Spaces per indentation level | |
| 46 | +| `maxDepth` | `number` | `Infinity` | Maximum depth to print | |
| 47 | +| `maxOutputLength` | `number` | `1_000_000` | Approximate per-depth output budget | |
| 48 | +| `maxWidth` | `number` | `Infinity` | Maximum number of items to print in collections | |
| 49 | +| `min` | `boolean` | `false` | Minimize added whitespace | |
| 50 | +| `plugins` | `array` | `[]` | Plugins to serialize application-specific data types | |
| 51 | +| `printBasicPrototype` | `boolean` | `true` | Print `Object` and `Array` prefixes for plain objects and arrays | |
| 52 | +| `printFunctionName` | `boolean` | `true` | Include or omit the function name | |
| 53 | +| `printShadowRoot` | `boolean` | `true` | Include shadow-root contents when formatting DOM nodes | |
| 54 | + |
| 55 | +Important: |
| 56 | + |
| 57 | +- `plugins: []` means the package does not auto-enable its built-in plugins by default |
| 58 | +- Vitest features opt into their own plugin stacks and option presets |
| 59 | + |
| 60 | +## Built-in Plugins |
| 61 | + |
| 62 | +The package exports these built-in plugins: |
| 63 | + |
| 64 | +- `ReactTestComponent` |
| 65 | +- `ReactElement` |
| 66 | +- `DOMElement` |
| 67 | +- `DOMCollection` |
| 68 | +- `Immutable` |
| 69 | +- `AsymmetricMatcher` |
| 70 | +- `Error` |
| 71 | + |
| 72 | +You can use them directly with `format(..., { plugins })`: |
| 73 | + |
| 74 | +```ts |
| 75 | +import { format, plugins } from '@vitest/pretty-format' |
| 76 | + |
| 77 | +console.log( |
| 78 | + format(document.body, { |
| 79 | + plugins: [plugins.DOMElement, plugins.DOMCollection], |
| 80 | + }), |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +## Vitest Extensions |
| 85 | + |
| 86 | +Besides the inherited `pretty-format` API surface, Vitest currently adds and documents these notable behaviors: |
| 87 | + |
| 88 | +### `printShadowRoot` |
| 89 | + |
| 90 | +Controls whether DOM serialization includes shadow-root contents. |
| 91 | + |
| 92 | +```ts |
| 93 | +format(element, { |
| 94 | + printShadowRoot: false, |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +### `maxOutputLength` |
| 99 | + |
| 100 | +Approximate per-depth output budget used to prevent pathological expansion of large recursive structures. |
| 101 | + |
| 102 | +This is a heuristic safety valve, not a hard cap on the final string length. |
| 103 | + |
| 104 | +```ts |
| 105 | +format(value, { |
| 106 | + maxOutputLength: 100_000, |
| 107 | +}) |
| 108 | +``` |
| 109 | + |
| 110 | +## How Vitest Uses It |
| 111 | + |
| 112 | +### Snapshots |
| 113 | + |
| 114 | +Snapshots use `@vitest/pretty-format` with snapshot-specific defaults such as: |
| 115 | + |
| 116 | +- `printBasicPrototype: false` |
| 117 | +- `escapeString: false` |
| 118 | +- `escapeRegex: true` |
| 119 | +- `printFunctionName: false` |
| 120 | + |
| 121 | +Default snapshot plugin stack: |
| 122 | + |
| 123 | +- `ReactTestComponent` |
| 124 | +- `ReactElement` |
| 125 | +- `DOMElement` |
| 126 | +- `DOMCollection` |
| 127 | +- `Immutable` |
| 128 | +- `AsymmetricMatcher` |
| 129 | +- `MockSerializer` |
| 130 | + |
| 131 | +Snapshot formatting is configured through [`test.snapshotFormat`](https://vitest.dev/config/snapshotformat), while serializer registration goes through [`expect.addSnapshotSerializer`](https://vitest.dev/api/expect#expect-addsnapshotserializer) or [`snapshotSerializers`](https://vitest.dev/config/snapshotserializers). |
| 132 | + |
| 133 | +### Diffs |
| 134 | + |
| 135 | +Assertion diffs use a different preset and plugin stack. |
| 136 | + |
| 137 | +Default diff plugins: |
| 138 | + |
| 139 | +- `ReactTestComponent` |
| 140 | +- `ReactElement` |
| 141 | +- `DOMElement` |
| 142 | +- `DOMCollection` |
| 143 | +- `Immutable` |
| 144 | +- `AsymmetricMatcher` |
| 145 | +- `Error` |
| 146 | + |
| 147 | +### Vitest `stringify` |
| 148 | + |
| 149 | +Matcher and error messages commonly go through Vitest's internal [`stringify`](https://github.com/vitest-dev/vitest/blob/59b0e6411be2b4aa5f2b339d02691aa83d5e403f/packages/utils/src/display.ts#L49) utility, which uses: |
| 150 | + |
| 151 | +- `ReactTestComponent` |
| 152 | +- `ReactElement` |
| 153 | +- `DOMElement` |
| 154 | +- `DOMCollection` |
| 155 | +- `Immutable` |
| 156 | +- `AsymmetricMatcher` |
| 157 | + |
| 158 | +`stringify` also adds wrapper-level behavior on top of `@vitest/pretty-format`: |
| 159 | + |
| 160 | +- `maxLength`: if the formatted output grows too large, `stringify` retries with a smaller `maxDepth` to keep the result bounded |
| 161 | +- `filterNode`: swaps the default DOM plugin for a filtered variant so selected nodes are omitted from the output |
| 162 | +- fallback on formatter errors: if formatting throws, `stringify` retries with `callToJSON: false` |
| 163 | + |
| 164 | +### Browser `prettyDOM` |
| 165 | + |
| 166 | +Browser `prettyDOM` builds on Vitest's `stringify` path and enables browser-oriented defaults such as: |
| 167 | + |
| 168 | +- `highlight: true` |
| 169 | + |
| 170 | +It can also replace the default DOM plugin with a filtered variant when `filterNode` is configured. |
0 commit comments