From e9a8fc944c28b58741ff8b95f96807e18395acd2 Mon Sep 17 00:00:00 2001 From: Tanay Vardhan Date: Mon, 29 Jan 2024 17:58:13 -0500 Subject: [PATCH] fix: Add support for exif jpeg file formats (#29208) --- .../src/matchers/toMatchSnapshot.ts | 2 +- tests/playwright-test/golden.spec.ts | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/playwright/src/matchers/toMatchSnapshot.ts b/packages/playwright/src/matchers/toMatchSnapshot.ts index e551ebf9c1..360d3f6d36 100644 --- a/packages/playwright/src/matchers/toMatchSnapshot.ts +++ b/packages/playwright/src/matchers/toMatchSnapshot.ts @@ -467,7 +467,7 @@ function determineFileExtension(file: string | Buffer): string { return 'txt'; if (compareMagicBytes(file, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return 'png'; - if (compareMagicBytes(file, [0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01])) + if (compareMagicBytes(file, [0xff, 0xd8, 0xff])) return 'jpg'; return 'dat'; } diff --git a/tests/playwright-test/golden.spec.ts b/tests/playwright-test/golden.spec.ts index ae094c27fb..2cba3b0623 100644 --- a/tests/playwright-test/golden.spec.ts +++ b/tests/playwright-test/golden.spec.ts @@ -577,6 +577,31 @@ test('should compare different PNG images', async ({ runInlineTest }, testInfo) expect(fs.existsSync(diffSnapshotArtifactPath)).toBe(true); }); +test('should correctly handle different JPEG image signatures', async ({ runInlineTest }, testInfo) => { + const result = await runInlineTest({ + ...files, + 'a.spec.js': ` + const { test, expect } = require('./helper'); + test('test1', ({}) => { + expect(Buffer.from([0xff, 0xd8, 0xff, 0xe1, 0x00, 0xbc, 0x45, 0x78, 0x69, 0x66, 0x00, 0x00, 0x49])).toMatchSnapshot(); + }); + test('test2', ({}) => { + expect(Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==', 'base64')).toMatchSnapshot(); + }); + test('test3', ({}) => { + expect(Buffer.from('/9j/4AAQSkZJRgABJD+3EOqoh+DYbgljkJTDA0AfvKeYZU/uxcluvipXU7hAGOoguGFv/Tq3/azTyFRJjgsQRp4mu0elkP9IxBh6uj5gpJVpNk9XJdE+51Nk7kUSQSZtPiXYUR2zd7JxzAVMvGFsjQ==', 'base64')).toMatchSnapshot(); + }); + `, + }, { 'update-snapshots': true }); + const expectedTest1ArtifactPath = testInfo.outputPath('a.spec.js-snapshots', 'test1-1.jpg'); + const expectedTest2ArtifactPath = testInfo.outputPath('a.spec.js-snapshots', 'test2-1.png'); + const expectedTest3ArtifactPath = testInfo.outputPath('a.spec.js-snapshots', 'test3-1.jpg'); + expect(result.exitCode).toBe(0); + expect(result.output).toContain(`A snapshot doesn't exist at ${expectedTest1ArtifactPath}, writing actual`); + expect(result.output).toContain(`A snapshot doesn't exist at ${expectedTest2ArtifactPath}, writing actual`); + expect(result.output).toContain(`A snapshot doesn't exist at ${expectedTest3ArtifactPath}, writing actual`); +}); + test('should respect threshold', async ({ runInlineTest }) => { const expected = fs.readFileSync(path.join(__dirname, 'assets/screenshot-canvas-expected.png')); const actual = fs.readFileSync(path.join(__dirname, 'assets/screenshot-canvas-actual.png'));