| 195 | } |
| 196 | |
| 197 | match({ |
| 198 | testName, |
| 199 | received, |
| 200 | key, |
| 201 | inlineSnapshot, |
| 202 | isInline, |
| 203 | error, |
| 204 | testFailing = false, |
| 205 | }: SnapshotMatchOptions): SnapshotReturnOptions { |
| 206 | this._counters.set(testName, (this._counters.get(testName) || 0) + 1); |
| 207 | const count = Number(this._counters.get(testName)); |
| 208 | |
| 209 | if (!key) { |
| 210 | key = testNameToKey(testName, count); |
| 211 | } |
| 212 | |
| 213 | // Do not mark the snapshot as "checked" if the snapshot is inline and |
| 214 | // there's an external snapshot. This way the external snapshot can be |
| 215 | // removed with `--updateSnapshot`. |
| 216 | if (!(isInline && this._snapshotData[key] !== undefined)) { |
| 217 | this._uncheckedKeys.delete(key); |
| 218 | } |
| 219 | |
| 220 | const receivedSerialized = addExtraLineBreaks( |
| 221 | serialize(received, undefined, this.snapshotFormat), |
| 222 | ); |
| 223 | const expected = isInline ? inlineSnapshot : this._snapshotData[key]; |
| 224 | const pass = expected === receivedSerialized; |
| 225 | const hasSnapshot = expected !== undefined; |
| 226 | const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath); |
| 227 | |
| 228 | if (pass && !isInline) { |
| 229 | // Executing a snapshot file as JavaScript and writing the strings back |
| 230 | // when other snapshots have changed loses the proper escaping for some |
| 231 | // characters. Since we check every snapshot in every test, use the newly |
| 232 | // generated formatted string. |
| 233 | // Note that this is only relevant when a snapshot is added and the dirty |
| 234 | // flag is set. |
| 235 | this._snapshotData[key] = receivedSerialized; |
| 236 | } |
| 237 | |
| 238 | // In pure matching only runs, return the match result while skipping any updates |
| 239 | // reports. |
| 240 | if (testFailing) { |
| 241 | if (hasSnapshot && !isInline) { |
| 242 | // Retain current snapshot values. |
| 243 | this._addSnapshot(key, expected, {error, isInline}); |
| 244 | } |
| 245 | return { |
| 246 | actual: removeExtraLineBreaks(receivedSerialized), |
| 247 | count, |
| 248 | expected: |
| 249 | expected === undefined ? undefined : removeExtraLineBreaks(expected), |
| 250 | key, |
| 251 | pass, |
| 252 | }; |
| 253 | } |
| 254 | |