@jest/snapshot-utils
Advanced tools
+11
-5
@@ -82,9 +82,10 @@ /*! | ||
| const SNAPSHOT_VERSION = exports.SNAPSHOT_VERSION = '1'; | ||
| const SNAPSHOT_VERSION_REGEXP = /^\/\/ Jest Snapshot v(.+),/; | ||
| const SNAPSHOT_HEADER_REGEXP = /^\/\/ Jest Snapshot v(.+), (.+)\n/; | ||
| const SNAPSHOT_GUIDE_LINK = exports.SNAPSHOT_GUIDE_LINK = 'https://jestjs.io/docs/snapshot-testing'; | ||
| const SNAPSHOT_VERSION_WARNING = exports.SNAPSHOT_VERSION_WARNING = _chalk.default.yellow(`${_chalk.default.bold('Warning')}: Before you upgrade snapshots, ` + 'we recommend that you revert any local changes to tests or other code, ' + 'to ensure that you do not store invalid state.'); | ||
| const writeSnapshotVersion = () => `// Jest Snapshot v${SNAPSHOT_VERSION}, ${SNAPSHOT_GUIDE_LINK}`; | ||
| const validateSnapshotVersion = snapshotContents => { | ||
| const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents); | ||
| const version = versionTest && versionTest[1]; | ||
| const validateSnapshotHeader = snapshotContents => { | ||
| const headerTest = SNAPSHOT_HEADER_REGEXP.exec(snapshotContents); | ||
| const version = headerTest && headerTest[1]; | ||
| const guideLink = headerTest && headerTest[2]; | ||
| if (!version) { | ||
@@ -103,2 +104,7 @@ return new Error(_chalk.default.red(`${_chalk.default.bold('Outdated snapshot')}: No snapshot header found. ` + 'Jest 19 introduced versioned snapshots to ensure all developers ' + 'on a project are using the same version of Jest. ' + 'Please update all snapshots during this upgrade of Jest.\n\n') + SNAPSHOT_VERSION_WARNING); | ||
| } | ||
| if (guideLink !== SNAPSHOT_GUIDE_LINK) { | ||
| return new Error( | ||
| // eslint-disable-next-line prefer-template | ||
| _chalk.default.red(`${_chalk.default.red.bold('Outdated guide link')}: The snapshot guide link is outdated.` + 'Please update all snapshots while upgrading of Jest') + '\n\n' + `Expected: ${SNAPSHOT_GUIDE_LINK}\n` + `Received: ${guideLink}`); | ||
| } | ||
| return null; | ||
@@ -152,3 +158,3 @@ }; | ||
| } | ||
| const validationResult = validateSnapshotVersion(snapshotContents); | ||
| const validationResult = validateSnapshotHeader(snapshotContents); | ||
| const isInvalid = snapshotContents && validationResult; | ||
@@ -155,0 +161,0 @@ if (update === 'none' && isInvalid) { |
+2
-2
| { | ||
| "name": "@jest/snapshot-utils", | ||
| "version": "30.0.5", | ||
| "version": "30.1.0", | ||
| "repository": { | ||
@@ -37,3 +37,3 @@ "type": "git", | ||
| }, | ||
| "gitHead": "22236cf58b66039f81893537c90dee290bab427f" | ||
| "gitHead": "4d5f41d0885c1d9630c81b4fd47f74ab0615e18f" | ||
| } |
@@ -160,2 +160,22 @@ /** | ||
| test('getSnapshotData() throws for deprecated snapshot guide link', () => { | ||
| const deprecatedGuideLink = 'https://goo.gl/fbAQLP'; | ||
| const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
| jest | ||
| .mocked(fs.readFileSync) | ||
| .mockReturnValue( | ||
| `// Jest Snapshot v1, ${deprecatedGuideLink}\n\n` + | ||
| 'exports[`myKey`] = `<div>\n</div>`;\n', | ||
| ); | ||
| const update = 'none'; | ||
| expect(() => getSnapshotData(filename, update)).toThrow( | ||
| `${chalk.red( | ||
| `${chalk.red.bold('Outdated guide link')}: The snapshot guide link is outdated.` + | ||
| 'Please update all snapshots while upgrading of Jest', | ||
| )}\n\nExpected: ${SNAPSHOT_GUIDE_LINK}\n` + | ||
| `Received: ${deprecatedGuideLink}`, | ||
| ); | ||
| }); | ||
| test('getSnapshotData() does not throw for when updating', () => { | ||
@@ -162,0 +182,0 @@ const filename = path.join(__dirname, 'old-snapshot.snap'); |
+19
-5
@@ -16,3 +16,3 @@ /** | ||
| export const SNAPSHOT_VERSION = '1'; | ||
| const SNAPSHOT_VERSION_REGEXP = /^\/\/ Jest Snapshot v(.+),/; | ||
| const SNAPSHOT_HEADER_REGEXP = /^\/\/ Jest Snapshot v(.+), (.+)\n/; | ||
| export const SNAPSHOT_GUIDE_LINK = 'https://jestjs.io/docs/snapshot-testing'; | ||
@@ -28,5 +28,6 @@ export const SNAPSHOT_VERSION_WARNING = chalk.yellow( | ||
| const validateSnapshotVersion = (snapshotContents: string) => { | ||
| const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents); | ||
| const version = versionTest && versionTest[1]; | ||
| const validateSnapshotHeader = (snapshotContents: string) => { | ||
| const headerTest = SNAPSHOT_HEADER_REGEXP.exec(snapshotContents); | ||
| const version = headerTest && headerTest[1]; | ||
| const guideLink = headerTest && headerTest[2]; | ||
@@ -77,2 +78,15 @@ if (!version) { | ||
| if (guideLink !== SNAPSHOT_GUIDE_LINK) { | ||
| return new Error( | ||
| // eslint-disable-next-line prefer-template | ||
| chalk.red( | ||
| `${chalk.red.bold('Outdated guide link')}: The snapshot guide link is outdated.` + | ||
| 'Please update all snapshots while upgrading of Jest', | ||
| ) + | ||
| '\n\n' + | ||
| `Expected: ${SNAPSHOT_GUIDE_LINK}\n` + | ||
| `Received: ${guideLink}`, | ||
| ); | ||
| } | ||
| return null; | ||
@@ -140,3 +154,3 @@ }; | ||
| const validationResult = validateSnapshotVersion(snapshotContents); | ||
| const validationResult = validateSnapshotHeader(snapshotContents); | ||
| const isInvalid = snapshotContents && validationResult; | ||
@@ -143,0 +157,0 @@ |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
29891
5.62%660
5.94%2
100%