@vitest/utils 1.3.1 → 1.5.0

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.
@@ -127,7 +127,7 @@ function objDisplay(obj, options = {}) {
127
127
  if (options.truncate && str.length >= options.truncate) {
128
128
  if (type === "[object Function]") {
129
129
  const fn = obj;
130
- return !fn.name || fn.name === "" ? "[Function]" : `[Function: ${fn.name}]`;
130
+ return !fn.name ? "[Function]" : `[Function: ${fn.name}]`;
131
131
  } else if (type === "[object Array]") {
132
132
  return `[ Array(${obj.length}) ]`;
133
133
  } else if (type === "[object Object]") {
package/dist/diff.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { D as DiffOptions } from './types-widbdqe5.js';
2
- export { a as DiffOptionsColor } from './types-widbdqe5.js';
1
+ import { D as DiffOptions } from './types-9l4niLY8.js';
2
+ export { a as DiffOptionsColor } from './types-9l4niLY8.js';
3
3
  import 'pretty-format';
4
4
 
5
5
  /**
@@ -63,7 +63,7 @@ declare class Diff {
63
63
 
64
64
  declare function diffLinesUnified(aLines: Array<string>, bLines: Array<string>, options?: DiffOptions): string;
65
65
  declare function diffLinesUnified2(aLinesDisplay: Array<string>, bLinesDisplay: Array<string>, aLinesCompare: Array<string>, bLinesCompare: Array<string>, options?: DiffOptions): string;
66
- declare function diffLinesRaw(aLines: Array<string>, bLines: Array<string>): Array<Diff>;
66
+ declare function diffLinesRaw(aLines: Array<string>, bLines: Array<string>, options?: DiffOptions): [Array<Diff>, boolean];
67
67
 
68
68
  /**
69
69
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -73,7 +73,7 @@ declare function diffLinesRaw(aLines: Array<string>, bLines: Array<string>): Arr
73
73
  */
74
74
 
75
75
  declare function diffStringsUnified(a: string, b: string, options?: DiffOptions): string;
76
- declare function diffStringsRaw(a: string, b: string, cleanup: boolean): Array<Diff>;
76
+ declare function diffStringsRaw(a: string, b: string, cleanup: boolean, options?: DiffOptions): [Array<Diff>, boolean];
77
77
 
78
78
  /**
79
79
  * Copyright (c) Meta Platforms, Inc. and affiliates.
package/dist/diff.js CHANGED
@@ -555,6 +555,7 @@ function joinAlignedDiffsExpand(diffs, options) {
555
555
 
556
556
  const noColor = (string) => string;
557
557
  const DIFF_CONTEXT_DEFAULT = 5;
558
+ const DIFF_TRUNCATE_THRESHOLD_DEFAULT = 0;
558
559
  function getDefaultOptions() {
559
560
  const c = getColors();
560
561
  return {
@@ -575,7 +576,10 @@ function getDefaultOptions() {
575
576
  expand: true,
576
577
  includeChangeCounts: false,
577
578
  omitAnnotationLines: false,
578
- patchColor: c.yellow
579
+ patchColor: c.yellow,
580
+ truncateThreshold: DIFF_TRUNCATE_THRESHOLD_DEFAULT,
581
+ truncateAnnotation: "... Diff result is truncated",
582
+ truncateAnnotationColor: noColor
579
583
  };
580
584
  }
581
585
  function getCompareKeys(compareKeys) {
@@ -644,16 +648,21 @@ ${bColor(b)}
644
648
 
645
649
  `;
646
650
  }
647
- function printDiffLines(diffs, options) {
648
- return printAnnotation(options, countChanges(diffs)) + (options.expand ? joinAlignedDiffsExpand(diffs, options) : joinAlignedDiffsNoExpand(diffs, options));
651
+ function printDiffLines(diffs, truncated, options) {
652
+ return printAnnotation(options, countChanges(diffs)) + (options.expand ? joinAlignedDiffsExpand(diffs, options) : joinAlignedDiffsNoExpand(diffs, options)) + (truncated ? options.truncateAnnotationColor(`
653
+ ${options.truncateAnnotation}`) : "");
649
654
  }
650
655
  function diffLinesUnified(aLines, bLines, options) {
656
+ const normalizedOptions = normalizeDiffOptions(options);
657
+ const [diffs, truncated] = diffLinesRaw(
658
+ isEmptyString(aLines) ? [] : aLines,
659
+ isEmptyString(bLines) ? [] : bLines,
660
+ normalizedOptions
661
+ );
651
662
  return printDiffLines(
652
- diffLinesRaw(
653
- isEmptyString(aLines) ? [] : aLines,
654
- isEmptyString(bLines) ? [] : bLines
655
- ),
656
- normalizeDiffOptions(options)
663
+ diffs,
664
+ truncated,
665
+ normalizedOptions
657
666
  );
658
667
  }
659
668
  function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options) {
@@ -668,7 +677,7 @@ function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCo
668
677
  if (aLinesDisplay.length !== aLinesCompare.length || bLinesDisplay.length !== bLinesCompare.length) {
669
678
  return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
670
679
  }
671
- const diffs = diffLinesRaw(aLinesCompare, bLinesCompare);
680
+ const [diffs, truncated] = diffLinesRaw(aLinesCompare, bLinesCompare, options);
672
681
  let aIndex = 0;
673
682
  let bIndex = 0;
674
683
  diffs.forEach((diff2) => {
@@ -687,11 +696,14 @@ function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCo
687
696
  bIndex += 1;
688
697
  }
689
698
  });
690
- return printDiffLines(diffs, normalizeDiffOptions(options));
699
+ return printDiffLines(diffs, truncated, normalizeDiffOptions(options));
691
700
  }
692
- function diffLinesRaw(aLines, bLines) {
693
- const aLength = aLines.length;
694
- const bLength = bLines.length;
701
+ function diffLinesRaw(aLines, bLines, options) {
702
+ const truncate = (options == null ? void 0 : options.truncateThreshold) ?? false;
703
+ const truncateThreshold = Math.max(Math.floor((options == null ? void 0 : options.truncateThreshold) ?? 0), 0);
704
+ const aLength = truncate ? Math.min(aLines.length, truncateThreshold) : aLines.length;
705
+ const bLength = truncate ? Math.min(bLines.length, truncateThreshold) : bLines.length;
706
+ const truncated = aLength !== aLines.length || bLength !== bLines.length;
695
707
  const isCommon = (aIndex2, bIndex2) => aLines[aIndex2] === bLines[bIndex2];
696
708
  const diffs = [];
697
709
  let aIndex = 0;
@@ -710,10 +722,30 @@ function diffLinesRaw(aLines, bLines) {
710
722
  diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
711
723
  for (; bIndex !== bLength; bIndex += 1)
712
724
  diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
713
- return diffs;
725
+ return [diffs, truncated];
714
726
  }
715
727
 
716
- function diffStrings(a, b) {
728
+ function getNewLineSymbol(string) {
729
+ return string.includes("\r\n") ? "\r\n" : "\n";
730
+ }
731
+ function diffStrings(a, b, options) {
732
+ const truncate = (options == null ? void 0 : options.truncateThreshold) ?? false;
733
+ const truncateThreshold = Math.max(Math.floor((options == null ? void 0 : options.truncateThreshold) ?? 0), 0);
734
+ let aLength = a.length;
735
+ let bLength = b.length;
736
+ if (truncate) {
737
+ const aMultipleLines = a.includes("\n");
738
+ const bMultipleLines = b.includes("\n");
739
+ const aNewLineSymbol = getNewLineSymbol(a);
740
+ const bNewLineSymbol = getNewLineSymbol(b);
741
+ const _a = aMultipleLines ? `${a.split(aNewLineSymbol, truncateThreshold).join(aNewLineSymbol)}
742
+ ` : a;
743
+ const _b = bMultipleLines ? `${b.split(bNewLineSymbol, truncateThreshold).join(bNewLineSymbol)}
744
+ ` : b;
745
+ aLength = _a.length;
746
+ bLength = _b.length;
747
+ }
748
+ const truncated = aLength !== a.length || bLength !== b.length;
717
749
  const isCommon = (aIndex2, bIndex2) => a[aIndex2] === b[bIndex2];
718
750
  let aIndex = 0;
719
751
  let bIndex = 0;
@@ -728,12 +760,12 @@ function diffStrings(a, b) {
728
760
  diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
729
761
  };
730
762
  const diffSequences = diff$1.default.default || diff$1.default;
731
- diffSequences(a.length, b.length, isCommon, foundSubsequence);
732
- if (aIndex !== a.length)
763
+ diffSequences(aLength, bLength, isCommon, foundSubsequence);
764
+ if (aIndex !== aLength)
733
765
  diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
734
- if (bIndex !== b.length)
766
+ if (bIndex !== bLength)
735
767
  diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
736
- return diffs;
768
+ return [diffs, truncated];
737
769
  }
738
770
 
739
771
  function concatenateRelevantDiffs(op, diffs, changeColor) {
@@ -888,27 +920,28 @@ function hasCommonDiff(diffs, isMultiline) {
888
920
  function diffStringsUnified(a, b, options) {
889
921
  if (a !== b && a.length !== 0 && b.length !== 0) {
890
922
  const isMultiline = a.includes("\n") || b.includes("\n");
891
- const diffs = diffStringsRaw(
923
+ const [diffs, truncated] = diffStringsRaw(
892
924
  isMultiline ? `${a}
893
925
  ` : a,
894
926
  isMultiline ? `${b}
895
927
  ` : b,
896
- true
928
+ true,
897
929
  // cleanupSemantic
930
+ options
898
931
  );
899
932
  if (hasCommonDiff(diffs, isMultiline)) {
900
933
  const optionsNormalized = normalizeDiffOptions(options);
901
934
  const lines = getAlignedDiffs(diffs, optionsNormalized.changeColor);
902
- return printDiffLines(lines, optionsNormalized);
935
+ return printDiffLines(lines, truncated, optionsNormalized);
903
936
  }
904
937
  }
905
938
  return diffLinesUnified(a.split("\n"), b.split("\n"), options);
906
939
  }
907
- function diffStringsRaw(a, b, cleanup) {
908
- const diffs = diffStrings(a, b);
940
+ function diffStringsRaw(a, b, cleanup, options) {
941
+ const [diffs, truncated] = diffStrings(a, b, options);
909
942
  if (cleanup)
910
943
  diff_cleanupSemantic(diffs);
911
- return diffs;
944
+ return [diffs, truncated];
912
945
  }
913
946
 
914
947
  function getCommonMessage(message, options) {
package/dist/error.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { D as DiffOptions } from './types-widbdqe5.js';
1
+ import { D as DiffOptions } from './types-9l4niLY8.js';
2
2
  import 'pretty-format';
3
3
 
4
4
  declare function serializeError(val: any, seen?: WeakMap<WeakKey, any>): any;
package/dist/helpers.js CHANGED
@@ -74,7 +74,14 @@ function clone(val, seen, options = defaultCloneOptions) {
74
74
  if (!descriptor)
75
75
  continue;
76
76
  const cloned = clone(val[k2], seen, options);
77
- if ("get" in descriptor) {
77
+ if (options.forceWritable) {
78
+ Object.defineProperty(out, k2, {
79
+ enumerable: descriptor.enumerable,
80
+ configurable: true,
81
+ writable: true,
82
+ value: cloned
83
+ });
84
+ } else if ("get" in descriptor) {
78
85
  Object.defineProperty(out, k2, {
79
86
  ...descriptor,
80
87
  get() {
@@ -84,7 +91,6 @@ function clone(val, seen, options = defaultCloneOptions) {
84
91
  } else {
85
92
  Object.defineProperty(out, k2, {
86
93
  ...descriptor,
87
- writable: options.forceWritable ? true : descriptor.writable,
88
94
  value: cloned
89
95
  });
90
96
  }
@@ -27,6 +27,9 @@ interface DiffOptions {
27
27
  omitAnnotationLines?: boolean;
28
28
  patchColor?: DiffOptionsColor;
29
29
  compareKeys?: CompareKeys;
30
+ truncateThreshold?: number;
31
+ truncateAnnotation?: string;
32
+ truncateAnnotationColor?: DiffOptionsColor;
30
33
  }
31
34
 
32
35
  export type { DiffOptions as D, DiffOptionsColor as a };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/utils",
3
3
  "type": "module",
4
- "version": "1.3.1",
4
+ "version": "1.5.0",
5
5
  "description": "Shared Vitest utility functions",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",