@@ -11,6 +11,8 @@ import type { OptionsReceived as PrettyFormatOptions } from 'pretty-format'
11
11
import type { SnapshotData , SnapshotEnvironment , SnapshotMatchOptions , SnapshotResult , SnapshotStateOptions , SnapshotUpdateState } from '../types'
12
12
import type { InlineSnapshot } from './inlineSnapshot'
13
13
import { saveInlineSnapshots } from './inlineSnapshot'
14
+ import type { RawSnapshot , RawSnapshotInfo } from './rawSnapshot'
15
+ import { saveRawSnapshots } from './rawSnapshot'
14
16
15
17
import {
16
18
addExtraLineBreaks ,
@@ -43,6 +45,7 @@ export default class SnapshotState {
43
45
private _snapshotData : SnapshotData
44
46
private _initialData : SnapshotData
45
47
private _inlineSnapshots : Array < InlineSnapshot >
48
+ private _rawSnapshots : Array < RawSnapshot >
46
49
private _uncheckedKeys : Set < string >
47
50
private _snapshotFormat : PrettyFormatOptions
48
51
private _environment : SnapshotEnvironment
@@ -69,6 +72,7 @@ export default class SnapshotState {
69
72
this . _snapshotData = data
70
73
this . _dirty = dirty
71
74
this . _inlineSnapshots = [ ]
75
+ this . _rawSnapshots = [ ]
72
76
this . _uncheckedKeys = new Set ( Object . keys ( this . _snapshotData ) )
73
77
this . _counters = new Map ( )
74
78
this . expand = options . expand || false
@@ -93,6 +97,10 @@ export default class SnapshotState {
93
97
return new SnapshotState ( testFilePath , snapshotPath , content , options )
94
98
}
95
99
100
+ get environment ( ) {
101
+ return this . _environment
102
+ }
103
+
96
104
markSnapshotsAsCheckedForTest ( testName : string ) : void {
97
105
this . _uncheckedKeys . forEach ( ( uncheckedKey ) => {
98
106
if ( keyToTestName ( uncheckedKey ) === testName )
@@ -115,7 +123,7 @@ export default class SnapshotState {
115
123
private _addSnapshot (
116
124
key : string ,
117
125
receivedSerialized : string ,
118
- options : { isInline : boolean ; error ?: Error } ,
126
+ options : { isInline : boolean ; rawSnapshot ?: RawSnapshotInfo ; error ?: Error } ,
119
127
) : void {
120
128
this . _dirty = true
121
129
if ( options . isInline ) {
@@ -135,6 +143,12 @@ export default class SnapshotState {
135
143
...stack ,
136
144
} )
137
145
}
146
+ else if ( options . rawSnapshot ) {
147
+ this . _rawSnapshots . push ( {
148
+ ...options . rawSnapshot ,
149
+ snapshot : receivedSerialized ,
150
+ } )
151
+ }
138
152
else {
139
153
this . _snapshotData [ key ] = receivedSerialized
140
154
}
@@ -154,7 +168,8 @@ export default class SnapshotState {
154
168
async save ( ) : Promise < SaveStatus > {
155
169
const hasExternalSnapshots = Object . keys ( this . _snapshotData ) . length
156
170
const hasInlineSnapshots = this . _inlineSnapshots . length
157
- const isEmpty = ! hasExternalSnapshots && ! hasInlineSnapshots
171
+ const hasRawSnapshots = this . _rawSnapshots . length
172
+ const isEmpty = ! hasExternalSnapshots && ! hasInlineSnapshots && ! hasRawSnapshots
158
173
159
174
const status : SaveStatus = {
160
175
deleted : false ,
@@ -168,6 +183,8 @@ export default class SnapshotState {
168
183
}
169
184
if ( hasInlineSnapshots )
170
185
await saveInlineSnapshots ( this . _environment , this . _inlineSnapshots )
186
+ if ( hasRawSnapshots )
187
+ await saveRawSnapshots ( this . _environment , this . _rawSnapshots )
171
188
172
189
status . saved = true
173
190
}
@@ -206,6 +223,7 @@ export default class SnapshotState {
206
223
inlineSnapshot,
207
224
isInline,
208
225
error,
226
+ rawSnapshot,
209
227
} : SnapshotMatchOptions ) : SnapshotReturnOptions {
210
228
this . _counters . set ( testName , ( this . _counters . get ( testName ) || 0 ) + 1 )
211
229
const count = Number ( this . _counters . get ( testName ) )
@@ -219,14 +237,24 @@ export default class SnapshotState {
219
237
if ( ! ( isInline && this . _snapshotData [ key ] !== undefined ) )
220
238
this . _uncheckedKeys . delete ( key )
221
239
222
- const receivedSerialized = addExtraLineBreaks ( serialize ( received , undefined , this . _snapshotFormat ) )
223
- const expected = isInline ? inlineSnapshot : this . _snapshotData [ key ]
240
+ let receivedSerialized = rawSnapshot && typeof received === 'string'
241
+ ? received as string
242
+ : serialize ( received , undefined , this . _snapshotFormat )
243
+
244
+ if ( ! rawSnapshot )
245
+ receivedSerialized = addExtraLineBreaks ( receivedSerialized )
246
+
247
+ const expected = isInline
248
+ ? inlineSnapshot
249
+ : rawSnapshot
250
+ ? rawSnapshot . content
251
+ : this . _snapshotData [ key ]
224
252
const expectedTrimmed = prepareExpected ( expected )
225
253
const pass = expectedTrimmed === prepareExpected ( receivedSerialized )
226
254
const hasSnapshot = expected !== undefined
227
- const snapshotIsPersisted = isInline || this . _fileExists
255
+ const snapshotIsPersisted = isInline || this . _fileExists || ( rawSnapshot && rawSnapshot . content != null )
228
256
229
- if ( pass && ! isInline ) {
257
+ if ( pass && ! isInline && ! rawSnapshot ) {
230
258
// Executing a snapshot file as JavaScript and writing the strings back
231
259
// when other snapshots have changed loses the proper escaping for some
232
260
// characters. Since we check every snapshot in every test, use the newly
@@ -255,14 +283,14 @@ export default class SnapshotState {
255
283
else
256
284
this . added ++
257
285
258
- this . _addSnapshot ( key , receivedSerialized , { error, isInline } )
286
+ this . _addSnapshot ( key , receivedSerialized , { error, isInline, rawSnapshot } )
259
287
}
260
288
else {
261
289
this . matched ++
262
290
}
263
291
}
264
292
else {
265
- this . _addSnapshot ( key , receivedSerialized , { error, isInline } )
293
+ this . _addSnapshot ( key , receivedSerialized , { error, isInline, rawSnapshot } )
266
294
this . added ++
267
295
}
268
296
0 commit comments