MCPcopy
hub / github.com/jestjs/jest / match

Method match

packages/jest-snapshot/src/State.ts:197–314  ·  view source on GitHub ↗
({
    testName,
    received,
    key,
    inlineSnapshot,
    isInline,
    error,
    testFailing = false,
  }: SnapshotMatchOptions)

Source from the content-addressed store, hash-verified

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

Callers 15

getOperatorNameFunction · 0.80
stringToBytesFunction · 0.80
formatHandleErrorsFunction · 0.80
findMatchingTestsMethod · 0.80
findMatchingTestsMethod · 0.80
parseDataUriFunction · 0.80
isRecrawlWarningDupeMethod · 0.80
formatPathFunction · 0.80
separateMessageFromStackFunction · 0.80
extractFunction · 0.80
stripFunction · 0.80

Calls 8

_addSnapshotMethod · 0.95
testNameToKeyFunction · 0.90
addExtraLineBreaksFunction · 0.90
serializeFunction · 0.90
removeExtraLineBreaksFunction · 0.90
deleteMethod · 0.80
setMethod · 0.45
getMethod · 0.45

Tested by 7

findMatchingTestsMethod · 0.64
findMatchingTestsMethod · 0.64
fnNameForFunction · 0.64
verifyNumPassedFunction · 0.64
extractMessageFunction · 0.64
numberOfTestRunsFunction · 0.64
numberOfTestRunsFunction · 0.64