@vitest/snapshot 4.1.3 → 4.1.5

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/index.d.ts CHANGED
@@ -1,90 +1,8 @@
1
- import { S as SnapshotStateOptions, a as SnapshotMatchOptions, b as SnapshotResult, R as RawSnapshotInfo } from './rawSnapshot.d-CmdgHNLi.js';
2
- export { c as SnapshotData, d as SnapshotSerializer, e as SnapshotSummary, f as SnapshotUpdateState, U as UncheckedSnapshot } from './rawSnapshot.d-CmdgHNLi.js';
3
- import { ParsedStack } from '@vitest/utils';
4
- import { S as SnapshotEnvironment } from './environment.d-DOJxxZV9.js';
1
+ import { S as SnapshotState, a as SnapshotStateOptions, b as SnapshotResult, R as RawSnapshotInfo, D as DomainSnapshotAdapter } from './rawSnapshot.d-D_X3-62x.js';
2
+ export { c as DomainMatchResult, d as SnapshotData, e as SnapshotMatchOptions, f as SnapshotSerializer, g as SnapshotSummary, h as SnapshotUpdateState, U as UncheckedSnapshot } from './rawSnapshot.d-D_X3-62x.js';
5
3
  import { Plugin, Plugins } from '@vitest/pretty-format';
6
-
7
- /**
8
- * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
9
- *
10
- * This source code is licensed under the MIT license found in the
11
- * LICENSE file in the root directory of this source tree.
12
- */
13
-
14
- declare class DefaultMap<
15
- K,
16
- V
17
- > extends Map<K, V> {
18
- private defaultFn;
19
- constructor(defaultFn: (key: K) => V, entries?: Iterable<readonly [K, V]>);
20
- get(key: K): V;
21
- }
22
- declare class CounterMap<K> extends DefaultMap<K, number> {
23
- constructor();
24
- _total: number | undefined;
25
- valueOf(): number;
26
- increment(key: K): void;
27
- total(): number;
28
- }
29
-
30
- interface SnapshotReturnOptions {
31
- actual: string;
32
- count: number;
33
- expected?: string;
34
- key: string;
35
- pass: boolean;
36
- }
37
- interface SaveStatus {
38
- deleted: boolean;
39
- saved: boolean;
40
- }
41
- declare class SnapshotState {
42
- testFilePath: string;
43
- snapshotPath: string;
44
- private _counters;
45
- private _dirty;
46
- private _updateSnapshot;
47
- private _snapshotData;
48
- private _initialData;
49
- private _inlineSnapshots;
50
- private _inlineSnapshotStacks;
51
- private _testIdToKeys;
52
- private _rawSnapshots;
53
- private _uncheckedKeys;
54
- private _snapshotFormat;
55
- private _environment;
56
- private _fileExists;
57
- expand: boolean;
58
- private _added;
59
- private _matched;
60
- private _unmatched;
61
- private _updated;
62
- get added(): CounterMap<string>;
63
- set added(value: number);
64
- get matched(): CounterMap<string>;
65
- set matched(value: number);
66
- get unmatched(): CounterMap<string>;
67
- set unmatched(value: number);
68
- get updated(): CounterMap<string>;
69
- set updated(value: number);
70
- private constructor();
71
- static create(testFilePath: string, options: SnapshotStateOptions): Promise<SnapshotState>;
72
- get environment(): SnapshotEnvironment;
73
- markSnapshotsAsCheckedForTest(testName: string): void;
74
- clearTest(testId: string): void;
75
- protected _inferInlineSnapshotStack(stacks: ParsedStack[]): ParsedStack | null;
76
- private _addSnapshot;
77
- save(): Promise<SaveStatus>;
78
- getUncheckedCount(): number;
79
- getUncheckedKeys(): Array<string>;
80
- removeUncheckedKeys(): void;
81
- probeExpectedSnapshot(options: Pick<SnapshotMatchOptions, "testName" | "testId" | "isInline" | "inlineSnapshot">): {
82
- data?: string;
83
- markAsChecked: () => void;
84
- };
85
- match({ testId, testName, received, key, inlineSnapshot, isInline, error, rawSnapshot, assertionName }: SnapshotMatchOptions): SnapshotReturnOptions;
86
- pack(): Promise<SnapshotResult>;
87
- }
4
+ export { S as SnapshotEnvironment } from './environment.d-DOJxxZV9.js';
5
+ import '@vitest/utils';
88
6
 
89
7
  interface AssertOptions {
90
8
  received: unknown;
@@ -104,6 +22,15 @@ interface AssertOptions {
104
22
  rawSnapshot?: RawSnapshotInfo;
105
23
  assertionName?: string;
106
24
  }
25
+ interface AssertDomainOptions extends Omit<AssertOptions, "received"> {
26
+ received: unknown;
27
+ adapter: DomainSnapshotAdapter<any, any>;
28
+ }
29
+ interface AssertDomainPollOptions extends Omit<AssertDomainOptions, "received"> {
30
+ poll: () => Promise<unknown> | unknown;
31
+ timeout?: number;
32
+ interval?: number;
33
+ }
107
34
  /** Same shape as expect.extend custom matcher result (SyncExpectationResult from @vitest/expect) */
108
35
  interface MatchResult {
109
36
  pass: boolean;
@@ -125,6 +52,8 @@ declare class SnapshotClient {
125
52
  getSnapshotState(filepath: string): SnapshotState;
126
53
  match(options: AssertOptions): MatchResult;
127
54
  assert(options: AssertOptions): void;
55
+ matchDomain(options: AssertDomainOptions): MatchResult;
56
+ pollMatchDomain(options: AssertDomainPollOptions): Promise<MatchResult>;
128
57
  assertRaw(options: AssertOptions): Promise<void>;
129
58
  clear(): void;
130
59
  }
@@ -141,5 +70,5 @@ declare function stripSnapshotIndentation(inlineSnapshot: string): string;
141
70
  declare function addSerializer(plugin: Plugin): void;
142
71
  declare function getSerializers(): Plugins;
143
72
 
144
- export { SnapshotClient, SnapshotEnvironment, SnapshotMatchOptions, SnapshotResult, SnapshotState, SnapshotStateOptions, addSerializer, getSerializers, stripSnapshotIndentation };
73
+ export { DomainSnapshotAdapter, SnapshotClient, SnapshotResult, SnapshotState, SnapshotStateOptions, addSerializer, getSerializers, stripSnapshotIndentation };
145
74
  export type { MatchResult };
package/dist/index.js CHANGED
@@ -533,6 +533,7 @@ class SnapshotState {
533
533
  this._snapshotFormat = {
534
534
  printBasicPrototype: false,
535
535
  escapeString: false,
536
+ maxOutputLength: 2 ** 27,
536
537
  ...options.snapshotFormat
537
538
  };
538
539
  this._environment = options.snapshotEnvironment;
@@ -542,6 +543,9 @@ class SnapshotState {
542
543
  const content = await options.snapshotEnvironment.readSnapshotFile(snapshotPath);
543
544
  return new SnapshotState(testFilePath, snapshotPath, content, options);
544
545
  }
546
+ get snapshotUpdateState() {
547
+ return this._updateSnapshot;
548
+ }
545
549
  get environment() {
546
550
  return this._environment;
547
551
  }
@@ -583,6 +587,11 @@ class SnapshotState {
583
587
  if (promiseIndex !== -1) {
584
588
  return stacks[promiseIndex + 3];
585
589
  }
590
+ // support poll + inline snapshot
591
+ const pollChainIndex = stacks.findIndex((i) => i.method.match(/__VITEST_POLL_CHAIN__/));
592
+ if (pollChainIndex !== -1) {
593
+ return stacks[pollChainIndex + 1];
594
+ }
586
595
  // inline snapshot function can be named __INLINE_SNAPSHOT_OFFSET_<n>__
587
596
  // to specify a custom stack offset
588
597
  for (let i = 0; i < stacks.length; i++) {
@@ -620,6 +629,114 @@ class SnapshotState {
620
629
  this._snapshotData[key] = receivedSerialized;
621
630
  }
622
631
  }
632
+ _resolveKey(testId, testName, key) {
633
+ this._counters.increment(testName);
634
+ const count = this._counters.get(testName);
635
+ if (!key) {
636
+ key = testNameToKey(testName, count);
637
+ }
638
+ this._testIdToKeys.get(testId).push(key);
639
+ return {
640
+ key,
641
+ count
642
+ };
643
+ }
644
+ _resolveInlineStack(options) {
645
+ const { testId, snapshot, assertionName, error } = options;
646
+ const stacks = parseErrorStacktrace(error, { ignoreStackEntries: [] });
647
+ const _stack = this._inferInlineSnapshotStack(stacks);
648
+ if (!_stack) {
649
+ const message = stacks.map((s) => ` ${s.file}:${s.line}:${s.column}${s.method ? ` (${s.method})` : ""}`).join("\n");
650
+ throw new Error(`@vitest/snapshot: Couldn't infer stack frame for inline snapshot.\n${message}`);
651
+ }
652
+ const stack = this.environment.processStackTrace?.(_stack) || _stack;
653
+ // removing 1 column, because source map points to the wrong
654
+ // location for js files, but `column-1` points to the same in both js/ts
655
+ // https://github.com/vitejs/vite/issues/8657
656
+ stack.column--;
657
+ // reject multiple inline snapshots at the same location if snapshot is different
658
+ const snapshotsWithSameStack = this._inlineSnapshotStacks.filter((s) => isSameStackPosition(s, stack));
659
+ if (snapshotsWithSameStack.length > 0) {
660
+ // ensure only one snapshot will be written at the same location
661
+ this._inlineSnapshots = this._inlineSnapshots.filter((s) => !isSameStackPosition(s, stack));
662
+ const differentSnapshot = snapshotsWithSameStack.find((s) => s.snapshot !== snapshot);
663
+ if (differentSnapshot) {
664
+ throw Object.assign(new Error(`${assertionName} with different snapshots cannot be called at the same location`), {
665
+ actual: snapshot,
666
+ expected: differentSnapshot.snapshot
667
+ });
668
+ }
669
+ }
670
+ this._inlineSnapshotStacks.push({
671
+ ...stack,
672
+ testId,
673
+ snapshot
674
+ });
675
+ return stack;
676
+ }
677
+ _reconcile(opts) {
678
+ // These are the conditions on when to write snapshots:
679
+ // * There's no snapshot file in a non-CI environment.
680
+ // * There is a snapshot file and we decided to update the snapshot.
681
+ // * There is a snapshot file, but it doesn't have this snapshot.
682
+ // These are the conditions on when not to write snapshots:
683
+ // * The update flag is set to 'none'.
684
+ // * There's no snapshot file or a file without this snapshot on a CI environment.
685
+ if (opts.hasSnapshot && this._updateSnapshot === "all" || (!opts.hasSnapshot || !opts.snapshotIsPersisted) && (this._updateSnapshot === "new" || this._updateSnapshot === "all")) {
686
+ if (this._updateSnapshot === "all") {
687
+ if (!opts.pass) {
688
+ if (opts.hasSnapshot) {
689
+ this.updated.increment(opts.testId);
690
+ } else {
691
+ this.added.increment(opts.testId);
692
+ }
693
+ this._addSnapshot(opts.key, opts.addValue, {
694
+ stack: opts.stack,
695
+ testId: opts.testId,
696
+ rawSnapshot: opts.rawSnapshot,
697
+ assertionName: opts.assertionName
698
+ });
699
+ } else {
700
+ this.matched.increment(opts.testId);
701
+ }
702
+ } else {
703
+ this._addSnapshot(opts.key, opts.addValue, {
704
+ stack: opts.stack,
705
+ testId: opts.testId,
706
+ rawSnapshot: opts.rawSnapshot,
707
+ assertionName: opts.assertionName
708
+ });
709
+ this.added.increment(opts.testId);
710
+ }
711
+ return {
712
+ actual: "",
713
+ count: opts.count,
714
+ expected: "",
715
+ key: opts.key,
716
+ pass: true
717
+ };
718
+ } else {
719
+ if (!opts.pass) {
720
+ this.unmatched.increment(opts.testId);
721
+ return {
722
+ actual: opts.actualDisplay,
723
+ count: opts.count,
724
+ expected: opts.expectedDisplay,
725
+ key: opts.key,
726
+ pass: false
727
+ };
728
+ } else {
729
+ this.matched.increment(opts.testId);
730
+ return {
731
+ actual: "",
732
+ count: opts.count,
733
+ expected: "",
734
+ key: opts.key,
735
+ pass: true
736
+ };
737
+ }
738
+ }
739
+ }
623
740
  async save() {
624
741
  const hasExternalSnapshots = Object.keys(this._snapshotData).length;
625
742
  const hasInlineSnapshots = this._inlineSnapshots.length;
@@ -667,6 +784,8 @@ class SnapshotState {
667
784
  const count = this._counters.get(options.testName) + 1;
668
785
  const key = testNameToKey(options.testName, count);
669
786
  return {
787
+ key,
788
+ count,
670
789
  data: options?.isInline ? options.inlineSnapshot : this._snapshotData[key],
671
790
  markAsChecked: () => {
672
791
  this._counters.increment(options.testName);
@@ -676,13 +795,9 @@ class SnapshotState {
676
795
  };
677
796
  }
678
797
  match({ testId, testName, received, key, inlineSnapshot, isInline, error, rawSnapshot, assertionName }) {
679
- // this also increments counter for inline snapshots. maybe we shouldn't?
680
- this._counters.increment(testName);
681
- const count = this._counters.get(testName);
682
- if (!key) {
683
- key = testNameToKey(testName, count);
684
- }
685
- this._testIdToKeys.get(testId).push(key);
798
+ const resolved = this._resolveKey(testId, testName, key);
799
+ key = resolved.key;
800
+ const count = resolved.count;
686
801
  // Do not mark the snapshot as "checked" if the snapshot is inline and
687
802
  // there's an external snapshot. This way the external snapshot can be
688
803
  // removed with `--updateSnapshot`.
@@ -705,107 +820,57 @@ class SnapshotState {
705
820
  const hasSnapshot = expected !== undefined;
706
821
  const snapshotIsPersisted = isInline || this._fileExists || rawSnapshot && rawSnapshot.content != null;
707
822
  if (pass && !isInline && !rawSnapshot) {
708
- // Executing a snapshot file as JavaScript and writing the strings back
709
- // when other snapshots have changed loses the proper escaping for some
710
- // characters. Since we check every snapshot in every test, use the newly
711
- // generated formatted string.
712
- // Note that this is only relevant when a snapshot is added and the dirty
713
- // flag is set.
823
+ // When the file is re-saved (because other snapshots changed), the JS
824
+ // round-trip can lose proper escaping. Refresh in-memory data with the
825
+ // freshly serialized string so the file is written correctly.
826
+ // _reconcile does not write _snapshotData on pass, so this is the only
827
+ // place it gets refreshed. Domain snapshots skip this because the stored
828
+ // value may contain match patterns that differ from the received output.
714
829
  this._snapshotData[key] = receivedSerialized;
715
830
  }
716
- // find call site of toMatchInlineSnapshot
717
- let stack;
718
- if (isInline) {
719
- const stacks = parseErrorStacktrace(error || new Error("snapshot"), { ignoreStackEntries: [] });
720
- const _stack = this._inferInlineSnapshotStack(stacks);
721
- if (!_stack) {
722
- throw new Error(`@vitest/snapshot: Couldn't infer stack frame for inline snapshot.\n${JSON.stringify(stacks)}`);
723
- }
724
- stack = this.environment.processStackTrace?.(_stack) || _stack;
725
- // removing 1 column, because source map points to the wrong
726
- // location for js files, but `column-1` points to the same in both js/ts
727
- // https://github.com/vitejs/vite/issues/8657
728
- stack.column--;
729
- // reject multiple inline snapshots at the same location if snapshot is different
730
- const snapshotsWithSameStack = this._inlineSnapshotStacks.filter((s) => isSameStackPosition(s, stack));
731
- if (snapshotsWithSameStack.length > 0) {
732
- // ensure only one snapshot will be written at the same location
733
- this._inlineSnapshots = this._inlineSnapshots.filter((s) => !isSameStackPosition(s, stack));
734
- const differentSnapshot = snapshotsWithSameStack.find((s) => s.snapshot !== receivedSerialized);
735
- if (differentSnapshot) {
736
- throw Object.assign(new Error("toMatchInlineSnapshot with different snapshots cannot be called at the same location"), {
737
- actual: receivedSerialized,
738
- expected: differentSnapshot.snapshot
739
- });
740
- }
741
- }
742
- this._inlineSnapshotStacks.push({
743
- ...stack,
744
- testId,
745
- snapshot: receivedSerialized
746
- });
747
- }
748
- // These are the conditions on when to write snapshots:
749
- // * There's no snapshot file in a non-CI environment.
750
- // * There is a snapshot file and we decided to update the snapshot.
751
- // * There is a snapshot file, but it doesn't have this snapshot.
752
- // These are the conditions on when not to write snapshots:
753
- // * The update flag is set to 'none'.
754
- // * There's no snapshot file or a file without this snapshot on a CI environment.
755
- if (hasSnapshot && this._updateSnapshot === "all" || (!hasSnapshot || !snapshotIsPersisted) && (this._updateSnapshot === "new" || this._updateSnapshot === "all")) {
756
- if (this._updateSnapshot === "all") {
757
- if (!pass) {
758
- if (hasSnapshot) {
759
- this.updated.increment(testId);
760
- } else {
761
- this.added.increment(testId);
762
- }
763
- this._addSnapshot(key, receivedSerialized, {
764
- stack,
765
- testId,
766
- rawSnapshot,
767
- assertionName
768
- });
769
- } else {
770
- this.matched.increment(testId);
771
- }
772
- } else {
773
- this._addSnapshot(key, receivedSerialized, {
774
- stack,
775
- testId,
776
- rawSnapshot,
777
- assertionName
778
- });
779
- this.added.increment(testId);
780
- }
781
- return {
782
- actual: "",
783
- count,
784
- expected: "",
785
- key,
786
- pass: true
787
- };
788
- } else {
789
- if (!pass) {
790
- this.unmatched.increment(testId);
791
- return {
792
- actual: rawSnapshot ? receivedSerialized : removeExtraLineBreaks(receivedSerialized),
793
- count,
794
- expected: expectedTrimmed !== undefined ? rawSnapshot ? expectedTrimmed : removeExtraLineBreaks(expectedTrimmed) : undefined,
795
- key,
796
- pass: false
797
- };
798
- } else {
799
- this.matched.increment(testId);
800
- return {
801
- actual: "",
802
- count,
803
- expected: "",
804
- key,
805
- pass: true
806
- };
807
- }
808
- }
831
+ const stack = isInline ? this._resolveInlineStack({
832
+ testId,
833
+ snapshot: receivedSerialized,
834
+ assertionName: assertionName || "toMatchInlineSnapshot",
835
+ error: error || new Error("snapshot")
836
+ }) : undefined;
837
+ return this._reconcile({
838
+ testId,
839
+ key,
840
+ count,
841
+ pass,
842
+ hasSnapshot,
843
+ snapshotIsPersisted: !!snapshotIsPersisted,
844
+ addValue: receivedSerialized,
845
+ actualDisplay: rawSnapshot ? receivedSerialized : removeExtraLineBreaks(receivedSerialized),
846
+ expectedDisplay: expectedTrimmed !== undefined ? rawSnapshot ? expectedTrimmed : removeExtraLineBreaks(expectedTrimmed) : undefined,
847
+ stack,
848
+ rawSnapshot,
849
+ assertionName
850
+ });
851
+ }
852
+ processDomainSnapshot({ testId, received, expectedSnapshot, matchResult, isInline, error, assertionName }) {
853
+ const stack = isInline ? this._resolveInlineStack({
854
+ testId,
855
+ snapshot: received,
856
+ assertionName,
857
+ error: error || new Error("STACK_TRACE_ERROR")
858
+ }) : undefined;
859
+ const actualResolved = matchResult?.resolved ?? received;
860
+ const expectedResolved = matchResult?.expected ?? expectedSnapshot.data;
861
+ return this._reconcile({
862
+ testId,
863
+ key: expectedSnapshot.key,
864
+ count: expectedSnapshot.count,
865
+ pass: matchResult?.pass ?? false,
866
+ hasSnapshot: !!expectedSnapshot.data,
867
+ snapshotIsPersisted: isInline ? true : this._fileExists,
868
+ addValue: actualResolved,
869
+ actualDisplay: removeExtraLineBreaks(actualResolved),
870
+ expectedDisplay: expectedResolved !== undefined ? removeExtraLineBreaks(expectedResolved) : undefined,
871
+ stack,
872
+ assertionName
873
+ });
809
874
  }
810
875
  async pack() {
811
876
  const snapshot = {
@@ -946,6 +1011,93 @@ class SnapshotClient {
946
1011
  throw createMismatchError(result.message(), snapshotState.expand, result.actual, result.expected);
947
1012
  }
948
1013
  }
1014
+ matchDomain(options) {
1015
+ const { received, filepath, name, testId = name, message, adapter, isInline = false, inlineSnapshot, error } = options;
1016
+ if (!filepath) {
1017
+ throw new Error("Snapshot cannot be used outside of test");
1018
+ }
1019
+ const captured = adapter.capture(received);
1020
+ const rendered = adapter.render(captured);
1021
+ const snapshotState = this.getSnapshotState(filepath);
1022
+ const testName = [name, ...message ? [message] : []].join(" > ");
1023
+ const expectedSnapshot = snapshotState.probeExpectedSnapshot({
1024
+ testName,
1025
+ testId,
1026
+ isInline,
1027
+ inlineSnapshot
1028
+ });
1029
+ expectedSnapshot.markAsChecked();
1030
+ const matchResult = expectedSnapshot.data ? adapter.match(captured, adapter.parseExpected(expectedSnapshot.data)) : undefined;
1031
+ const { actual, expected, key, pass } = snapshotState.processDomainSnapshot({
1032
+ testId,
1033
+ received: rendered,
1034
+ expectedSnapshot,
1035
+ matchResult,
1036
+ isInline,
1037
+ error,
1038
+ assertionName: options.assertionName
1039
+ });
1040
+ return {
1041
+ pass,
1042
+ message: () => `Snapshot \`${key}\` mismatched`,
1043
+ actual: actual?.trim(),
1044
+ expected: expected?.trim()
1045
+ };
1046
+ }
1047
+ async pollMatchDomain(options) {
1048
+ const { poll, filepath, name, testId = name, message, adapter, isInline = false, inlineSnapshot, error, timeout = 1e3, interval = 50 } = options;
1049
+ if (!filepath) {
1050
+ throw new Error("Snapshot cannot be used outside of test");
1051
+ }
1052
+ const snapshotState = this.getSnapshotState(filepath);
1053
+ const testName = [name, ...message ? [message] : []].join(" > ");
1054
+ const expectedSnapshot = snapshotState.probeExpectedSnapshot({
1055
+ testName,
1056
+ testId,
1057
+ isInline,
1058
+ inlineSnapshot
1059
+ });
1060
+ const reference = expectedSnapshot.data && snapshotState.snapshotUpdateState !== "all" ? adapter.parseExpected(expectedSnapshot.data) : undefined;
1061
+ const timedOut = timeout > 0 ? new Promise((r) => setTimeout(r, timeout)) : undefined;
1062
+ const stableResult = await getStableSnapshot({
1063
+ adapter,
1064
+ poll,
1065
+ interval,
1066
+ timedOut,
1067
+ match: reference ? (captured) => adapter.match(captured, reference).pass : undefined
1068
+ });
1069
+ expectedSnapshot.markAsChecked();
1070
+ if (!stableResult?.rendered) {
1071
+ // the original caller `expect.poll` later manipulates error via `throwWithCause`,
1072
+ // so here we can directly throw `lastPollError` if exists.
1073
+ if (stableResult?.lastPollError) {
1074
+ throw stableResult.lastPollError;
1075
+ }
1076
+ return {
1077
+ pass: false,
1078
+ message: () => `poll() did not produce a stable snapshot within the timeout`
1079
+ };
1080
+ }
1081
+ // TODO: should `all` mode ignore parse error?
1082
+ // Sielently hiding the error and creating snaphsot full scratch isn't good either.
1083
+ // Users can fix or purge the broken snapshot manually and that decision affects how domain snapshot gets updated.
1084
+ const matchResult = expectedSnapshot.data ? adapter.match(stableResult.captured, adapter.parseExpected(expectedSnapshot.data)) : undefined;
1085
+ const { actual, expected, key, pass } = snapshotState.processDomainSnapshot({
1086
+ testId,
1087
+ received: stableResult.rendered,
1088
+ expectedSnapshot,
1089
+ matchResult,
1090
+ isInline,
1091
+ error,
1092
+ assertionName: options.assertionName
1093
+ });
1094
+ return {
1095
+ pass,
1096
+ message: () => `Snapshot \`${key}\` mismatched`,
1097
+ actual: actual?.trim(),
1098
+ expected: expected?.trim()
1099
+ };
1100
+ }
949
1101
  async assertRaw(options) {
950
1102
  if (!options.rawSnapshot) {
951
1103
  throw new Error("Raw snapshot is required");
@@ -968,5 +1120,69 @@ class SnapshotClient {
968
1120
  this.snapshotStateMap.clear();
969
1121
  }
970
1122
  }
1123
+ /**
1124
+ * Polls repeatedly until the value reaches a stable state.
1125
+ *
1126
+ * Compares consecutive rendered outputs from the current session —
1127
+ * when two consecutive polls produce the same rendered string,
1128
+ * the value is considered stable.
1129
+ *
1130
+ * Every `await` (poll call, interval delay) races against `timedOut`
1131
+ * so that hanging polls and delays are interrupted.
1132
+ */
1133
+ async function getStableSnapshot({ adapter, poll, interval, timedOut, match }) {
1134
+ let lastRendered;
1135
+ let lastPollError;
1136
+ let lastStable;
1137
+ while (true) {
1138
+ try {
1139
+ const pollResult = await raceWith(Promise.resolve(poll()), timedOut);
1140
+ if (!pollResult.ok) {
1141
+ break;
1142
+ }
1143
+ const captured = adapter.capture(pollResult.value);
1144
+ const rendered = adapter.render(captured);
1145
+ if (lastRendered !== undefined && rendered === lastRendered) {
1146
+ lastStable = {
1147
+ captured,
1148
+ rendered
1149
+ };
1150
+ if (!match || match(captured)) {
1151
+ break;
1152
+ }
1153
+ } else {
1154
+ lastRendered = rendered;
1155
+ lastStable = undefined;
1156
+ }
1157
+ } catch (pollError) {
1158
+ // poll() threw — reset stability baseline and retry
1159
+ lastRendered = undefined;
1160
+ lastStable = undefined;
1161
+ lastPollError = pollError;
1162
+ }
1163
+ const delayed = await raceWith(new Promise((r) => setTimeout(r, interval)), timedOut);
1164
+ if (!delayed.ok) {
1165
+ break;
1166
+ }
1167
+ }
1168
+ return {
1169
+ ...lastStable,
1170
+ lastPollError
1171
+ };
1172
+ }
1173
+ /** Type-safe `Promise.race` — tells you which promise won. */
1174
+ function raceWith(promise, other) {
1175
+ const left = promise.then((value) => ({
1176
+ ok: true,
1177
+ value
1178
+ }));
1179
+ if (!other) {
1180
+ return left;
1181
+ }
1182
+ return Promise.race([left, other.then((value) => ({
1183
+ ok: false,
1184
+ value
1185
+ }))]);
1186
+ }
971
1187
 
972
1188
  export { SnapshotClient, SnapshotState, addSerializer, getSerializers, stripSnapshotIndentation };
package/dist/manager.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { S as SnapshotStateOptions, e as SnapshotSummary, b as SnapshotResult } from './rawSnapshot.d-CmdgHNLi.js';
1
+ import { a as SnapshotStateOptions, g as SnapshotSummary, b as SnapshotResult } from './rawSnapshot.d-D_X3-62x.js';
2
2
  import '@vitest/pretty-format';
3
- import './environment.d-DOJxxZV9.js';
4
3
  import '@vitest/utils';
4
+ import './environment.d-DOJxxZV9.js';
5
5
 
6
6
  declare class SnapshotManager {
7
7
  options: Omit<SnapshotStateOptions, "snapshotEnvironment">;
@@ -0,0 +1,205 @@
1
+ import { OptionsReceived, Plugin } from '@vitest/pretty-format';
2
+ import { ParsedStack } from '@vitest/utils';
3
+ import { S as SnapshotEnvironment } from './environment.d-DOJxxZV9.js';
4
+
5
+ interface DomainMatchResult {
6
+ pass: boolean;
7
+ message?: string;
8
+ /**
9
+ * The captured value viewed through the template's lens.
10
+ *
11
+ * Where the template uses patterns (e.g. regexes) or omits details,
12
+ * the resolved string adopts those patterns. Where the template doesn't
13
+ * match, the resolved string uses literal captured values instead.
14
+ *
15
+ * Used for two purposes:
16
+ * - **Diff display** (actual side): compared against `expected`
17
+ * so the diff highlights only genuine mismatches, not pattern-vs-literal noise.
18
+ * - **Snapshot update** (`--update`): written as the new snapshot content,
19
+ * preserving user-edited patterns from matched regions while incorporating
20
+ * actual values for mismatched regions.
21
+ *
22
+ * When omitted, falls back to `render(capture(received))` (the raw rendered value).
23
+ */
24
+ resolved?: string;
25
+ /**
26
+ * The stored template re-rendered as a string, representing what the user
27
+ * originally wrote or last saved.
28
+ *
29
+ * Used as the expected side in diff display.
30
+ *
31
+ * When omitted, falls back to the raw snapshot string from the snap file
32
+ * or inline snapshot.
33
+ */
34
+ expected?: string;
35
+ }
36
+ interface DomainSnapshotAdapter<
37
+ Captured = unknown,
38
+ Expected = unknown
39
+ > {
40
+ name: string;
41
+ capture: (received: unknown) => Captured;
42
+ render: (captured: Captured) => string;
43
+ parseExpected: (input: string) => Expected;
44
+ match: (captured: Captured, expected: Expected) => DomainMatchResult;
45
+ }
46
+
47
+ /**
48
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
49
+ *
50
+ * This source code is licensed under the MIT license found in the
51
+ * LICENSE file in the root directory of this source tree.
52
+ */
53
+
54
+ declare class DefaultMap<
55
+ K,
56
+ V
57
+ > extends Map<K, V> {
58
+ private defaultFn;
59
+ constructor(defaultFn: (key: K) => V, entries?: Iterable<readonly [K, V]>);
60
+ get(key: K): V;
61
+ }
62
+ declare class CounterMap<K> extends DefaultMap<K, number> {
63
+ constructor();
64
+ _total: number | undefined;
65
+ valueOf(): number;
66
+ increment(key: K): void;
67
+ total(): number;
68
+ }
69
+
70
+ interface SnapshotReturnOptions {
71
+ actual: string;
72
+ count: number;
73
+ expected?: string;
74
+ key: string;
75
+ pass: boolean;
76
+ }
77
+ interface SaveStatus {
78
+ deleted: boolean;
79
+ saved: boolean;
80
+ }
81
+ interface ExpectedSnapshot {
82
+ key: string;
83
+ count: number;
84
+ data?: string;
85
+ markAsChecked: () => void;
86
+ }
87
+ declare class SnapshotState {
88
+ testFilePath: string;
89
+ snapshotPath: string;
90
+ private _counters;
91
+ private _dirty;
92
+ private _updateSnapshot;
93
+ private _snapshotData;
94
+ private _initialData;
95
+ private _inlineSnapshots;
96
+ private _inlineSnapshotStacks;
97
+ private _testIdToKeys;
98
+ private _rawSnapshots;
99
+ private _uncheckedKeys;
100
+ private _snapshotFormat;
101
+ private _environment;
102
+ private _fileExists;
103
+ expand: boolean;
104
+ private _added;
105
+ private _matched;
106
+ private _unmatched;
107
+ private _updated;
108
+ get added(): CounterMap<string>;
109
+ set added(value: number);
110
+ get matched(): CounterMap<string>;
111
+ set matched(value: number);
112
+ get unmatched(): CounterMap<string>;
113
+ set unmatched(value: number);
114
+ get updated(): CounterMap<string>;
115
+ set updated(value: number);
116
+ private constructor();
117
+ static create(testFilePath: string, options: SnapshotStateOptions): Promise<SnapshotState>;
118
+ get snapshotUpdateState(): SnapshotUpdateState;
119
+ get environment(): SnapshotEnvironment;
120
+ markSnapshotsAsCheckedForTest(testName: string): void;
121
+ clearTest(testId: string): void;
122
+ protected _inferInlineSnapshotStack(stacks: ParsedStack[]): ParsedStack | null;
123
+ private _addSnapshot;
124
+ private _resolveKey;
125
+ private _resolveInlineStack;
126
+ private _reconcile;
127
+ save(): Promise<SaveStatus>;
128
+ getUncheckedCount(): number;
129
+ getUncheckedKeys(): Array<string>;
130
+ removeUncheckedKeys(): void;
131
+ probeExpectedSnapshot(options: Pick<SnapshotMatchOptions, "testName" | "testId" | "isInline" | "inlineSnapshot">): ExpectedSnapshot;
132
+ match({ testId, testName, received, key, inlineSnapshot, isInline, error, rawSnapshot, assertionName }: SnapshotMatchOptions): SnapshotReturnOptions;
133
+ processDomainSnapshot({ testId, received, expectedSnapshot, matchResult, isInline, error, assertionName }: ProcessDomainSnapshotOptions): SnapshotReturnOptions;
134
+ pack(): Promise<SnapshotResult>;
135
+ }
136
+
137
+ type SnapshotData = Record<string, string>;
138
+ type SnapshotUpdateState = "all" | "new" | "none";
139
+ type SnapshotSerializer = Plugin;
140
+ interface SnapshotStateOptions {
141
+ updateSnapshot: SnapshotUpdateState;
142
+ snapshotEnvironment: SnapshotEnvironment;
143
+ expand?: boolean;
144
+ snapshotFormat?: OptionsReceived;
145
+ resolveSnapshotPath?: (path: string, extension: string, context?: any) => string;
146
+ }
147
+ interface SnapshotMatchOptions {
148
+ testId: string;
149
+ testName: string;
150
+ received: unknown;
151
+ key?: string;
152
+ inlineSnapshot?: string;
153
+ isInline: boolean;
154
+ error?: Error;
155
+ rawSnapshot?: RawSnapshotInfo;
156
+ assertionName?: string;
157
+ }
158
+ interface ProcessDomainSnapshotOptions {
159
+ testId: string;
160
+ received: string;
161
+ expectedSnapshot: ExpectedSnapshot;
162
+ matchResult?: DomainMatchResult;
163
+ isInline?: boolean;
164
+ assertionName?: string;
165
+ error?: Error;
166
+ }
167
+ interface SnapshotResult {
168
+ filepath: string;
169
+ added: number;
170
+ fileDeleted: boolean;
171
+ matched: number;
172
+ unchecked: number;
173
+ uncheckedKeys: Array<string>;
174
+ unmatched: number;
175
+ updated: number;
176
+ }
177
+ interface UncheckedSnapshot {
178
+ filePath: string;
179
+ keys: Array<string>;
180
+ }
181
+ interface SnapshotSummary {
182
+ added: number;
183
+ didUpdate: boolean;
184
+ failure: boolean;
185
+ filesAdded: number;
186
+ filesRemoved: number;
187
+ filesRemovedList: Array<string>;
188
+ filesUnmatched: number;
189
+ filesUpdated: number;
190
+ matched: number;
191
+ total: number;
192
+ unchecked: number;
193
+ uncheckedKeysByFile: Array<UncheckedSnapshot>;
194
+ unmatched: number;
195
+ updated: number;
196
+ }
197
+
198
+ interface RawSnapshotInfo {
199
+ file: string;
200
+ readonly?: boolean;
201
+ content?: string;
202
+ }
203
+
204
+ export { SnapshotState as S };
205
+ export type { DomainSnapshotAdapter as D, RawSnapshotInfo as R, UncheckedSnapshot as U, SnapshotStateOptions as a, SnapshotResult as b, DomainMatchResult as c, SnapshotData as d, SnapshotMatchOptions as e, SnapshotSerializer as f, SnapshotSummary as g, SnapshotUpdateState as h };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/snapshot",
3
3
  "type": "module",
4
- "version": "4.1.3",
4
+ "version": "4.1.5",
5
5
  "description": "Vitest snapshot manager",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -45,8 +45,8 @@
45
45
  "dependencies": {
46
46
  "magic-string": "^0.30.21",
47
47
  "pathe": "^2.0.3",
48
- "@vitest/utils": "4.1.3",
49
- "@vitest/pretty-format": "4.1.3"
48
+ "@vitest/pretty-format": "4.1.5",
49
+ "@vitest/utils": "4.1.5"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/natural-compare": "^1.4.3",
@@ -1,62 +0,0 @@
1
- import { OptionsReceived, Plugin } from '@vitest/pretty-format';
2
- import { S as SnapshotEnvironment } from './environment.d-DOJxxZV9.js';
3
-
4
- type SnapshotData = Record<string, string>;
5
- type SnapshotUpdateState = "all" | "new" | "none";
6
- type SnapshotSerializer = Plugin;
7
- interface SnapshotStateOptions {
8
- updateSnapshot: SnapshotUpdateState;
9
- snapshotEnvironment: SnapshotEnvironment;
10
- expand?: boolean;
11
- snapshotFormat?: OptionsReceived;
12
- resolveSnapshotPath?: (path: string, extension: string, context?: any) => string;
13
- }
14
- interface SnapshotMatchOptions {
15
- testId: string;
16
- testName: string;
17
- received: unknown;
18
- key?: string;
19
- inlineSnapshot?: string;
20
- isInline: boolean;
21
- error?: Error;
22
- rawSnapshot?: RawSnapshotInfo;
23
- assertionName?: string;
24
- }
25
- interface SnapshotResult {
26
- filepath: string;
27
- added: number;
28
- fileDeleted: boolean;
29
- matched: number;
30
- unchecked: number;
31
- uncheckedKeys: Array<string>;
32
- unmatched: number;
33
- updated: number;
34
- }
35
- interface UncheckedSnapshot {
36
- filePath: string;
37
- keys: Array<string>;
38
- }
39
- interface SnapshotSummary {
40
- added: number;
41
- didUpdate: boolean;
42
- failure: boolean;
43
- filesAdded: number;
44
- filesRemoved: number;
45
- filesRemovedList: Array<string>;
46
- filesUnmatched: number;
47
- filesUpdated: number;
48
- matched: number;
49
- total: number;
50
- unchecked: number;
51
- uncheckedKeysByFile: Array<UncheckedSnapshot>;
52
- unmatched: number;
53
- updated: number;
54
- }
55
-
56
- interface RawSnapshotInfo {
57
- file: string;
58
- readonly?: boolean;
59
- content?: string;
60
- }
61
-
62
- export type { RawSnapshotInfo as R, SnapshotStateOptions as S, UncheckedSnapshot as U, SnapshotMatchOptions as a, SnapshotResult as b, SnapshotData as c, SnapshotSerializer as d, SnapshotSummary as e, SnapshotUpdateState as f };