@@ -23,6 +23,8 @@ import { saveRawSnapshots } from './rawSnapshot'
23
23
24
24
import {
25
25
addExtraLineBreaks ,
26
+ CounterMap ,
27
+ DefaultMap ,
26
28
getSnapshotData ,
27
29
keyToTestName ,
28
30
normalizeNewlines ,
@@ -47,24 +49,24 @@ interface SaveStatus {
47
49
}
48
50
49
51
export default class SnapshotState {
50
- private _counters : Map < string , number >
52
+ private _counters = new CounterMap < string > ( )
51
53
private _dirty : boolean
52
54
private _updateSnapshot : SnapshotUpdateState
53
55
private _snapshotData : SnapshotData
54
56
private _initialData : SnapshotData
55
57
private _inlineSnapshots : Array < InlineSnapshot >
56
- private _inlineSnapshotStacks : Array < ParsedStack >
58
+ private _inlineSnapshotStacks : Array < ParsedStack & { testId : string } >
59
+ private _testIdToKeys = new DefaultMap < string , string [ ] > ( ( ) => [ ] )
57
60
private _rawSnapshots : Array < RawSnapshot >
58
61
private _uncheckedKeys : Set < string >
59
62
private _snapshotFormat : PrettyFormatOptions
60
63
private _environment : SnapshotEnvironment
61
64
private _fileExists : boolean
62
-
63
- 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 > ( )
64
69
expand : boolean
65
- matched : number
66
- unmatched : number
67
- updated : number
68
70
69
71
private constructor (
70
72
public testFilePath : string ,
@@ -74,20 +76,15 @@ export default class SnapshotState {
74
76
) {
75
77
const { data, dirty } = getSnapshotData ( snapshotContent , options )
76
78
this . _fileExists = snapshotContent != null // TODO: update on watch?
77
- this . _initialData = data
78
- this . _snapshotData = data
79
+ this . _initialData = { ... data }
80
+ this . _snapshotData = { ... data }
79
81
this . _dirty = dirty
80
82
this . _inlineSnapshots = [ ]
81
83
this . _inlineSnapshotStacks = [ ]
82
84
this . _rawSnapshots = [ ]
83
85
this . _uncheckedKeys = new Set ( Object . keys ( this . _snapshotData ) )
84
- this . _counters = new Map ( )
85
86
this . expand = options . expand || false
86
- this . added = 0
87
- this . matched = 0
88
- this . unmatched = 0
89
87
this . _updateSnapshot = options . updateSnapshot
90
- this . updated = 0
91
88
this . _snapshotFormat = {
92
89
printBasicPrototype : false ,
93
90
escapeString : false ,
@@ -118,6 +115,31 @@ export default class SnapshotState {
118
115
} )
119
116
}
120
117
118
+ clearTest ( testId : string ) : void {
119
+ // clear inline
120
+ this . _inlineSnapshots = this . _inlineSnapshots . filter ( s => s . testId !== testId )
121
+ this . _inlineSnapshotStacks = this . _inlineSnapshotStacks . filter ( s => s . testId !== testId )
122
+
123
+ // clear file
124
+ for ( const key of this . _testIdToKeys . get ( testId ) ) {
125
+ const name = keyToTestName ( key )
126
+ const count = this . _counters . get ( name )
127
+ if ( count > 0 ) {
128
+ if ( key in this . _snapshotData || key in this . _initialData ) {
129
+ this . _snapshotData [ key ] = this . _initialData [ key ]
130
+ }
131
+ this . _counters . set ( name , count - 1 )
132
+ }
133
+ }
134
+ this . _testIdToKeys . delete ( testId )
135
+
136
+ // clear stats
137
+ this . added . delete ( testId )
138
+ this . updated . delete ( testId )
139
+ this . matched . delete ( testId )
140
+ this . unmatched . delete ( testId )
141
+ }
142
+
121
143
protected _inferInlineSnapshotStack ( stacks : ParsedStack [ ] ) : ParsedStack | null {
122
144
// if called inside resolves/rejects, stacktrace is different
123
145
const promiseIndex = stacks . findIndex ( i =>
@@ -138,12 +160,13 @@ export default class SnapshotState {
138
160
private _addSnapshot (
139
161
key : string ,
140
162
receivedSerialized : string ,
141
- options : { rawSnapshot ?: RawSnapshotInfo ; stack ?: ParsedStack } ,
163
+ options : { rawSnapshot ?: RawSnapshotInfo ; stack ?: ParsedStack ; testId : string } ,
142
164
) : void {
143
165
this . _dirty = true
144
166
if ( options . stack ) {
145
167
this . _inlineSnapshots . push ( {
146
168
snapshot : receivedSerialized ,
169
+ testId : options . testId ,
147
170
...options . stack ,
148
171
} )
149
172
}
@@ -158,17 +181,6 @@ export default class SnapshotState {
158
181
}
159
182
}
160
183
161
- clear ( ) : void {
162
- this . _snapshotData = this . _initialData
163
- // this._inlineSnapshots = []
164
- this . _counters = new Map ( )
165
- this . added = 0
166
- this . matched = 0
167
- this . unmatched = 0
168
- this . updated = 0
169
- this . _dirty = false
170
- }
171
-
172
184
async save ( ) : Promise < SaveStatus > {
173
185
const hasExternalSnapshots = Object . keys ( this . _snapshotData ) . length
174
186
const hasInlineSnapshots = this . _inlineSnapshots . length
@@ -228,6 +240,7 @@ export default class SnapshotState {
228
240
}
229
241
230
242
match ( {
243
+ testId,
231
244
testName,
232
245
received,
233
246
key,
@@ -236,12 +249,14 @@ export default class SnapshotState {
236
249
error,
237
250
rawSnapshot,
238
251
} : SnapshotMatchOptions ) : SnapshotReturnOptions {
239
- this . _counters . set ( testName , ( this . _counters . get ( testName ) || 0 ) + 1 )
240
- const count = Number ( this . _counters . get ( testName ) )
252
+ // this also increments counter for inline snapshots. maybe we shouldn't?
253
+ this . _counters . increment ( testName )
254
+ const count = this . _counters . get ( testName )
241
255
242
256
if ( ! key ) {
243
257
key = testNameToKey ( testName , count )
244
258
}
259
+ this . _testIdToKeys . get ( testId ) . push ( key )
245
260
246
261
// Do not mark the snapshot as "checked" if the snapshot is inline and
247
262
// there's an external snapshot. This way the external snapshot can be
@@ -320,7 +335,7 @@ export default class SnapshotState {
320
335
this . _inlineSnapshots = this . _inlineSnapshots . filter ( s => ! ( s . file === stack ! . file && s . line === stack ! . line && s . column === stack ! . column ) )
321
336
throw new Error ( 'toMatchInlineSnapshot cannot be called multiple times at the same location.' )
322
337
}
323
- this . _inlineSnapshotStacks . push ( stack )
338
+ this . _inlineSnapshotStacks . push ( { ... stack , testId } )
324
339
}
325
340
326
341
// These are the conditions on when to write snapshots:
@@ -338,27 +353,29 @@ export default class SnapshotState {
338
353
if ( this . _updateSnapshot === 'all' ) {
339
354
if ( ! pass ) {
340
355
if ( hasSnapshot ) {
341
- this . updated ++
356
+ this . updated . increment ( testId )
342
357
}
343
358
else {
344
- this . added ++
359
+ this . added . increment ( testId )
345
360
}
346
361
347
362
this . _addSnapshot ( key , receivedSerialized , {
348
363
stack,
364
+ testId,
349
365
rawSnapshot,
350
366
} )
351
367
}
352
368
else {
353
- this . matched ++
369
+ this . matched . increment ( testId )
354
370
}
355
371
}
356
372
else {
357
373
this . _addSnapshot ( key , receivedSerialized , {
358
374
stack,
375
+ testId,
359
376
rawSnapshot,
360
377
} )
361
- this . added ++
378
+ this . added . increment ( testId )
362
379
}
363
380
364
381
return {
@@ -371,7 +388,7 @@ export default class SnapshotState {
371
388
}
372
389
else {
373
390
if ( ! pass ) {
374
- this . unmatched ++
391
+ this . unmatched . increment ( testId )
375
392
return {
376
393
actual : removeExtraLineBreaks ( receivedSerialized ) ,
377
394
count,
@@ -384,7 +401,7 @@ export default class SnapshotState {
384
401
}
385
402
}
386
403
else {
387
- this . matched ++
404
+ this . matched . increment ( testId )
388
405
return {
389
406
actual : '' ,
390
407
count,
@@ -415,10 +432,10 @@ export default class SnapshotState {
415
432
416
433
const status = await this . save ( )
417
434
snapshot . fileDeleted = status . deleted
418
- snapshot . added = this . added
419
- snapshot . matched = this . matched
420
- snapshot . unmatched = this . unmatched
421
- snapshot . updated = this . updated
435
+ snapshot . added = this . added . total ( )
436
+ snapshot . matched = this . matched . total ( )
437
+ snapshot . unmatched = this . unmatched . total ( )
438
+ snapshot . updated = this . updated . total ( )
422
439
snapshot . unchecked = ! status . deleted ? uncheckedCount : 0
423
440
snapshot . uncheckedKeys = Array . from ( uncheckedKeys )
424
441
0 commit comments