* Matches the given snapshot with the contained one. * @param name The name of the snapshot * @param value The raw value for which to create the snapshot * @returns An error message if there was a match error, if they match null
(name: string, value: unknown)
| 981 | * @returns An error message if there was a match error, if they match null |
| 982 | */ |
| 983 | public match(name: string, value: unknown): string | null { |
| 984 | const expected = this.snapshots.get(name)?.split('\n'); |
| 985 | if (expected === undefined) { |
| 986 | return `No snapshot '${name}' found`; |
| 987 | } |
| 988 | |
| 989 | // https://github.com/jestjs/jest/blob/8e683abe2a1d3f6f6513dd9467f0f49d3d2ffc0d/packages/jest-snapshot-utils/src/utils.ts#L190C51-L190C70 |
| 990 | const actual = SnapshotFile._printBacktickString(PrettyFormat.format(value, SnapshotFile._matchOptions)).split( |
| 991 | '\n' |
| 992 | ); |
| 993 | |
| 994 | const lines = Math.min(expected.length, actual.length); |
| 995 | const errors: string[] = []; |
| 996 | |
| 997 | if (expected.length !== actual.length) { |
| 998 | errors.push(`Expected ${expected.length} lines, but only got ${actual.length}`); |
| 999 | } |
| 1000 | |
| 1001 | for (let i = 0; i < lines; i++) { |
| 1002 | if (actual[i].trimEnd() !== expected[i].trimEnd()) { |
| 1003 | errors.push(`Error on line ${i + 1}: `); |
| 1004 | errors.push(`+ ${actual[i]}`); |
| 1005 | errors.push(`- ${expected[i]}`); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | if (errors.length > 0) { |
| 1010 | return errors.join('\n'); |
| 1011 | } |
| 1012 | return null; |
| 1013 | } |
| 1014 | |
| 1015 | // https://github.com/jestjs/jest/blob/8e683abe2a1d3f6f6513dd9467f0f49d3d2ffc0d/packages/jest-snapshot-utils/src/utils.ts#L167-L171 |
| 1016 | private static _printBacktickString(str: string) { |