diff --git a/src/image.utils.ts b/src/image.utils.ts index 4f5af2e2..b87a5be0 100644 --- a/src/image.utils.ts +++ b/src/image.utils.ts @@ -10,7 +10,7 @@ import { METADATA_KEY } from "./constants"; type PluginMetadata = { version: string; - testingType?: 'e2e' | 'component'; + testingType?: "e2e" | "component"; }; type PluginMetadataConfig = { @@ -18,7 +18,14 @@ type PluginMetadataConfig = { }; export const addPNGMetadata = (config: PluginMetadataConfig, png: Buffer) => - addMetadata(png, METADATA_KEY, JSON.stringify({ version, testingType: config.testingType || 'e2e' } as PluginMetadata) /* c8 ignore next */); + addMetadata( + png, + METADATA_KEY, + JSON.stringify({ + version, + testingType: config.testingType || "e2e", + } as PluginMetadata) /* c8 ignore next */ + ); export const getPNGMetadata = (png: Buffer): PluginMetadata | undefined => { const metadataString = getMetadata(png, METADATA_KEY /* c8 ignore next */); @@ -28,18 +35,30 @@ export const getPNGMetadata = (png: Buffer): PluginMetadata | undefined => { } catch { return { version: metadataString }; } -} +}; export const isImageCurrentVersion = (png: Buffer) => getPNGMetadata(png)?.version === version; export const isImageGeneratedByPlugin = (png: Buffer) => !!getPNGMetadata(png /* c8 ignore next */); -export const isImageOfTestType = (png: Buffer, testingType?: PluginMetadataConfig['testingType']) => { +export const isImageOfTestType = ( + png: Buffer, + testingType?: PluginMetadataConfig["testingType"] +) => { if (!isImageGeneratedByPlugin(png)) return false; - const imageTestingType = getPNGMetadata(png /* c8 ignore next */)?.testingType; - return imageTestingType === testingType || testingType === imageTestingType === undefined; + const imageTestingType = getPNGMetadata( + png /* c8 ignore next */ + )?.testingType; + return ( + imageTestingType === testingType || + (testingType === imageTestingType) === undefined + ); }; -export const writePNG = (config: PluginMetadataConfig, name: string, png: PNG | Buffer) => +export const writePNG = ( + config: PluginMetadataConfig, + name: string, + png: PNG | Buffer +) => fs.writeFileSync( name, addPNGMetadata(config, png instanceof PNG ? PNG.sync.write(png) : png) @@ -117,7 +136,9 @@ export const alignImagesToSameSize = ( ]; }; -export const cleanupUnused = (config: PluginMetadataConfig & { projectRoot: string; }) => { +export const cleanupUnused = ( + config: PluginMetadataConfig & { projectRoot: string } +) => { glob .sync("**/*.png", { cwd: config.projectRoot, diff --git a/src/task.hook.test.ts b/src/task.hook.test.ts index 58cfe5de..ed11d78b 100644 --- a/src/task.hook.test.ts +++ b/src/task.hook.test.ts @@ -118,7 +118,7 @@ describe("cleanupImagesTask", () => { ); }; - describe('when testing type does not match', () => { + describe("when testing type does not match", () => { it("does not remove unused screenshot", async () => { const { path: projectRoot } = await dir(); const screenshotPath = await writeTmpFixture( @@ -129,14 +129,14 @@ describe("cleanupImagesTask", () => { cleanupImagesTask({ projectRoot, env: { pluginVisualRegressionCleanupUnusedImages: true }, - testingType: 'component', + testingType: "component", } as unknown as Cypress.PluginConfigOptions); expect(existsSync(screenshotPath)).toBe(true); }); }); - describe('when testing type matches', () => { + describe("when testing type matches", () => { it("does not remove used screenshot", async () => { const { path: projectRoot } = await dir(); const screenshotPath = await writeTmpFixture( @@ -147,7 +147,7 @@ describe("cleanupImagesTask", () => { cleanupImagesTask({ projectRoot, env: { pluginVisualRegressionCleanupUnusedImages: true }, - testingType: 'e2e', + testingType: "e2e", } as unknown as Cypress.PluginConfigOptions); expect(existsSync(screenshotPath)).toBe(true); @@ -163,7 +163,7 @@ describe("cleanupImagesTask", () => { cleanupImagesTask({ projectRoot, env: { pluginVisualRegressionCleanupUnusedImages: true }, - testingType: 'e2e', + testingType: "e2e", } as unknown as Cypress.PluginConfigOptions); expect(existsSync(screenshotPath)).toBe(false); @@ -205,7 +205,10 @@ describe("compareImagesTask", () => { describe("when old screenshot exists", () => { it("resolves with a success message", async () => expect( - compareImagesTask({ testingType: 'e2e' }, await generateConfig({ updateImages: true })) + compareImagesTask( + { testingType: "e2e" }, + await generateConfig({ updateImages: true }) + ) ).resolves.toEqual({ message: "Image diff factor (0%) is within boundaries of maximum threshold option 0.5.", @@ -224,7 +227,9 @@ describe("compareImagesTask", () => { const cfg = await generateConfig({ updateImages: false }); await fs.unlink(cfg.imgOld); - await expect(compareImagesTask({ testingType: 'e2e' }, cfg)).resolves.toEqual({ + await expect( + compareImagesTask({ testingType: "e2e" }, cfg) + ).resolves.toEqual({ message: "Image diff factor (0%) is within boundaries of maximum threshold option 0.5.", imgDiff: 0, @@ -241,7 +246,9 @@ describe("compareImagesTask", () => { it("resolves with an error message", async () => { const cfg = await generateConfig({ updateImages: false }); - await expect(compareImagesTask({ testingType: 'e2e' }, cfg)).resolves.toMatchSnapshot(); + await expect( + compareImagesTask({ testingType: "e2e" }, cfg) + ).resolves.toMatchSnapshot(); }); }); @@ -250,7 +257,9 @@ describe("compareImagesTask", () => { const cfg = await generateConfig({ updateImages: false }); await writeTmpFixture(cfg.imgNew, oldImgFixture); - await expect(compareImagesTask({ testingType: 'e2e' }, cfg)).resolves.toMatchSnapshot(); + await expect( + compareImagesTask({ testingType: "e2e" }, cfg) + ).resolves.toMatchSnapshot(); }); }); });