This commit is contained in:
2026-08-14 16:03:47 +01:00
parent fd53e3f4fb
commit dfdc3b2840
2463 changed files with 186456 additions and 111315 deletions
+7
View File
@@ -0,0 +1,7 @@
# @vitest/pretty-format
[![NPM version](https://img.shields.io/npm/v/@vitest/pretty-format?color=a1b858&label=)](https://npmx.dev/package/@vitest/pretty-format)
Vitest's fork of Jest's [`pretty-format`](https://npmx.dev/package/pretty-format).
[GitHub](https://github.com/vitest-dev/vitest/tree/main/packages/pretty-format) | [Documentation](https://github.com/vitest-dev/vitest/blob/main/packages/pretty-format/USAGE.md)
+72 -1
View File
@@ -46,18 +46,78 @@ interface Options extends Omit<RequiredOptions, "compareKeys" | "theme"> {
theme: Theme;
}
interface PrettyFormatOptions {
/**
* Call `toJSON` on objects before formatting them.
* Ignored after the formatter has already called `toJSON` once for a value.
* @default true
*/
callToJSON?: boolean;
/**
* Whether to escape special characters in regular expressions.
* @default false
*/
escapeRegex?: boolean;
/**
* Whether to escape special characters in strings.
* @default true
*/
escapeString?: boolean;
/**
* Whether to highlight syntax using terminal colors.
* @default false
*/
highlight?: boolean;
/**
* Number of spaces to use for each level of indentation.
* @default 2
*/
indent?: number;
/**
* Maximum depth to recurse into nested values.
* @default Infinity
*/
maxDepth?: number;
/**
* Maximum number of items to print in arrays, sets, maps, and similar collections.
* @default Infinity
*/
maxWidth?: number;
/**
* Approximate per-depth-level budget for output length.
* When the accumulated output at any single depth level exceeds this value,
* further nesting is collapsed. This is a heuristic safety valve, not a hard
* limit — total output can reach up to roughly `maxDepth × maxOutputLength`.
* @default 1_000_000
*/
maxOutputLength?: number;
/**
* Whether to minimize added whitespace, including indentation and line breaks.
* @default false
*/
min?: boolean;
/**
* Whether to print `Object` / `Array` prefixes for plain objects and arrays.
* @default true
*/
printBasicPrototype?: boolean;
/**
* Whether to include the function name when formatting functions.
* @default true
*/
printFunctionName?: boolean;
/**
* Whether to include shadow-root contents when formatting DOM nodes.
* @default true
*/
printShadowRoot?: boolean;
/**
* Compare function used when sorting object keys. Set to `null` to disable sorting.
*/
compareKeys?: CompareKeys;
/**
* Plugins used to serialize application-specific data types.
* @default []
*/
plugins?: Plugins;
}
type OptionsReceived = PrettyFormatOptions;
@@ -77,6 +137,7 @@ interface Config {
printShadowRoot: boolean;
spacingInner: string;
spacingOuter: string;
maxOutputLength: number;
}
type Printer = (val: unknown, config: Config, indentation: string, depth: number, refs: Refs, hasCalledToJSON?: boolean) => string;
type Test = (arg0: any) => boolean;
@@ -103,6 +164,15 @@ type Plugins = Array<Plugin>;
* LICENSE file in the root directory of this source tree.
*/
declare function createDOMElementFilter(filterNode?: (node: any) => boolean): NewPlugin;
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
declare const DEFAULT_OPTIONS: Options;
/**
* Returns a presentation string of your `val` object
@@ -110,6 +180,7 @@ declare const DEFAULT_OPTIONS: Options;
* @param options Custom settings
*/
declare function format(val: unknown, options?: OptionsReceived): string;
declare const plugins: {
AsymmetricMatcher: NewPlugin;
DOMCollection: NewPlugin;
@@ -120,5 +191,5 @@ declare const plugins: {
Error: NewPlugin;
};
export { DEFAULT_OPTIONS, format, plugins };
export { DEFAULT_OPTIONS, createDOMElementFilter, format, plugins };
export type { Colors, CompareKeys, Config, NewPlugin, OldPlugin, Options, OptionsReceived, Plugin, Plugins, PrettyFormatOptions, Printer, Refs, Theme };
+57 -10
View File
@@ -295,7 +295,25 @@ function nodeIsComment(node) {
function nodeIsFragment(node) {
return node.nodeType === FRAGMENT_NODE;
}
const serialize$3 = (node, config, indentation, depth, refs, printer) => {
function filterChildren(children, filterNode) {
// Filter out text nodes that only contain whitespace to prevent empty lines
// This is done regardless of whether a filterNode is provided
let filtered = children.filter((node) => {
// Filter out text nodes that are only whitespace
if (node.nodeType === TEXT_NODE) {
const text = node.data || "";
// Keep text nodes that have non-whitespace content
return text.trim().length > 0;
}
return true;
});
// Apply additional user-provided filter if specified
if (filterNode) {
filtered = filtered.filter(filterNode);
}
return filtered;
}
function serializeDOM(node, config, indentation, depth, refs, printer, filterNode) {
if (nodeIsText(node)) {
return printText(node.data, config);
}
@@ -306,11 +324,22 @@ const serialize$3 = (node, config, indentation, depth, refs, printer) => {
if (++depth > config.maxDepth) {
return printElementAsLeaf(type, config);
}
const children = Array.prototype.slice.call(node.childNodes || node.children);
const shadowChildren = nodeIsFragment(node) || !node.shadowRoot ? [] : Array.prototype.slice.call(node.shadowRoot.children);
const resolvedChildren = filterNode ? filterChildren(children, filterNode) : children;
const resolvedShadowChildren = filterNode ? filterChildren(shadowChildren, filterNode) : shadowChildren;
return printElement(type, printProps(nodeIsFragment(node) ? [] : Array.from(node.attributes, (attr) => attr.name).sort(), nodeIsFragment(node) ? {} : [...node.attributes].reduce((props, attribute) => {
props[attribute.name] = attribute.value;
return props;
}, {}), config, indentation + config.indent, depth, refs, printer), (nodeIsFragment(node) || !node.shadowRoot ? "" : printShadowRoot(Array.prototype.slice.call(node.shadowRoot.children), config, indentation + config.indent, depth, refs, printer)) + printChildren(Array.prototype.slice.call(node.childNodes || node.children), config, indentation + config.indent, depth, refs, printer), config, indentation);
};
}, {}), config, indentation + config.indent, depth, refs, printer), (resolvedShadowChildren.length > 0 ? printShadowRoot(resolvedShadowChildren, config, indentation + config.indent, depth, refs, printer) : "") + printChildren(resolvedChildren, config, indentation + config.indent, depth, refs, printer), config, indentation);
}
const serialize$3 = (node, config, indentation, depth, refs, printer) => serializeDOM(node, config, indentation, depth, refs, printer);
function createDOMElementFilter(filterNode) {
return {
test: test$3,
serialize: (node, config, indentation, depth, refs, printer) => serializeDOM(node, config, indentation, depth, refs, printer, filterNode)
};
}
const plugin$3 = {
serialize: serialize$3,
test: test$3
@@ -892,15 +921,30 @@ function findPlugin(plugins, val) {
return null;
}
function printer(val, config, indentation, depth, refs, hasCalledToJSON) {
let result;
const plugin = findPlugin(config.plugins, val);
if (plugin !== null) {
return printPlugin(plugin, val, config, indentation, depth, refs);
result = printPlugin(plugin, val, config, indentation, depth, refs);
} else {
const basicResult = printBasicValue(val, config.printFunctionName, config.escapeRegex, config.escapeString);
if (basicResult !== null) {
result = basicResult;
} else {
result = printComplexValue(val, config, indentation, depth, refs, hasCalledToJSON);
}
}
const basicResult = printBasicValue(val, config.printFunctionName, config.escapeRegex, config.escapeString);
if (basicResult !== null) {
return basicResult;
// Per-depth output budget (inspired by Node's util.inspect).
// Each depth level tracks output independently, so nested results
// don't inflate a single counter (which would undercount by ~Nx for
// N levels of nesting). Nodes at the same depth produce disjoint spans
// in the output string, so each bucket accurately reflects output at
// that level. Total output is bounded by maxDepth × maxOutputLength.
config._outputLengthPerDepth[depth] ??= 0;
config._outputLengthPerDepth[depth] += result.length;
if (config._outputLengthPerDepth[depth] > config.maxOutputLength) {
config.maxDepth = 0;
}
return printComplexValue(val, config, indentation, depth, refs, hasCalledToJSON);
return result;
}
const DEFAULT_THEME = {
comment: "gray",
@@ -918,6 +962,7 @@ const DEFAULT_OPTIONS = {
highlight: false,
indent: 2,
maxDepth: Number.POSITIVE_INFINITY,
maxOutputLength: 1e6,
maxWidth: Number.POSITIVE_INFINITY,
min: false,
plugins: [],
@@ -982,7 +1027,9 @@ function getConfig(options) {
printFunctionName: getPrintFunctionName(options),
printShadowRoot: options?.printShadowRoot ?? true,
spacingInner: options?.min ? " " : "\n",
spacingOuter: options?.min ? "" : "\n"
spacingOuter: options?.min ? "" : "\n",
maxOutputLength: options?.maxOutputLength ?? DEFAULT_OPTIONS.maxOutputLength,
_outputLengthPerDepth: []
};
}
function createIndent(indent) {
@@ -1019,4 +1066,4 @@ const plugins = {
Error: ErrorPlugin
};
export { DEFAULT_OPTIONS, format, plugins };
export { DEFAULT_OPTIONS, createDOMElementFilter, format, plugins };
+10 -4
View File
@@ -1,11 +1,11 @@
{
"name": "@vitest/pretty-format",
"type": "module",
"version": "4.0.18",
"version": "4.1.9",
"description": "Fork of pretty-format with support for ESM",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
"homepage": "https://github.com/vitest-dev/vitest/tree/main/packages/utils#readme",
"homepage": "https://github.com/vitest-dev/vitest/tree/main/packages/pretty-format",
"repository": {
"type": "git",
"url": "git+https://github.com/vitest-dev/vitest.git",
@@ -14,6 +14,12 @@
"bugs": {
"url": "https://github.com/vitest-dev/vitest/issues"
},
"keywords": [
"vitest",
"test",
"pretty",
"pretty-format"
],
"sideEffects": false,
"exports": {
".": {
@@ -30,11 +36,11 @@
"dist"
],
"dependencies": {
"tinyrainbow": "^3.0.3"
"tinyrainbow": "^3.1.0"
},
"devDependencies": {
"@types/react-is": "^19.2.0",
"react-is": "^19.2.0",
"react-is": "^19.2.4",
"react-is-18": "npm:react-is@18.3.1"
},
"scripts": {