UNPKG

14.3 kBMarkdownView Raw
1# pretty-format
2
3Stringify any JavaScript value.
4
5- Serialize built-in JavaScript types.
6- Serialize application-specific data types with built-in or user-defined plugins.
7
8## Installation
9
10```sh
11$ yarn add pretty-format
12```
13
14## Usage
15
16```js
17const {format: prettyFormat} = require('pretty-format'); // CommonJS
18```
19
20```js
21import {format as prettyFormat} from 'pretty-format'; // ES2015 modules
22```
23
24```js
25const val = {object: {}};
26val.circularReference = val;
27val[Symbol('foo')] = 'foo';
28val.map = new Map([['prop', 'value']]);
29val.array = [-0, Infinity, NaN];
30
31console.log(prettyFormat(val));
32/*
33Object {
34 "array": Array [
35 -0,
36 Infinity,
37 NaN,
38 ],
39 "circularReference": [Circular],
40 "map": Map {
41 "prop" => "value",
42 },
43 "object": Object {},
44 Symbol(foo): "foo",
45}
46*/
47```
48
49## Usage with options
50
51```js
52function onClick() {}
53
54console.log(prettyFormat(onClick));
55/*
56[Function onClick]
57*/
58
59const options = {
60 printFunctionName: false,
61};
62console.log(prettyFormat(onClick, options));
63/*
64[Function]
65*/
66```
67
68<!-- prettier-ignore -->
69| key | type | default | description |
70| :-------------------- | :--------------- | :---------- | :-------------------------------------------------------------------------------------- |
71| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
72| `compareKeys` | `function\|null` | `undefined` | compare function used when sorting object keys, `null` can be used to skip over sorting |
73| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
74| `escapeString` | `boolean` | `true` | escape special characters in strings |
75| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
76| `indent` | `number` | `2` | spaces in each level of indentation |
77| `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on |
78| `maxWidth` | `number` | `Infinity` | number of elements to print in arrays, sets, and so on |
79| `min` | `boolean` | `false` | minimize added space: no indentation nor line breaks |
80| `plugins` | `array` | `[]` | plugins to serialize application-specific data types |
81| `printBasicPrototype` | `boolean` | `false` | print the prototype for plain objects and arrays |
82| `printFunctionName` | `boolean` | `true` | include or omit the name of a function |
83| `theme` | `object` | | colors to highlight syntax in terminal |
84
85Property values of `theme` are from [ansi-styles colors](https://github.com/chalk/ansi-styles#colors)
86
87```js
88const DEFAULT_THEME = {
89 comment: 'gray',
90 content: 'reset',
91 prop: 'yellow',
92 tag: 'cyan',
93 value: 'green',
94};
95```
96
97## Usage with plugins
98
99The `pretty-format` package provides some built-in plugins, including:
100
101- `ReactElement` for elements from `react`
102- `ReactTestComponent` for test objects from `react-test-renderer`
103
104```js
105// CommonJS
106const React = require('react');
107const renderer = require('react-test-renderer');
108const {format: prettyFormat, plugins} = require('pretty-format');
109
110const {ReactElement, ReactTestComponent} = plugins;
111```
112
113```js
114// ES2015 modules and destructuring assignment
115import React from 'react';
116import renderer from 'react-test-renderer';
117import {plugins, format as prettyFormat} from 'pretty-format';
118
119const {ReactElement, ReactTestComponent} = plugins;
120```
121
122```js
123const onClick = () => {};
124const element = React.createElement('button', {onClick}, 'Hello World');
125
126const formatted1 = prettyFormat(element, {
127 plugins: [ReactElement],
128 printFunctionName: false,
129});
130const formatted2 = prettyFormat(renderer.create(element).toJSON(), {
131 plugins: [ReactTestComponent],
132 printFunctionName: false,
133});
134/*
135<button
136 onClick=[Function]
137>
138 Hello World
139</button>
140*/
141```
142
143## Usage in Jest
144
145For snapshot tests, Jest uses `pretty-format` with options that include some of its built-in plugins. For this purpose, plugins are also known as **snapshot serializers**.
146
147To serialize application-specific data types, you can add modules to `devDependencies` of a project, and then:
148
149In an **individual** test file, you can add a module as follows. It precedes any modules from Jest configuration.
150
151```js
152import serializer from 'my-serializer-module';
153expect.addSnapshotSerializer(serializer);
154
155// tests which have `expect(value).toMatchSnapshot()` assertions
156```
157
158For **all** test files, you can specify modules in Jest configuration. They precede built-in plugins for React, HTML, and Immutable.js data types. For example, in a `package.json` file:
159
160```json
161{
162 "jest": {
163 "snapshotSerializers": ["my-serializer-module"]
164 }
165}
166```
167
168## Writing plugins
169
170A plugin is a JavaScript object.
171
172If `options` has a `plugins` array: for the first plugin whose `test(val)` method returns a truthy value, then `prettyFormat(val, options)` returns the result from either:
173
174- `serialize(val, …)` method of the **improved** interface (available in **version 21** or later)
175- `print(val, …)` method of the **original** interface (if plugin does not have `serialize` method)
176
177### test
178
179Write `test` so it can receive `val` argument of any type. To serialize **objects** which have certain properties, then a guarded expression like `val != null && …` or more concise `val && …` prevents the following errors:
180
181- `TypeError: Cannot read property 'whatever' of null`
182- `TypeError: Cannot read property 'whatever' of undefined`
183
184For example, `test` method of built-in `ReactElement` plugin:
185
186```js
187const elementSymbol = Symbol.for('react.element');
188const test = val => val && val.$$typeof === elementSymbol;
189```
190
191Pay attention to efficiency in `test` because `pretty-format` calls it often.
192
193### serialize
194
195The **improved** interface is available in **version 21** or later.
196
197Write `serialize` to return a string, given the arguments:
198
199- `val` which “passed the test”
200- unchanging `config` object: derived from `options`
201- current `indentation` string: concatenate to `indent` from `config`
202- current `depth` number: compare to `maxDepth` from `config`
203- current `refs` array: find circular references in objects
204- `printer` callback function: serialize children
205
206### config
207
208<!-- prettier-ignore -->
209| key | type | description |
210| :------------------ | :--------------- | :-------------------------------------------------------------------------------------- |
211| `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects |
212| `compareKeys` | `function\|null` | compare function used when sorting object keys, `null` can be used to skip over sorting |
213| `colors` | `Object` | escape codes for colors to highlight syntax |
214| `escapeRegex` | `boolean` | escape special characters in regular expressions |
215| `escapeString` | `boolean` | escape special characters in strings |
216| `indent` | `string` | spaces in each level of indentation |
217| `maxDepth` | `number` | levels to print in arrays, objects, elements, and so on |
218| `min` | `boolean` | minimize added space: no indentation nor line breaks |
219| `plugins` | `array` | plugins to serialize application-specific data types |
220| `printFunctionName` | `boolean` | include or omit the name of a function |
221| `spacingInner` | `string` | spacing to separate items in a list |
222| `spacingOuter` | `string` | spacing to enclose a list of items |
223
224Each property of `colors` in `config` corresponds to a property of `theme` in `options`:
225
226- the key is the same (for example, `tag`)
227- the value in `colors` is a object with `open` and `close` properties whose values are escape codes from [ansi-styles](https://github.com/chalk/ansi-styles) for the color value in `theme` (for example, `'cyan'`)
228
229Some properties in `config` are derived from `min` in `options`:
230
231- `spacingInner` and `spacingOuter` are **newline** if `min` is `false`
232- `spacingInner` is **space** and `spacingOuter` is **empty string** if `min` is `true`
233
234### Example of serialize and test
235
236This plugin is a pattern you can apply to serialize composite data types. Side note: `pretty-format` does not need a plugin to serialize arrays.
237
238```js
239// We reused more code when we factored out a function for child items
240// that is independent of depth, name, and enclosing punctuation (see below).
241const SEPARATOR = ',';
242function serializeItems(items, config, indentation, depth, refs, printer) {
243 if (items.length === 0) {
244 return '';
245 }
246 const indentationItems = indentation + config.indent;
247 return (
248 config.spacingOuter +
249 items
250 .map(
251 item =>
252 indentationItems +
253 printer(item, config, indentationItems, depth, refs), // callback
254 )
255 .join(SEPARATOR + config.spacingInner) +
256 (config.min ? '' : SEPARATOR) + // following the last item
257 config.spacingOuter +
258 indentation
259 );
260}
261
262const plugin = {
263 test(val) {
264 return Array.isArray(val);
265 },
266 serialize(array, config, indentation, depth, refs, printer) {
267 const name = array.constructor.name;
268 return ++depth > config.maxDepth
269 ? `[${name}]`
270 : `${config.min ? '' : `${name} `}[${serializeItems(
271 array,
272 config,
273 indentation,
274 depth,
275 refs,
276 printer,
277 )}]`;
278 },
279};
280```
281
282```js
283const val = {
284 filter: 'completed',
285 items: [
286 {
287 text: 'Write test',
288 completed: true,
289 },
290 {
291 text: 'Write serialize',
292 completed: true,
293 },
294 ],
295};
296```
297
298```js
299console.log(
300 prettyFormat(val, {
301 plugins: [plugin],
302 }),
303);
304/*
305Object {
306 "filter": "completed",
307 "items": Array [
308 Object {
309 "completed": true,
310 "text": "Write test",
311 },
312 Object {
313 "completed": true,
314 "text": "Write serialize",
315 },
316 ],
317}
318*/
319```
320
321```js
322console.log(
323 prettyFormat(val, {
324 indent: 4,
325 plugins: [plugin],
326 }),
327);
328/*
329Object {
330 "filter": "completed",
331 "items": Array [
332 Object {
333 "completed": true,
334 "text": "Write test",
335 },
336 Object {
337 "completed": true,
338 "text": "Write serialize",
339 },
340 ],
341}
342*/
343```
344
345```js
346console.log(
347 prettyFormat(val, {
348 maxDepth: 1,
349 plugins: [plugin],
350 }),
351);
352/*
353Object {
354 "filter": "completed",
355 "items": [Array],
356}
357*/
358```
359
360```js
361console.log(
362 prettyFormat(val, {
363 min: true,
364 plugins: [plugin],
365 }),
366);
367/*
368{"filter": "completed", "items": [{"completed": true, "text": "Write test"}, {"completed": true, "text": "Write serialize"}]}
369*/
370```
371
372### print
373
374The **original** interface is adequate for plugins:
375
376- that **do not** depend on options other than `highlight` or `min`
377- that **do not** depend on `depth` or `refs` in recursive traversal, and
378- if values either
379 - do **not** require indentation, or
380 - do **not** occur as children of JavaScript data structures (for example, array)
381
382Write `print` to return a string, given the arguments:
383
384- `val` which “passed the test”
385- current `printer(valChild)` callback function: serialize children
386- current `indenter(lines)` callback function: indent lines at the next level
387- unchanging `config` object: derived from `options`
388- unchanging `colors` object: derived from `options`
389
390The 3 properties of `config` are `min` in `options` and:
391
392- `spacing` and `edgeSpacing` are **newline** if `min` is `false`
393- `spacing` is **space** and `edgeSpacing` is **empty string** if `min` is `true`
394
395Each property of `colors` corresponds to a property of `theme` in `options`:
396
397- the key is the same (for example, `tag`)
398- the value in `colors` is a object with `open` and `close` properties whose values are escape codes from [ansi-styles](https://github.com/chalk/ansi-styles) for the color value in `theme` (for example, `'cyan'`)
399
400### Example of print and test
401
402This plugin prints functions with the **number of named arguments** excluding rest argument.
403
404```js
405const plugin = {
406 print(val) {
407 return `[Function ${val.name || 'anonymous'} ${val.length}]`;
408 },
409 test(val) {
410 return typeof val === 'function';
411 },
412};
413```
414
415```js
416const val = {
417 onClick(event) {},
418 render() {},
419};
420
421prettyFormat(val, {
422 plugins: [plugin],
423});
424/*
425Object {
426 "onClick": [Function onClick 1],
427 "render": [Function render 0],
428}
429*/
430
431prettyFormat(val);
432/*
433Object {
434 "onClick": [Function onClick],
435 "render": [Function render],
436}
437*/
438```
439
440This plugin **ignores** the `printFunctionName` option. That limitation of the original `print` interface is a reason to use the improved `serialize` interface, described above.
441
442```js
443prettyFormat(val, {
444 plugins: [pluginOld],
445 printFunctionName: false,
446});
447/*
448Object {
449 "onClick": [Function onClick 1],
450 "render": [Function render 0],
451}
452*/
453
454prettyFormat(val, {
455 printFunctionName: false,
456});
457/*
458Object {
459 "onClick": [Function],
460 "render": [Function],
461}
462*/
463```