(
object: unknown,
maxDepth = 10,
{ maxLength, filterNode, ...options }: StringifyOptions = {},
)
| 47 | } |
| 48 | |
| 49 | export function stringify( |
| 50 | object: unknown, |
| 51 | maxDepth = 10, |
| 52 | { maxLength, filterNode, ...options }: StringifyOptions = {}, |
| 53 | ): string { |
| 54 | const MAX_LENGTH = maxLength ?? 10000 |
| 55 | let result |
| 56 | |
| 57 | // Convert string selector to filter function |
| 58 | const filterFn = typeof filterNode === 'string' |
| 59 | ? createNodeFilterFromSelector(filterNode) |
| 60 | : filterNode |
| 61 | |
| 62 | const plugins = filterFn |
| 63 | ? [ |
| 64 | ReactTestComponent, |
| 65 | ReactElement, |
| 66 | createDOMElementFilter(filterFn), |
| 67 | DOMCollection, |
| 68 | Immutable, |
| 69 | AsymmetricMatcher, |
| 70 | ] |
| 71 | : PLUGINS |
| 72 | |
| 73 | try { |
| 74 | result = prettyFormat(object, { |
| 75 | maxDepth, |
| 76 | escapeString: false, |
| 77 | // min: true, |
| 78 | plugins, |
| 79 | ...options, |
| 80 | }) |
| 81 | } |
| 82 | catch { |
| 83 | result = prettyFormat(object, { |
| 84 | callToJSON: false, |
| 85 | maxDepth, |
| 86 | escapeString: false, |
| 87 | // min: true, |
| 88 | plugins, |
| 89 | ...options, |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | // Prevents infinite loop https://github.com/vitest-dev/vitest/issues/7249 |
| 94 | return result.length >= MAX_LENGTH && maxDepth > 1 |
| 95 | ? stringify(object, Math.floor(Math.min(maxDepth, Number.MAX_SAFE_INTEGER) / 2), { maxLength, filterNode, ...options }) |
| 96 | : result |
| 97 | } |
| 98 | |
| 99 | function createNodeFilterFromSelector(selector: string): (node: any) => boolean { |
| 100 | const ELEMENT_NODE = 1 |
no test coverage detected