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

Function validateSnapshotHeader

packages/jest-snapshot-utils/src/utils.ts:27–92  ·  view source on GitHub ↗
(snapshotContents: string)

Source from the content-addressed store, hash-verified

25 `// Jest Snapshot v${SNAPSHOT_VERSION}, ${SNAPSHOT_GUIDE_LINK}`;
26
27const validateSnapshotHeader = (snapshotContents: string) => {
28 const headerTest = SNAPSHOT_HEADER_REGEXP.exec(snapshotContents);
29 const version = headerTest && headerTest[1];
30 const guideLink = headerTest && headerTest[2];
31
32 if (!version) {
33 return new Error(
34 chalk.red(
35 `${chalk.bold('Outdated snapshot')}: No snapshot header found. ` +
36 'Jest 19 introduced versioned snapshots to ensure all developers ' +
37 'on a project are using the same version of Jest. ' +
38 'Please update all snapshots during this upgrade of Jest.\n\n',
39 ) + SNAPSHOT_VERSION_WARNING,
40 );
41 }
42
43 if (version < SNAPSHOT_VERSION) {
44 return new Error(
45 // eslint-disable-next-line prefer-template
46 chalk.red(
47 `${chalk.red.bold('Outdated snapshot')}: The version of the snapshot ` +
48 'file associated with this test is outdated. The snapshot file ' +
49 'version ensures that all developers on a project are using ' +
50 'the same version of Jest. ' +
51 'Please update all snapshots during this upgrade of Jest.',
52 ) +
53 '\n\n' +
54 `Expected: v${SNAPSHOT_VERSION}\n` +
55 `Received: v${version}\n\n` +
56 SNAPSHOT_VERSION_WARNING,
57 );
58 }
59
60 if (version > SNAPSHOT_VERSION) {
61 return new Error(
62 // eslint-disable-next-line prefer-template
63 chalk.red(
64 `${chalk.red.bold('Outdated Jest version')}: The version of this ` +
65 'snapshot file indicates that this project is meant to be used ' +
66 'with a newer version of Jest. The snapshot file version ensures ' +
67 'that all developers on a project are using the same version of ' +
68 'Jest. Please update your version of Jest and re-run the tests.',
69 ) +
70 '\n\n' +
71 `Expected: v${SNAPSHOT_VERSION}\n` +
72 `Received: v${version}`,
73 );
74 }
75
76 if (guideLink !== SNAPSHOT_GUIDE_LINK) {
77 return new Error(
78 // eslint-disable-next-line prefer-template
79 chalk.red(
80 `${chalk.red.bold(
81 'Outdated guide link',
82 )}: The snapshot guide link at the top of this snapshot is outdated. ` +
83 'Please update all snapshots during this upgrade of Jest.',
84 ) +

Callers 1

getSnapshotDataFunction · 0.85

Calls 1

execMethod · 0.80

Tested by

no test coverage detected