@@ -48,14 +48,20 @@ interface SaveStatus {
4848 saved : boolean
4949}
5050
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+
5157export default class SnapshotState {
5258 private _counters = new CounterMap < string > ( )
5359 private _dirty : boolean
5460 private _updateSnapshot : SnapshotUpdateState
5561 private _snapshotData : SnapshotData
5662 private _initialData : SnapshotData
5763 private _inlineSnapshots : Array < InlineSnapshot >
58- private _inlineSnapshotStacks : Array < ParsedStack & { testId : string } >
64+ private _inlineSnapshotStacks : Array < ParsedStack & { testId : string ; snapshot : string } >
5965 private _testIdToKeys = new DefaultMap < string , string [ ] > ( ( ) => [ ] )
6066 private _rawSnapshots : Array < RawSnapshot >
6167 private _uncheckedKeys : Set < string >
@@ -343,13 +349,26 @@ export default class SnapshotState {
343349 // https://github.com/vitejs/vite/issues/8657
344350 stack . column --
345351
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+ }
351370 }
352- this . _inlineSnapshotStacks . push ( { ...stack , testId } )
371+ this . _inlineSnapshotStacks . push ( { ...stack , testId, snapshot : receivedSerialized } )
353372 }
354373
355374 // These are the conditions on when to write snapshots:
0 commit comments