@jest/snapshot-utils
Advanced tools
Comparing version
@@ -104,3 +104,27 @@ /*! | ||
}; | ||
const testNameToKey = (testName, count) => `${testName} ${count}`; | ||
const normalizeTestNameForKey = testName => testName.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; | ||
} | ||
}); | ||
const denormalizeTestNameFromKey = key => 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; | ||
} | ||
}); | ||
const testNameToKey = (testName, count) => `${normalizeTestNameForKey(testName)} ${count}`; | ||
exports.testNameToKey = testNameToKey; | ||
@@ -111,3 +135,4 @@ const keyToTestName = key => { | ||
} | ||
return key.replace(/ \d+$/, ''); | ||
const testNameWithoutCount = key.replace(/ \d+$/, ''); | ||
return denormalizeTestNameFromKey(testNameWithoutCount); | ||
}; | ||
@@ -114,0 +139,0 @@ exports.keyToTestName = keyToTestName; |
{ | ||
"name": "@jest/snapshot-utils", | ||
"version": "30.0.1", | ||
"version": "30.0.4", | ||
"repository": { | ||
@@ -37,3 +37,3 @@ "type": "git", | ||
}, | ||
"gitHead": "5ce865b4060189fe74cd486544816c079194a0f7" | ||
"gitHead": "f4296d2bc85c1405f84ddf613a25d0bc3766b7e5" | ||
} |
@@ -29,2 +29,5 @@ /** | ||
expect(keyToTestName('abc cde 12')).toBe('abc cde '); | ||
expect(keyToTestName('test with\\r\\nCRLF 1')).toBe('test with\r\nCRLF'); | ||
expect(keyToTestName('test with\\rCR 1')).toBe('test with\rCR'); | ||
expect(keyToTestName('test with\\nLF 1')).toBe('test with\nLF'); | ||
expect(() => keyToTestName('abc cde')).toThrow( | ||
@@ -40,2 +43,31 @@ 'Snapshot keys must end with a number.', | ||
test('testNameToKey escapes line endings to prevent collisions', () => { | ||
expect(testNameToKey('test with\r\nCRLF', 1)).toBe('test with\\r\\nCRLF 1'); | ||
expect(testNameToKey('test with\rCR', 1)).toBe('test with\\rCR 1'); | ||
expect(testNameToKey('test with\nLF', 1)).toBe('test with\\nLF 1'); | ||
expect(testNameToKey('test\r\n', 1)).not.toBe(testNameToKey('test\r', 1)); | ||
expect(testNameToKey('test\r\n', 1)).not.toBe(testNameToKey('test\n', 1)); | ||
expect(testNameToKey('test\r', 1)).not.toBe(testNameToKey('test\n', 1)); | ||
}); | ||
test('keyToTestName reverses testNameToKey transformation', () => { | ||
const testCases = [ | ||
'simple test', | ||
'test with\r\nCRLF', | ||
'test with\rCR only', | ||
'test with\nLF only', | ||
'mixed\r\nline\rendings\n', | ||
'test\r', | ||
'test\r\n', | ||
'test\n', | ||
]; | ||
for (const testName of testCases) { | ||
const key = testNameToKey(testName, 1); | ||
const recovered = keyToTestName(key); | ||
expect(recovered).toBe(testName); | ||
} | ||
}); | ||
test('saveSnapshotFile() works with \r\n', () => { | ||
@@ -42,0 +74,0 @@ const filename = path.join(__dirname, 'remove-newlines.snap'); |
@@ -78,4 +78,32 @@ /** | ||
const normalizeTestNameForKey = (testName: string): string => | ||
testName.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; | ||
} | ||
}); | ||
const denormalizeTestNameFromKey = (key: string): string => | ||
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 => | ||
`${testName} ${count}`; | ||
`${normalizeTestNameForKey(testName)} ${count}`; | ||
@@ -86,4 +114,4 @@ export const keyToTestName = (key: string): string => { | ||
} | ||
return key.replace(/ \d+$/, ''); | ||
const testNameWithoutCount = key.replace(/ \d+$/, ''); | ||
return denormalizeTestNameFromKey(testNameWithoutCount); | ||
}; | ||
@@ -90,0 +118,0 @@ |
28301
15.45%12
9.09%623
14.73%