From e9f813344d949f286b9ba9e2d5087166095b6f37 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 31 Oct 2024 11:43:51 +0900 Subject: [PATCH] fix: clear added/updated stats --- packages/snapshot/src/port/state.ts | 44 +++++++++++++++-------------- packages/snapshot/src/port/utils.ts | 10 +++++++ 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/packages/snapshot/src/port/state.ts b/packages/snapshot/src/port/state.ts index 4c975155dfb9e..2dfe42cfebc08 100644 --- a/packages/snapshot/src/port/state.ts +++ b/packages/snapshot/src/port/state.ts @@ -23,6 +23,7 @@ import { saveRawSnapshots } from './rawSnapshot' import { addExtraLineBreaks, + CounterMap, DefaultMap, getSnapshotData, keyToTestName, @@ -61,12 +62,11 @@ export default class SnapshotState { private _snapshotFormat: PrettyFormatOptions private _environment: SnapshotEnvironment private _fileExists: boolean - - added: number + private added = new CounterMap() + private matched = new CounterMap() + private unmatched = new CounterMap() + private updated = new CounterMap() expand: boolean - matched: number - unmatched: number - updated: number private constructor( public testFilePath: string, @@ -85,11 +85,7 @@ export default class SnapshotState { this._uncheckedKeys = new Set(Object.keys(this._snapshotData)) this._counters = new Map() this.expand = options.expand || false - this.added = 0 - this.matched = 0 - this.unmatched = 0 this._updateSnapshot = options.updateSnapshot - this.updated = 0 this._snapshotFormat = { printBasicPrototype: false, escapeString: false, @@ -121,11 +117,11 @@ export default class SnapshotState { } clearTest(testId: string): void { - // TODO: reset stats: added, matched, etc.. - + // clear inline this._inlineSnapshots = this._inlineSnapshots.filter(s => s.testId !== testId) this._inlineSnapshotStacks = this._inlineSnapshotStacks.filter(s => s.testId !== testId) + // clear file for (const key of this._testIdToKeys.get(testId)) { const name = keyToTestName(key) const counter = this._counters.get(name) @@ -142,6 +138,12 @@ export default class SnapshotState { } } this._testIdToKeys.delete(testId) + + // clear stats + this.added.delete(testId) + this.updated.delete(testId) + this.matched.delete(testId) + this.unmatched.delete(testId) } protected _inferInlineSnapshotStack(stacks: ParsedStack[]): ParsedStack | null { @@ -357,10 +359,10 @@ export default class SnapshotState { if (this._updateSnapshot === 'all') { if (!pass) { if (hasSnapshot) { - this.updated++ + this.updated.increment(testId) } else { - this.added++ + this.added.increment(testId) } this._addSnapshot(key, receivedSerialized, { @@ -370,7 +372,7 @@ export default class SnapshotState { }) } else { - this.matched++ + this.matched.increment(testId) } } else { @@ -379,7 +381,7 @@ export default class SnapshotState { testId, rawSnapshot, }) - this.added++ + this.added.increment(testId) } return { @@ -392,7 +394,7 @@ export default class SnapshotState { } else { if (!pass) { - this.unmatched++ + this.unmatched.increment(testId) return { actual: removeExtraLineBreaks(receivedSerialized), count, @@ -405,7 +407,7 @@ export default class SnapshotState { } } else { - this.matched++ + this.matched.increment(testId) return { actual: '', count, @@ -436,10 +438,10 @@ export default class SnapshotState { const status = await this.save() snapshot.fileDeleted = status.deleted - snapshot.added = this.added - snapshot.matched = this.matched - snapshot.unmatched = this.unmatched - snapshot.updated = this.updated + snapshot.added = this.added.total() + snapshot.matched = this.matched.total() + snapshot.unmatched = this.unmatched.total() + snapshot.updated = this.updated.total() snapshot.unchecked = !status.deleted ? uncheckedCount : 0 snapshot.uncheckedKeys = Array.from(uncheckedKeys) diff --git a/packages/snapshot/src/port/utils.ts b/packages/snapshot/src/port/utils.ts index 111d15cb15f12..34b73260ef00a 100644 --- a/packages/snapshot/src/port/utils.ts +++ b/packages/snapshot/src/port/utils.ts @@ -281,3 +281,13 @@ export class DefaultMap extends Map { return super.get(key)! } } + +export class CounterMap extends Map { + increment(key: K): void { + this.set(key, (this.get(key) ?? 0) + 1) + } + + total(): number { + return this.values().reduce((x, y) => x + y, 0) + } +}