@@ -53,6 +53,7 @@ export default class SnapshotState {
53
53
private _snapshotData : SnapshotData
54
54
private _initialData : SnapshotData
55
55
private _inlineSnapshots : Array < InlineSnapshot >
56
+ private _inlineSnapshotStacks : Array < ParsedStack >
56
57
private _rawSnapshots : Array < RawSnapshot >
57
58
private _uncheckedKeys : Set < string >
58
59
private _snapshotFormat : PrettyFormatOptions
@@ -77,6 +78,7 @@ export default class SnapshotState {
77
78
this . _snapshotData = data
78
79
this . _dirty = dirty
79
80
this . _inlineSnapshots = [ ]
81
+ this . _inlineSnapshotStacks = [ ]
80
82
this . _rawSnapshots = [ ]
81
83
this . _uncheckedKeys = new Set ( Object . keys ( this . _snapshotData ) )
82
84
this . _counters = new Map ( )
@@ -136,38 +138,13 @@ export default class SnapshotState {
136
138
private _addSnapshot (
137
139
key : string ,
138
140
receivedSerialized : string ,
139
- options : { isInline : boolean ; rawSnapshot ?: RawSnapshotInfo ; error ?: Error } ,
141
+ options : { rawSnapshot ?: RawSnapshotInfo ; stack ?: ParsedStack } ,
140
142
) : void {
141
143
this . _dirty = true
142
- if ( options . isInline ) {
143
- const error = options . error || new Error ( 'snapshot' )
144
- const stacks = parseErrorStacktrace (
145
- error ,
146
- { ignoreStackEntries : [ ] } ,
147
- )
148
- const _stack = this . _inferInlineSnapshotStack ( stacks )
149
- if ( ! _stack ) {
150
- throw new Error (
151
- `@vitest/snapshot: Couldn't infer stack frame for inline snapshot.\n${ JSON . stringify (
152
- stacks ,
153
- ) } `,
154
- )
155
- }
156
- const stack = this . environment . processStackTrace ?.( _stack ) || _stack
157
- // removing 1 column, because source map points to the wrong
158
- // location for js files, but `column-1` points to the same in both js/ts
159
- // https://github.com/vitejs/vite/issues/8657
160
- stack . column --
161
- // reject multiple inline snapshots at the same location
162
- const duplicateIndex = this . _inlineSnapshots . findIndex ( s => s . file === stack . file && s . line === stack . line && s . column === stack . column )
163
- if ( duplicateIndex >= 0 ) {
164
- // remove the first one to avoid updating an inline snapshot
165
- this . _inlineSnapshots . splice ( duplicateIndex , 1 )
166
- throw new Error ( 'toMatchInlineSnapshot cannot be called multiple times at the same location.' )
167
- }
144
+ if ( options . stack ) {
168
145
this . _inlineSnapshots . push ( {
169
146
snapshot : receivedSerialized ,
170
- ...stack ,
147
+ ...options . stack ,
171
148
} )
172
149
}
173
150
else if ( options . rawSnapshot ) {
@@ -316,6 +293,36 @@ export default class SnapshotState {
316
293
this . _snapshotData [ key ] = receivedSerialized
317
294
}
318
295
296
+ // find call site of toMatchInlineSnapshot
297
+ let stack : ParsedStack | undefined
298
+ if ( isInline ) {
299
+ const stacks = parseErrorStacktrace (
300
+ error || new Error ( 'snapshot' ) ,
301
+ { ignoreStackEntries : [ ] } ,
302
+ )
303
+ const _stack = this . _inferInlineSnapshotStack ( stacks )
304
+ if ( ! _stack ) {
305
+ throw new Error (
306
+ `@vitest/snapshot: Couldn't infer stack frame for inline snapshot.\n${ JSON . stringify (
307
+ stacks ,
308
+ ) } `,
309
+ )
310
+ }
311
+ stack = this . environment . processStackTrace ?.( _stack ) || _stack
312
+ // removing 1 column, because source map points to the wrong
313
+ // location for js files, but `column-1` points to the same in both js/ts
314
+ // https://github.com/vitejs/vite/issues/8657
315
+ stack . column --
316
+
317
+ // reject multiple inline snapshots at the same location
318
+ if ( this . _inlineSnapshotStacks . some ( s => s . file === stack ! . file && s . line === stack ! . line && s . column === stack ! . column ) ) {
319
+ // remove already succeeded snapshot
320
+ this . _inlineSnapshots = this . _inlineSnapshots . filter ( s => ! ( s . file === stack ! . file && s . line === stack ! . line && s . column === stack ! . column ) )
321
+ throw new Error ( 'toMatchInlineSnapshot cannot be called multiple times at the same location.' )
322
+ }
323
+ this . _inlineSnapshotStacks . push ( stack )
324
+ }
325
+
319
326
// These are the conditions on when to write snapshots:
320
327
// * There's no snapshot file in a non-CI environment.
321
328
// * There is a snapshot file and we decided to update the snapshot.
@@ -338,8 +345,7 @@ export default class SnapshotState {
338
345
}
339
346
340
347
this . _addSnapshot ( key , receivedSerialized , {
341
- error,
342
- isInline,
348
+ stack,
343
349
rawSnapshot,
344
350
} )
345
351
}
@@ -349,8 +355,7 @@ export default class SnapshotState {
349
355
}
350
356
else {
351
357
this . _addSnapshot ( key , receivedSerialized , {
352
- error,
353
- isInline,
358
+ stack,
354
359
rawSnapshot,
355
360
} )
356
361
this . added ++
0 commit comments