Skip to content

Commit e9f8133

Browse files
committed
fix: clear added/updated stats
1 parent 80accd6 commit e9f8133

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

packages/snapshot/src/port/state.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { saveRawSnapshots } from './rawSnapshot'
2323

2424
import {
2525
addExtraLineBreaks,
26+
CounterMap,
2627
DefaultMap,
2728
getSnapshotData,
2829
keyToTestName,
@@ -61,12 +62,11 @@ export default class SnapshotState {
6162
private _snapshotFormat: PrettyFormatOptions
6263
private _environment: SnapshotEnvironment
6364
private _fileExists: boolean
64-
65-
added: number
65+
private added = new CounterMap<string>()
66+
private matched = new CounterMap<string>()
67+
private unmatched = new CounterMap<string>()
68+
private updated = new CounterMap<string>()
6669
expand: boolean
67-
matched: number
68-
unmatched: number
69-
updated: number
7070

7171
private constructor(
7272
public testFilePath: string,
@@ -85,11 +85,7 @@ export default class SnapshotState {
8585
this._uncheckedKeys = new Set(Object.keys(this._snapshotData))
8686
this._counters = new Map()
8787
this.expand = options.expand || false
88-
this.added = 0
89-
this.matched = 0
90-
this.unmatched = 0
9188
this._updateSnapshot = options.updateSnapshot
92-
this.updated = 0
9389
this._snapshotFormat = {
9490
printBasicPrototype: false,
9591
escapeString: false,
@@ -121,11 +117,11 @@ export default class SnapshotState {
121117
}
122118

123119
clearTest(testId: string): void {
124-
// TODO: reset stats: added, matched, etc..
125-
120+
// clear inline
126121
this._inlineSnapshots = this._inlineSnapshots.filter(s => s.testId !== testId)
127122
this._inlineSnapshotStacks = this._inlineSnapshotStacks.filter(s => s.testId !== testId)
128123

124+
// clear file
129125
for (const key of this._testIdToKeys.get(testId)) {
130126
const name = keyToTestName(key)
131127
const counter = this._counters.get(name)
@@ -142,6 +138,12 @@ export default class SnapshotState {
142138
}
143139
}
144140
this._testIdToKeys.delete(testId)
141+
142+
// clear stats
143+
this.added.delete(testId)
144+
this.updated.delete(testId)
145+
this.matched.delete(testId)
146+
this.unmatched.delete(testId)
145147
}
146148

147149
protected _inferInlineSnapshotStack(stacks: ParsedStack[]): ParsedStack | null {
@@ -357,10 +359,10 @@ export default class SnapshotState {
357359
if (this._updateSnapshot === 'all') {
358360
if (!pass) {
359361
if (hasSnapshot) {
360-
this.updated++
362+
this.updated.increment(testId)
361363
}
362364
else {
363-
this.added++
365+
this.added.increment(testId)
364366
}
365367

366368
this._addSnapshot(key, receivedSerialized, {
@@ -370,7 +372,7 @@ export default class SnapshotState {
370372
})
371373
}
372374
else {
373-
this.matched++
375+
this.matched.increment(testId)
374376
}
375377
}
376378
else {
@@ -379,7 +381,7 @@ export default class SnapshotState {
379381
testId,
380382
rawSnapshot,
381383
})
382-
this.added++
384+
this.added.increment(testId)
383385
}
384386

385387
return {
@@ -392,7 +394,7 @@ export default class SnapshotState {
392394
}
393395
else {
394396
if (!pass) {
395-
this.unmatched++
397+
this.unmatched.increment(testId)
396398
return {
397399
actual: removeExtraLineBreaks(receivedSerialized),
398400
count,
@@ -405,7 +407,7 @@ export default class SnapshotState {
405407
}
406408
}
407409
else {
408-
this.matched++
410+
this.matched.increment(testId)
409411
return {
410412
actual: '',
411413
count,
@@ -436,10 +438,10 @@ export default class SnapshotState {
436438

437439
const status = await this.save()
438440
snapshot.fileDeleted = status.deleted
439-
snapshot.added = this.added
440-
snapshot.matched = this.matched
441-
snapshot.unmatched = this.unmatched
442-
snapshot.updated = this.updated
441+
snapshot.added = this.added.total()
442+
snapshot.matched = this.matched.total()
443+
snapshot.unmatched = this.unmatched.total()
444+
snapshot.updated = this.updated.total()
443445
snapshot.unchecked = !status.deleted ? uncheckedCount : 0
444446
snapshot.uncheckedKeys = Array.from(uncheckedKeys)
445447

packages/snapshot/src/port/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,13 @@ export class DefaultMap<K, V> extends Map<K, V> {
281281
return super.get(key)!
282282
}
283283
}
284+
285+
export class CounterMap<K> extends Map<K, number> {
286+
increment(key: K): void {
287+
this.set(key, (this.get(key) ?? 0) + 1)
288+
}
289+
290+
total(): number {
291+
return this.values().reduce((x, y) => x + y, 0)
292+
}
293+
}

0 commit comments

Comments
 (0)