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

Function _toMatchSnapshot

packages/jest-snapshot/src/index.ts:277–417  ·  view source on GitHub ↗
(config: MatchSnapshotConfig)

Source from the content-addressed store, hash-verified

275};
276
277const _toMatchSnapshot = (config: MatchSnapshotConfig) => {
278 const {context, hint, inlineSnapshot, isInline, matcherName, properties} =
279 config;
280 let {received} = config;
281
282 /** If a test was ran with `test.failing`. Passed by Jest Circus. */
283 const {testFailing = false} = context;
284
285 if (!testFailing && context.dontThrow) {
286 // Suppress errors while running tests
287 context.dontThrow();
288 }
289
290 const {currentConcurrentTestName, isNot, snapshotState} = context;
291 const currentTestName =
292 currentConcurrentTestName?.() ?? context.currentTestName;
293
294 if (isNot) {
295 throw new Error(
296 matcherErrorMessage(
297 matcherHintFromConfig(config, false),
298 NOT_SNAPSHOT_MATCHERS,
299 ),
300 );
301 }
302
303 if (snapshotState == null) {
304 // Because the state is the problem, this is not a matcher error.
305 // Call generic stringify from jest-matcher-utils package
306 // because uninitialized snapshot state does not need snapshot serializers.
307 throw new Error(
308 `${matcherHintFromConfig(config, false)}\n\n` +
309 'Snapshot state must be initialized' +
310 `\n\n${printWithType('Snapshot state', snapshotState, stringify)}`,
311 );
312 }
313
314 const fullTestName =
315 currentTestName && hint
316 ? `${currentTestName}: ${hint}`
317 : currentTestName || ''; // future BREAKING change: || hint
318
319 if (typeof properties === 'object') {
320 if (typeof received !== 'object' || received === null) {
321 throw new Error(
322 matcherErrorMessage(
323 matcherHintFromConfig(config, false),
324 `${RECEIVED_COLOR(
325 'received',
326 )} value must be an object when the matcher has ${EXPECTED_COLOR(
327 'properties',
328 )}`,
329 printWithType('Received', received, printReceived),
330 ),
331 );
332 }
333
334 const propertyPass = context.equals(received, properties, [

Callers 3

toMatchSnapshotFunction · 0.85
toMatchInlineSnapshotFunction · 0.85

Calls 11

matcherErrorMessageFunction · 0.90
matcherHintFromConfigFunction · 0.90
printWithTypeFunction · 0.90
deepMergeFunction · 0.90
printSnapshotAndReceivedFunction · 0.90
printSnapshotNameFunction · 0.85
dontThrowMethod · 0.80
equalsMethod · 0.80
failMethod · 0.80
execMethod · 0.80
matchMethod · 0.80

Tested by

no test coverage detected