Registry / testing / pretty-format

pretty-format

JSON →
library30.3.0jsnpmunverified

pretty-format is a robust utility for stringifying any JavaScript value, particularly useful for debugging and generating consistent output for snapshot testing. Maintained as a core package within the Jest testing framework, it is currently on version 30.3.0 and aligns its major releases with Jest itself, which aims for more frequent major updates going forward. Key differentiators include its extensibility through a powerful plugin system, allowing serialization of application-specific data types (often called snapshot serializers in the context of Jest). It can handle circular references, various built-in types, and offers extensive configuration options for output style, indentation, and color highlighting.

npm install pretty-format
INSTALL
IMPORT
SIG · PRETTY-FORMAT
P
pretty-format
testingjavascriptv30.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

format
import { format } from 'pretty-format';
const format = require('pretty-format');
While CommonJS `require` still works, ES Modules `import` is the recommended standard for modern Node.js environments (v18.14.0+), aligning with Jest 30's requirements. The `format` function is a named export.
prettyFormat
import { format as prettyFormat } from 'pretty-format';
import prettyFormat from 'pretty-format';
The primary formatting function is a named export `format`. It's common practice to alias it to `prettyFormat` for clarity, as seen in Jest's own usage examples. There is no default export.
plugins
import { format, plugins } from 'pretty-format'; // or specific plugins: import { ReactElement, ReactTestComponent } from 'pretty-format/build/plugins';
import { plugins } from 'pretty-format/plugins';
Built-in plugins like `ReactElement` and `ReactTestComponent` are available for specialized formatting. They are typically imported directly from `pretty-format/build/plugins` when used outside of Jest's automatic configuration.

Demonstrates basic usage of `pretty-format` with a complex object, including circular references, various JavaScript types, and the use of built-in React plugins and a simple custom plugin.

import { format, plugins } from 'pretty-format'; const { ReactElement, ReactTestComponent } = plugins; const val = { string: 'hello world', number: 123.45, boolean: true, array: [-0, Infinity, NaN, null, undefined], object: { key: 'value', nested: { id: 1 } }, circularReference: null, map: new Map([['prop', 'value']]), set: new Set([1, 2, 3]), symbol: Symbol('description'), func: function myFunc() {}, date: new Date('2023-01-01T12:00:00.000Z') }; val.circularReference = val; // Create a circular reference const options = { indent: 2, min: false, callToJSON: true, plugins: [ReactElement, ReactTestComponent] }; console.log(format(val, options)); // Example with a custom plugin (simple) const customPlugin = { test(value) { return value instanceof Error; }, serialize(value, config, indentation, depth, refs, printer) { return `Custom Error: ${value.message}`; }, }; const error = new Error('Something went wrong'); console.log(format(error, { plugins: [customPlugin] }));
Debug
Known issues
breakingThe React plugin for `pretty-format` no longer renders empty string children (`''`) in React elements. If your snapshots previously captured these empty strings, they will now be omitted, leading to snapshot mismatches.
fix
Update existing snapshots (`jest --updateSnapshot`) after upgrading to Jest 30 or `pretty-format` v30 to reflect the new rendering behavior. Review the changes to ensure they are intended.
affects: >=30.0.0
breakingThe formatting of `ArrayBuffer` and `DataView` objects has been updated to be more correct and human-readable in version 30. This change will likely cause snapshot mismatches if your tests include these types.
fix
Run Jest with `--updateSnapshot` to accept the new, corrected formatting for `ArrayBuffer` and `DataView` instances in your snapshots. Verify the updated output is as expected.
affects: >=30.0.0
breakingAs part of the Jest 30 upgrade, `pretty-format` introduces general improvements to object printing. While specific changes beyond React empty strings and ArrayBuffer/DataView are not individually listed as 'breaking' for `pretty-format`, the overall improvements may alter snapshot output for various JavaScript values.
fix
It is highly recommended to run `jest --updateSnapshot` after upgrading to Jest 30 to re-record any snapshots affected by these underlying formatting improvements. Always review snapshot diffs carefully.
affects: >=30.0.0
gotchaWhen developing custom plugins, ensure that the `test` method is highly performant as it is called frequently by `pretty-format` for all values. Inefficient `test` implementations can significantly degrade performance.
fix
Keep `test` methods simple, fast, and avoid expensive operations. Perform heavy logic only within the `serialize` or `print` methods once `test` has confirmed the plugin should handle the value.
affects: >=1.0.0
Errors
Common errors & fixes
Snapshot mismatch
Upgrading `pretty-format` (especially with Jest 30) or its dependencies can alter the serialized output of various JavaScript values, leading to failing snapshot tests.
fix
Review the snapshot differences and, if the changes are expected and correct, update your snapshots by running `jest --updateSnapshot`.
TypeError: (0 , pretty_format__WEBPACK_IMPORTED_MODULE_0__.format) is not a function
This error typically occurs in a mixed CommonJS/ESM environment or when a bundler incorrectly handles named exports, treating `format` as a default export, or when trying to destructure a CommonJS `require` directly with an ESM import syntax.
fix
Ensure you are using the correct import syntax for your module system. For ESM, use `import { format } from 'pretty-format';`. For CommonJS, use `const { format } = require('pretty-format');`. Verify your bundler/TypeScript configuration is set up to correctly handle module interop.
Custom plugin not applied or applied incorrectly.
The `test` method of a custom plugin might be returning `false` incorrectly, or the `serialize` method has a bug, or the plugin is not passed in the `options.plugins` array to `format`.
fix
Debug the `test` method to ensure it correctly identifies the target value. Check the `serialize` method for logic errors. Confirm the plugin is included in the `plugins` array passed to the `format` function. Ensure the `serialize` method uses `printer` for child values and `indenter` for new lines as expected.
Upgrade
Version history
30.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
pretty-format — npm install pretty-format · libregistry