@@ -48,14 +48,20 @@ interface SaveStatus {
48
48
saved : boolean
49
49
}
50
50
51
+ type ParsedStackPosition = Pick < ParsedStack , 'file' | 'line' | 'column' >
52
+
53
+ function isSameStackPosition ( x : ParsedStackPosition , y : ParsedStackPosition ) {
54
+ return x . file === y . file && x . column === y . column && x . line === y . line
55
+ }
56
+
51
57
export default class SnapshotState {
52
58
private _counters = new CounterMap < string > ( )
53
59
private _dirty : boolean
54
60
private _updateSnapshot : SnapshotUpdateState
55
61
private _snapshotData : SnapshotData
56
62
private _initialData : SnapshotData
57
63
private _inlineSnapshots : Array < InlineSnapshot >
58
- private _inlineSnapshotStacks : Array < ParsedStack & { testId : string } >
64
+ private _inlineSnapshotStacks : Array < ParsedStack & { testId : string ; snapshot : string } >
59
65
private _testIdToKeys = new DefaultMap < string , string [ ] > ( ( ) => [ ] )
60
66
private _rawSnapshots : Array < RawSnapshot >
61
67
private _uncheckedKeys : Set < string >
@@ -343,13 +349,26 @@ export default class SnapshotState {
343
349
// https://github.com/vitejs/vite/issues/8657
344
350
stack . column --
345
351
346
- // reject multiple inline snapshots at the same location
347
- if ( this . _inlineSnapshotStacks . some ( s => s . file === stack ! . file && s . line === stack ! . line && s . column === stack ! . column ) ) {
348
- // remove already succeeded snapshot
349
- this . _inlineSnapshots = this . _inlineSnapshots . filter ( s => ! ( s . file === stack ! . file && s . line === stack ! . line && s . column === stack ! . column ) )
350
- throw new Error ( 'toMatchInlineSnapshot cannot be called multiple times at the same location.' )
352
+ // reject multiple inline snapshots at the same location if snapshot is different
353
+ const snapshotsWithSameStack = this . _inlineSnapshotStacks . filter ( s => isSameStackPosition ( s , stack ! ) )
354
+ if ( snapshotsWithSameStack . length > 0 ) {
355
+ // ensure only one snapshot will be written at the same location
356
+ this . _inlineSnapshots = this . _inlineSnapshots . filter ( s => ! isSameStackPosition ( s , stack ! ) )
357
+
358
+ const differentSnapshot = snapshotsWithSameStack . find ( s => s . snapshot !== receivedSerialized )
359
+ if ( differentSnapshot ) {
360
+ throw Object . assign (
361
+ new Error (
362
+ 'toMatchInlineSnapshot with different snapshots cannot be called at the same location' ,
363
+ ) ,
364
+ {
365
+ actual : receivedSerialized ,
366
+ expected : differentSnapshot . snapshot ,
367
+ } ,
368
+ )
369
+ }
351
370
}
352
- this . _inlineSnapshotStacks . push ( { ...stack , testId } )
371
+ this . _inlineSnapshotStacks . push ( { ...stack , testId, snapshot : receivedSerialized } )
353
372
}
354
373
355
374
// These are the conditions on when to write snapshots:
0 commit comments