@vitest/utils 4.1.5 → 4.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/diff.d.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import { PrettyFormatOptions } from '@vitest/pretty-format';
1
2
  import { D as DiffOptions } from './types.d-BCElaP-c.js';
2
3
  export { a as DiffOptionsColor, S as SerializedDiffOptions } from './types.d-BCElaP-c.js';
3
- import '@vitest/pretty-format';
4
4
 
5
5
  /**
6
6
  * Diff Match and Patch
@@ -75,14 +75,29 @@ declare function diffLinesRaw(aLines: Array<string>, bLines: Array<string>, opti
75
75
  declare function diffStringsUnified(a: string, b: string, options?: DiffOptions): string;
76
76
  declare function diffStringsRaw(a: string, b: string, cleanup: boolean, options?: DiffOptions): [Array<Diff>, boolean];
77
77
 
78
+ /**
79
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
80
+ *
81
+ * This source code is licensed under the MIT license found in the
82
+ * LICENSE file in the root directory of this source tree.
83
+ */
84
+
85
+ interface StringifiedMemory {
86
+ expected?: string;
87
+ actual?: string;
88
+ }
89
+ interface Memorize {
90
+ (pointer: "expected" | "actual", stringifiedValue: string): string;
91
+ }
78
92
  /**
79
93
  * @param a Expected value
80
94
  * @param b Received value
81
95
  * @param options Diff options
82
96
  * @returns {string | null} a string diff
83
97
  */
84
- declare function diff(a: any, b: any, options?: DiffOptions): string | undefined;
85
- declare function printDiffOrStringify(received: unknown, expected: unknown, options?: DiffOptions): string | undefined;
98
+ declare function diff(a: any, b: any, options?: DiffOptions, memorize?: Memorize): string | undefined;
99
+ declare function getDefaultFormatOptions(options?: DiffOptions): PrettyFormatOptions;
100
+ declare function printDiffOrStringify(received: unknown, expected: unknown, options?: DiffOptions, memory?: StringifiedMemory): string | undefined;
86
101
  declare function replaceAsymmetricMatcher(actual: any, expected: any, actualReplaced?: WeakSet<WeakKey>, expectedReplaced?: WeakSet<WeakKey>): {
87
102
  replacedActual: any;
88
103
  replacedExpected: any;
@@ -90,4 +105,5 @@ declare function replaceAsymmetricMatcher(actual: any, expected: any, actualRepl
90
105
  type PrintLabel = (string: string) => string;
91
106
  declare function getLabelPrinter(...strings: Array<string>): PrintLabel;
92
107
 
93
- export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, DiffOptions, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified, getLabelPrinter, printDiffOrStringify, replaceAsymmetricMatcher };
108
+ export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, DiffOptions, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified, getDefaultFormatOptions, getLabelPrinter, printDiffOrStringify, replaceAsymmetricMatcher };
109
+ export type { StringifiedMemory };
package/dist/diff.js CHANGED
@@ -1968,6 +1968,7 @@ const FALLBACK_FORMAT_OPTIONS = {
1968
1968
  maxDepth: 8,
1969
1969
  plugins: PLUGINS
1970
1970
  };
1971
+ const DEFAULT_MEMORIZE = (_, v) => v;
1971
1972
  // Generate a string that will highlight the difference between two values
1972
1973
  // with green and red. (similar to how github does code diffing)
1973
1974
  /**
@@ -1976,7 +1977,7 @@ const FALLBACK_FORMAT_OPTIONS = {
1976
1977
  * @param options Diff options
1977
1978
  * @returns {string | null} a string diff
1978
1979
  */
1979
- function diff(a, b, options) {
1980
+ function diff(a, b, options, memorize = DEFAULT_MEMORIZE) {
1980
1981
  if (Object.is(a, b)) {
1981
1982
  return "";
1982
1983
  }
@@ -2010,8 +2011,8 @@ function diff(a, b, options) {
2010
2011
  function truncate(s) {
2011
2012
  return s.length <= MAX_LENGTH ? s : `${s.slice(0, MAX_LENGTH)}...`;
2012
2013
  }
2013
- aDisplay = truncate(aDisplay);
2014
- bDisplay = truncate(bDisplay);
2014
+ aDisplay = memorize("expected", truncate(aDisplay));
2015
+ bDisplay = memorize("actual", truncate(bDisplay));
2015
2016
  const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)}\n${aDisplay}`;
2016
2017
  const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)}\n${bDisplay}`;
2017
2018
  return `${aDiff}\n\n${bDiff}`;
@@ -2022,15 +2023,21 @@ function diff(a, b, options) {
2022
2023
  switch (aType) {
2023
2024
  case "string": return diffLinesUnified(a.split("\n"), b.split("\n"), options);
2024
2025
  case "boolean":
2025
- case "number": return comparePrimitive(a, b, options);
2026
- case "map": return compareObjects(sortMap(a), sortMap(b), options);
2027
- case "set": return compareObjects(sortSet(a), sortSet(b), options);
2028
- default: return compareObjects(a, b, options);
2026
+ case "number": return comparePrimitive(a, b, options, memorize);
2027
+ case "map": return compareObjects(sortMap(a), sortMap(b), options, memorize);
2028
+ case "set": return compareObjects(sortSet(a), sortSet(b), options, memorize);
2029
+ default: return compareObjects(a, b, options, memorize);
2029
2030
  }
2030
2031
  }
2031
- function comparePrimitive(a, b, options) {
2032
- const aFormat = format(a, FORMAT_OPTIONS);
2033
- const bFormat = format(b, FORMAT_OPTIONS);
2032
+ function createMemorize(memory) {
2033
+ return (pointer, stringifiedValue) => {
2034
+ memory[pointer] = stringifiedValue;
2035
+ return stringifiedValue;
2036
+ };
2037
+ }
2038
+ function comparePrimitive(a, b, options, memorize = DEFAULT_MEMORIZE) {
2039
+ const aFormat = memorize("expected", format(a, FORMAT_OPTIONS));
2040
+ const bFormat = memorize("actual", format(b, FORMAT_OPTIONS));
2034
2041
  return aFormat === bFormat ? "" : diffLinesUnified(aFormat.split("\n"), bFormat.split("\n"), options);
2035
2042
  }
2036
2043
  function sortMap(map) {
@@ -2039,12 +2046,12 @@ function sortMap(map) {
2039
2046
  function sortSet(set) {
2040
2047
  return new Set(Array.from(set.values()).sort());
2041
2048
  }
2042
- function compareObjects(a, b, options) {
2049
+ function compareObjects(a, b, options, memorize = DEFAULT_MEMORIZE) {
2043
2050
  let difference;
2044
2051
  let hasThrown = false;
2045
2052
  try {
2046
2053
  const formatOptions = getFormatOptions(FORMAT_OPTIONS, options);
2047
- difference = getObjectsDifference(a, b, formatOptions, options);
2054
+ difference = getObjectsDifference(a, b, formatOptions, options, memorize);
2048
2055
  } catch {
2049
2056
  hasThrown = true;
2050
2057
  }
@@ -2053,13 +2060,16 @@ function compareObjects(a, b, options) {
2053
2060
  // without calling `toJSON`. It's also possible that toJSON might throw.
2054
2061
  if (difference === undefined || difference === noDiffMessage) {
2055
2062
  const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
2056
- difference = getObjectsDifference(a, b, formatOptions, options);
2063
+ difference = getObjectsDifference(a, b, formatOptions, options, memorize);
2057
2064
  if (difference !== noDiffMessage && !hasThrown) {
2058
2065
  difference = `${getCommonMessage(SIMILAR_MESSAGE, options)}\n\n${difference}`;
2059
2066
  }
2060
2067
  }
2061
2068
  return difference;
2062
2069
  }
2070
+ function getDefaultFormatOptions(options) {
2071
+ return getFormatOptions(FORMAT_OPTIONS, options);
2072
+ }
2063
2073
  function getFormatOptions(formatOptions, options) {
2064
2074
  const { compareKeys, printBasicPrototype, maxDepth } = normalizeDiffOptions(options);
2065
2075
  return {
@@ -2069,7 +2079,7 @@ function getFormatOptions(formatOptions, options) {
2069
2079
  maxDepth: maxDepth ?? formatOptions.maxDepth
2070
2080
  };
2071
2081
  }
2072
- function getObjectsDifference(a, b, formatOptions, options) {
2082
+ function getObjectsDifference(a, b, formatOptions, options, memorize = DEFAULT_MEMORIZE) {
2073
2083
  const formatOptionsZeroIndent = {
2074
2084
  ...formatOptions,
2075
2085
  indent: 0
@@ -2079,8 +2089,8 @@ function getObjectsDifference(a, b, formatOptions, options) {
2079
2089
  if (aCompare === bCompare) {
2080
2090
  return getCommonMessage(NO_DIFF_MESSAGE, options);
2081
2091
  } else {
2082
- const aDisplay = format(a, formatOptions);
2083
- const bDisplay = format(b, formatOptions);
2092
+ const aDisplay = memorize("expected", format(a, formatOptions));
2093
+ const bDisplay = memorize("actual", format(b, formatOptions));
2084
2094
  return diffLinesUnified2(aDisplay.split("\n"), bDisplay.split("\n"), aCompare.split("\n"), bCompare.split("\n"), options);
2085
2095
  }
2086
2096
  }
@@ -2094,7 +2104,7 @@ function isReplaceable(obj1, obj2) {
2094
2104
  const obj2Type = getType$1(obj2);
2095
2105
  return obj1Type === obj2Type && (obj1Type === "Object" || obj1Type === "Array");
2096
2106
  }
2097
- function printDiffOrStringify(received, expected, options) {
2107
+ function printDiffOrStringify(received, expected, options, memory) {
2098
2108
  const { aAnnotation, bAnnotation } = normalizeDiffOptions(options);
2099
2109
  if (typeof expected === "string" && typeof received === "string" && expected.length > 0 && received.length > 0 && expected.length <= MAX_DIFF_STRING_LENGTH && received.length <= MAX_DIFF_STRING_LENGTH && expected !== received) {
2100
2110
  if (expected.includes("\n") || received.includes("\n")) {
@@ -2111,7 +2121,8 @@ function printDiffOrStringify(received, expected, options) {
2111
2121
  const clonedExpected = deepClone(expected, { forceWritable: true });
2112
2122
  const clonedReceived = deepClone(received, { forceWritable: true });
2113
2123
  const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedReceived, clonedExpected);
2114
- const difference = diff(replacedExpected, replacedActual, options);
2124
+ const memorize = memory ? createMemorize(memory) : DEFAULT_MEMORIZE;
2125
+ const difference = diff(replacedExpected, replacedActual, options, memorize);
2115
2126
  return difference;
2116
2127
  // }
2117
2128
  // const printLabel = getLabelPrinter(aAnnotation, bAnnotation)
@@ -2198,4 +2209,4 @@ function getCommonAndChangedSubstrings(diffs, op, hasCommonDiff) {
2198
2209
  return diffs.reduce((reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op ? hasCommonDiff ? c.inverse(diff[1]) : diff[1] : ""), "");
2199
2210
  }
2200
2211
 
2201
- export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified, getLabelPrinter, printDiffOrStringify, replaceAsymmetricMatcher };
2212
+ export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified, getDefaultFormatOptions, getLabelPrinter, printDiffOrStringify, replaceAsymmetricMatcher };
package/dist/error.js CHANGED
@@ -1,8 +1,8 @@
1
- import { printDiffOrStringify } from './diff.js';
2
- import { stringify } from './display.js';
1
+ import { format } from '@vitest/pretty-format';
2
+ import { printDiffOrStringify, getDefaultFormatOptions } from './diff.js';
3
3
  import { serializeValue } from './serialize.js';
4
- import '@vitest/pretty-format';
5
4
  import 'tinyrainbow';
5
+ import './display.js';
6
6
  import './helpers.js';
7
7
  import './constants.js';
8
8
 
@@ -12,16 +12,13 @@ function processError(_err, diffOptions, seen = new WeakSet()) {
12
12
  }
13
13
  const err = _err;
14
14
  if (err.showDiff || err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined) {
15
- err.diff = printDiffOrStringify(err.actual, err.expected, {
15
+ const options = {
16
16
  ...diffOptions,
17
17
  ...err.diffOptions
18
- });
19
- }
20
- if ("expected" in err && typeof err.expected !== "string") {
21
- err.expected = stringify(err.expected, 10);
22
- }
23
- if ("actual" in err && typeof err.actual !== "string") {
24
- err.actual = stringify(err.actual, 10);
18
+ };
19
+ err.diff = printDiffOrStringify(err.actual, err.expected, options, err);
20
+ err.expected = prettifyValue(err.expected, options);
21
+ err.actual = prettifyValue(err.actual, options);
25
22
  }
26
23
  // some Error implementations may not allow rewriting cause
27
24
  // in most cases, the assignment will lead to "err.cause = err.cause"
@@ -37,5 +34,11 @@ function processError(_err, diffOptions, seen = new WeakSet()) {
37
34
  return serializeValue(new Error(`Failed to fully serialize error: ${e?.message}\nInner error message: ${err?.message}`));
38
35
  }
39
36
  }
37
+ function prettifyValue(value, options) {
38
+ if (typeof value !== "string") {
39
+ return format(value, getDefaultFormatOptions(options));
40
+ }
41
+ return value;
42
+ }
40
43
 
41
44
  export { processError, serializeValue as serializeError };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/utils",
3
3
  "type": "module",
4
- "version": "4.1.5",
4
+ "version": "4.1.6",
5
5
  "description": "Shared Vitest utility functions",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -86,7 +86,7 @@
86
86
  "dependencies": {
87
87
  "convert-source-map": "^2.0.0",
88
88
  "tinyrainbow": "^3.1.0",
89
- "@vitest/pretty-format": "4.1.5"
89
+ "@vitest/pretty-format": "4.1.6"
90
90
  },
91
91
  "devDependencies": {
92
92
  "@jridgewell/trace-mapping": "0.3.31",