From 7a35b928b7ba67cb4df2b1835790acc17a5da3b6 Mon Sep 17 00:00:00 2001 From: KienHT <71423573+kien-ht@users.noreply.github.com> Date: Sat, 4 May 2024 06:41:36 +0000 Subject: [PATCH] feat: add INLINE_ASSETS config option --- src/command.js | 1 + src/config.default.js | 1 + src/plugin.js | 25 +++++++++++++++++++++---- src/utils.js | 9 ++++++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/command.js b/src/command.js index ecbeb79..e131e46 100644 --- a/src/command.js +++ b/src/command.js @@ -63,6 +63,7 @@ const compareSnapshotCommand = () => { failOnMissingBaseline: userConfig.FAIL_ON_MISSING_BASELINE, specFilename: Cypress.spec.name, specPath: Cypress.spec.relative, + inlineAssets: userConfig.INLINE_ASSETS } return cy.task('compareSnapshotsPlugin', options) diff --git a/src/config.default.js b/src/config.default.js index 5030436..a6151f8 100644 --- a/src/config.default.js +++ b/src/config.default.js @@ -11,4 +11,5 @@ export default { OVERWRITE: true, }, CYPRESS_SCREENSHOT_OPTIONS: {}, + INLINE_ASSETS: false } diff --git a/src/plugin.js b/src/plugin.js index 36c25cd..e64bb00 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -12,7 +12,8 @@ import { renameAndMoveFile, renameAndCopyFile, getRelativePathFromCwd, getCleanDate, - writeFileIncrement + writeFileIncrement, + toBase64 } from './utils' import paths, { userConfig } from './config' import TestStatus from './reporter/test-status' @@ -119,7 +120,7 @@ async function compareSnapshotsPlugin(args) { const { percentage, testFailed } = await getStatsComparisonAndPopulateDiffIfAny(args) // Saving test status object to build report if task is triggered - testStatuses.push(new TestStatus({ + let newTest = new TestStatus({ status: !testFailed, name: args.testName, percentage, @@ -128,8 +129,24 @@ async function compareSnapshotsPlugin(args) { specPath: args.specPath, baselinePath: getRelativePathFromCwd(paths.image.baseline(args.testName)), diffPath: getRelativePathFromCwd(paths.image.diff(args.testName)), - comparisonPath: getRelativePathFromCwd(paths.image.comparison(args.testName)), - })) + comparisonPath: getRelativePathFromCwd(paths.image.comparison(args.testName)) + }) + + if (args.inlineAssets) { + const [baselineDataUrl, diffDataUrl, comparisonDataUrl] = await Promise.all([ + toBase64(newTest.baselinePath), + toBase64(newTest.diffPath), + toBase64(newTest.comparisonPath), + ]) + newTest = { + ...newTest, + baselineDataUrl, + diffDataUrl, + comparisonDataUrl + } + } + + testStatuses.push(newTest) return percentage } diff --git a/src/utils.js b/src/utils.js index 56aa625..203bead 100644 --- a/src/utils.js +++ b/src/utils.js @@ -98,4 +98,11 @@ const writeFileIncrement = async (name, data, increment = 1) => { return writeFileIncrement(name, data, increment + 1) } -export { createDir, cleanDir, readDir, parseImage, adjustCanvas, setFilePermission, renameAndMoveFile, renameAndCopyFile, getRelativePathFromCwd, getCleanDate, writeFileIncrement } +const toBase64 = async (relativePath) => { + if (relativePath === '') return '' + const absolutePath = path.join(process.cwd(), relativePath) + const content = await fs.readFile(absolutePath, { encoding: 'base64' }) + return `data:image/png;base64,${content}` +} + +export { createDir, cleanDir, readDir, parseImage, adjustCanvas, setFilePermission, renameAndMoveFile, renameAndCopyFile, getRelativePathFromCwd, getCleanDate, writeFileIncrement, toBase64 }