jestjs/jest · error · Error
Snapshot keys must end with a number.
Error message
Snapshot keys must end with a number.
What it means
Thrown by `keyToTestName` in @jest/snapshot-utils (utils.ts:125-128) when a snapshot file key does not end with ` <number>`. Every snapshot key is built by `testNameToKey` as `${normalizedTestName} ${count}` (utils.ts:122-123); the trailing count is what lets `keyToTestName` reverse the mapping. A key without it means the snapshot file is corrupt or hand-edited.
Source
Thrown at packages/jest-snapshot-utils/src/utils.ts:127
key.replaceAll(/\\r\\n|\\r|\\n/g, match => {
switch (match) {
case '\\r\\n':
return '\r\n';
case '\\r':
return '\r';
case '\\n':
return '\n';
default:
return match;
}
});
export const testNameToKey = (testName: string, count: number): string =>
`${normalizeTestNameForKey(testName)} ${count}`;
export const keyToTestName = (key: string): string => {
if (!/ \d+$/.test(key)) {
throw new Error('Snapshot keys must end with a number.');
}
const testNameWithoutCount = key.replace(/ \d+$/, '');
return denormalizeTestNameFromKey(testNameWithoutCount);
};
export const getSnapshotData = (
snapshotPath: string,
update: Config.SnapshotUpdateState,
): {
data: SnapshotData;
dirty: boolean;
} => {
const data = Object.create(null);
let snapshotContents = '';
let dirty = false;
if (fs.existsSync(snapshotPath)) {
try {View on GitHub (pinned to f49721c78e)
Solutions
- Delete the offending snapshot file and re-run with `--ci=false` (or `-u`) so Jest regenerates valid keys.
- Fix the key by appending ` <n>` (e.g. `'my test' 1`) if you must edit by hand.
- Stop hand-editing `*.snap` files; treat them as generated artifacts.
Example fix
// before — snapshots/my.test.snap exports['my test'] = `"value"`; // after exports['my test 1'] = `"value"`;
Defensive patterns
Strategy: try-catch
Validate before calling
function isKeyValid(key: string): boolean {
return / \d+$/.test(key);
}
Object.keys(snapshotData).forEach(k => {
if (!isKeyValid(k)) throw new Error(`Corrupt snapshot key: ${k}`);
}); Type guard
function isValidSnapshotKey(key: unknown): key is string {
return typeof key === 'string' && / \d+$/.test(key);
} Try / catch
try {
keyToTestName(k);
} catch (e) {
if (e instanceof Error && e.message === 'Snapshot keys must end with a number.') {
// delete the snapshot file and regenerate with -u
} else throw e;
} Prevention
- Never edit *.snap files by hand; regenerate them via the test runner.
- Add *.snap files to .gitattributes as binary-ish to discourage merge conflicts.
- Run `jest --ci` in CI to catch corrupt snapshots before they ship.
When it happens
Trigger: A `*.snap` file with a key like `exports['my test'] = ...` (missing the trailing ` 1`). Calling `keyToTestName('badKey')` directly. Snapshot files written by non-Jest tooling.
Common situations: Manual edits to `*.snap` files. Merge conflicts in snapshots that dropped the count. Custom serializers/reporters that wrote keys incorrectly.
Related errors
- ${matcherHint(...)} Expected properties must be an object ${
- ${matcherHint(...)} Expected properties must be an object ${
- ${matcherHint(...)} Inline snapshot must be a string ${print
- ${matcherHintFromConfig(...)} Snapshot matchers cannot be u
- ${matcherHintFromConfig(...)} Snapshot state must be initia
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/f65b20dadb390acc.json.
Report an issue: GitHub.