(actual: ImageDataLike, imageFilename: string)
| 12 | }; |
| 13 | |
| 14 | export function assertMatchesImage(actual: ImageDataLike, imageFilename: string): void { |
| 15 | if (!imageFilename.endsWith('.png')) { |
| 16 | console.log('throwing'); |
| 17 | throw new Error('imageFilename must end in .png'); |
| 18 | } |
| 19 | let fileBuffer; |
| 20 | try { |
| 21 | fileBuffer = fs.readFileSync(path.join(snapshotsPath, imageFilename)); |
| 22 | } |
| 23 | catch (_) { |
| 24 | writeImageSnapshot(actual, imageFilename); |
| 25 | return; |
| 26 | } |
| 27 | const png = PNG.sync.read(fileBuffer); |
| 28 | if (actual.data.length * 4 !== png.data.length) { |
| 29 | throw new Error('Expected actual.length to match png.data.length'); |
| 30 | } |
| 31 | const identical = actual.data.every((value, i) => value == png.data[i * 4]); |
| 32 | if (!identical) { |
| 33 | console.log(png.data); |
| 34 | writeImageSnapshot(actual, imageFilename.replace('.png', '.error.png')); |
| 35 | throw new Error('expected data to be identitcal'); |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | |
| 40 | function writeImageSnapshot(actual: ImageDataLike, imageFilename: string) { |
| 41 | const png = new PNG({ |
no test coverage detected