Skip to content

Commit

Permalink
feat: add INLINE_ASSETS config option
Browse files Browse the repository at this point in the history
  • Loading branch information
kien-ht committed May 28, 2024
1 parent 08e28f5 commit 7a35b92
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export default {
OVERWRITE: true,
},
CYPRESS_SCREENSHOT_OPTIONS: {},
INLINE_ASSETS: false
}
25 changes: 21 additions & 4 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 7a35b92

Please sign in to comment.