Skip to content

Commit

Permalink
Merge pull request #161 from kien-ht/main
Browse files Browse the repository at this point in the history
Feat: add an option to fail a test if its baseline doesn't exist
  • Loading branch information
PippoRaimondi authored Aug 17, 2023
2 parents a6a8c74 + ab5b6af commit f00da61
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Currently supported values in the custom config file:
- ROOT_DIR (value relative to the root of the directory)
- FAILURE_THRESHOLD: must be between 0 and 1, default to 0
- RETRY_OPTIONS: see [retry options](https://www.npmjs.com/package/cypress-recurse#options)
- FAIL_ON_MISSING_BASELINE: a boolean to determine whether to fail a test if its baseline doesn't exist, default to false

> **Note**: In order to make this custom config values effective, remember to return `getCompareSnapshotsPlugin` instance inside function `setupNodeEvents`:
Expand Down
11 changes: 7 additions & 4 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ const compareSnapshotCommand = defaultScreenshotOptions => {
cy.task('deleteScreenshot', { testName })
cy.task('deleteReport', { testName })

// Take a screenshot and copy to baseline if it does not exist
const objToOperateOn = subject ? cy.get(subject) : cy
objToOperateOn
.screenshot(testName, defaultScreenshotOptions)
.task('copyScreenshot', {
const screenshotted = objToOperateOn.screenshot(testName, defaultScreenshotOptions)

if (userConfig.FAIL_ON_MISSING_BASELINE === false) {
// copy to baseline if it does not exist
screenshotted.task('copyScreenshot', {
testName,
})
}

// Compare screenshots
const options = {
testName,
testThreshold,
failOnMissingBaseline: userConfig.FAIL_ON_MISSING_BASELINE
}

return cy.task('compareSnapshotsPlugin', options)
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function getUserConfigFile() {
const DEFAULT_CONFIG = {
ROOT_DIR: '',
FAILURE_THRESHOLD: 0,
RETRY_OPTIONS: {}
RETRY_OPTIONS: {},
FAIL_ON_MISSING_BASELINE: false
}

export const userConfig = { ...DEFAULT_CONFIG, ...getUserConfigFile() }
Expand Down
18 changes: 16 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ const deleteScreenshot = args => {
return true
}

async function compareSnapshotsPlugin(args) {
const baselineImg = await parseImage(paths.image.baseline(args.testName))
const getStatsComparisonAndPopulateDiffIfAny = async (args) => {
let baselineImg
try {
baselineImg = await parseImage(paths.image.baseline(args.testName))
} catch (e) {
return args.failOnMissingBaseline
? { percentage: 1, testFailed: true }
: { percentage: 0, testFailed: false }
}

const comparisonImg = await parseImage(paths.image.comparison(args.testName))
const diff = new PNG({
width: Math.max(comparisonImg.width, baselineImg.width),
Expand Down Expand Up @@ -96,6 +104,12 @@ async function compareSnapshotsPlugin(args) {
diff.pack().pipe(fs.createWriteStream(paths.image.diff(args.testName)))
}

return { percentage, testFailed }
}

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({
status: !testFailed,
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ const renameAndCopyFile = (originalFilePath, newFilePath) => {
const parseImage = async image => {
return new Promise((resolve, reject) => {
const fd = fs.createReadStream(image)
fd.pipe(new PNG())
fd.on('error', (error) => reject(error))
.pipe(new PNG())
// eslint-disable-next-line func-names
.on('parsed', function() {
const that = this
resolve(that)
})
.on('error', (error) => reject(error))
})
}

Expand Down

0 comments on commit f00da61

Please sign in to comment.