-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split some
jest-snapshot
utility functions to its own package…
… `@jest/snapshot-utils` (#15095)
- Loading branch information
1 parent
b7ae0b8
commit 7a2ccc2
Showing
19 changed files
with
445 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "@jest/snapshot-utils", | ||
"version": "30.0.0-alpha.4", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jestjs/jest.git", | ||
"directory": "packages/jest-snapshot-utils" | ||
}, | ||
"license": "MIT", | ||
"main": "./build/index.js", | ||
"types": "./build/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./build/index.d.ts", | ||
"require": "./build/index.js", | ||
"import": "./build/index.mjs", | ||
"default": "./build/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"dependencies": { | ||
"@jest/types": "workspace:*", | ||
"chalk": "^4.0.0", | ||
"graceful-fs": "^4.2.9", | ||
"natural-compare": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"@types/graceful-fs": "^4.1.3", | ||
"@types/natural-compare": "^1.4.0" | ||
}, | ||
"engines": { | ||
"node": "^16.10.0 || ^18.12.0 || >=20.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
packages/jest-snapshot-utils/src/__tests__/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
jest.mock('graceful-fs', () => ({ | ||
...jest.createMockFromModule<typeof import('fs')>('fs'), | ||
existsSync: jest.fn().mockReturnValue(true), | ||
})); | ||
|
||
import * as path from 'path'; | ||
import chalk = require('chalk'); | ||
import * as fs from 'graceful-fs'; | ||
import { | ||
SNAPSHOT_GUIDE_LINK, | ||
SNAPSHOT_VERSION, | ||
SNAPSHOT_VERSION_WARNING, | ||
getSnapshotData, | ||
keyToTestName, | ||
saveSnapshotFile, | ||
testNameToKey, | ||
} from '../utils'; | ||
|
||
test('keyToTestName()', () => { | ||
expect(keyToTestName('abc cde 12')).toBe('abc cde'); | ||
expect(keyToTestName('abc cde 12')).toBe('abc cde '); | ||
expect(() => keyToTestName('abc cde')).toThrow( | ||
'Snapshot keys must end with a number.', | ||
); | ||
}); | ||
|
||
test('testNameToKey', () => { | ||
expect(testNameToKey('abc cde', 1)).toBe('abc cde 1'); | ||
expect(testNameToKey('abc cde ', 12)).toBe('abc cde 12'); | ||
}); | ||
|
||
test('saveSnapshotFile() works with \r\n', () => { | ||
const filename = path.join(__dirname, 'remove-newlines.snap'); | ||
const data = { | ||
myKey: '<div>\r\n</div>', | ||
}; | ||
|
||
saveSnapshotFile(data, filename); | ||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
filename, | ||
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` + | ||
'exports[`myKey`] = `<div>\n</div>`;\n', | ||
); | ||
}); | ||
|
||
test('saveSnapshotFile() works with \r', () => { | ||
const filename = path.join(__dirname, 'remove-newlines.snap'); | ||
const data = { | ||
myKey: '<div>\r</div>', | ||
}; | ||
|
||
saveSnapshotFile(data, filename); | ||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
filename, | ||
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` + | ||
'exports[`myKey`] = `<div>\n</div>`;\n', | ||
); | ||
}); | ||
|
||
test('getSnapshotData() throws when no snapshot version', () => { | ||
const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
jest | ||
.mocked(fs.readFileSync) | ||
.mockReturnValue('exports[`myKey`] = `<div>\n</div>`;\n'); | ||
const update = 'none'; | ||
|
||
expect(() => getSnapshotData(filename, update)).toThrow( | ||
chalk.red( | ||
`${chalk.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, | ||
); | ||
}); | ||
|
||
test('getSnapshotData() throws for older snapshot version', () => { | ||
const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
jest | ||
.mocked(fs.readFileSync) | ||
.mockReturnValue( | ||
`// Jest Snapshot v0.99, ${SNAPSHOT_GUIDE_LINK}\n\n` + | ||
'exports[`myKey`] = `<div>\n</div>`;\n', | ||
); | ||
const update = 'none'; | ||
|
||
expect(() => getSnapshotData(filename, update)).toThrow( | ||
`${chalk.red( | ||
`${chalk.red.bold('Outdated snapshot')}: The version of the snapshot ` + | ||
'file associated with this test is outdated. The snapshot file ' + | ||
'version ensures that all developers on a project are using ' + | ||
'the same version of Jest. ' + | ||
'Please update all snapshots during this upgrade of Jest.', | ||
)}\n\nExpected: v${SNAPSHOT_VERSION}\n` + | ||
`Received: v0.99\n\n${SNAPSHOT_VERSION_WARNING}`, | ||
); | ||
}); | ||
|
||
test('getSnapshotData() throws for newer snapshot version', () => { | ||
const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
jest | ||
.mocked(fs.readFileSync) | ||
.mockReturnValue( | ||
`// Jest Snapshot v2, ${SNAPSHOT_GUIDE_LINK}\n\n` + | ||
'exports[`myKey`] = `<div>\n</div>`;\n', | ||
); | ||
const update = 'none'; | ||
|
||
expect(() => getSnapshotData(filename, update)).toThrow( | ||
`${chalk.red( | ||
`${chalk.red.bold('Outdated Jest version')}: The version of this ` + | ||
'snapshot file indicates that this project is meant to be used ' + | ||
'with a newer version of Jest. ' + | ||
'The snapshot file version ensures that all developers on a project ' + | ||
'are using the same version of Jest. ' + | ||
'Please update your version of Jest and re-run the tests.', | ||
)}\n\nExpected: v${SNAPSHOT_VERSION}\nReceived: v2`, | ||
); | ||
}); | ||
|
||
test('getSnapshotData() does not throw for when updating', () => { | ||
const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
jest | ||
.mocked(fs.readFileSync) | ||
.mockReturnValue('exports[`myKey`] = `<div>\n</div>`;\n'); | ||
const update = 'all'; | ||
|
||
expect(() => getSnapshotData(filename, update)).not.toThrow(); | ||
}); | ||
|
||
test('getSnapshotData() marks invalid snapshot dirty when updating', () => { | ||
const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
jest | ||
.mocked(fs.readFileSync) | ||
.mockReturnValue('exports[`myKey`] = `<div>\n</div>`;\n'); | ||
const update = 'all'; | ||
|
||
expect(getSnapshotData(filename, update)).toMatchObject({dirty: true}); | ||
}); | ||
|
||
test('getSnapshotData() marks valid snapshot not dirty when updating', () => { | ||
const filename = path.join(__dirname, 'old-snapshot.snap'); | ||
jest | ||
.mocked(fs.readFileSync) | ||
.mockReturnValue( | ||
`// Jest Snapshot v${SNAPSHOT_VERSION}, ${SNAPSHOT_GUIDE_LINK}\n\n` + | ||
'exports[`myKey`] = `<div>\n</div>`;\n', | ||
); | ||
const update = 'all'; | ||
|
||
expect(getSnapshotData(filename, update)).toMatchObject({dirty: false}); | ||
}); | ||
|
||
test('escaping', () => { | ||
const filename = path.join(__dirname, 'escaping.snap'); | ||
const data = '"\'\\'; | ||
const writeFileSync = jest.mocked(fs.writeFileSync); | ||
|
||
writeFileSync.mockReset(); | ||
saveSnapshotFile({key: data}, filename); | ||
const writtenData = writeFileSync.mock.calls[0][1]; | ||
expect(writtenData).toBe( | ||
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` + | ||
'exports[`key`] = `"\'\\\\`;\n', | ||
); | ||
|
||
// eslint-disable-next-line no-eval | ||
const readData = eval(`var exports = {}; ${writtenData} exports`); | ||
expect(readData).toEqual({key: data}); | ||
const snapshotData = readData.key; | ||
expect(data).toEqual(snapshotData); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export * from './utils'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export type SnapshotData = Record<string, string>; |
Oops, something went wrong.