Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/improve-error-message-baseline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@wdio/image-comparison-core": patch
"@wdio/visual-service": patch
---

# 🐛 Bugfixes

## #1098 Improve error message when baseline is missing and both flags are false

When `autoSaveBaseline = false` and `alwaysSaveActualImage = false` and a baseline image doesn't exist, the error message now provides clear guidance suggesting users set `alwaysSaveActualImage` to `true` if they need the actual image to create a baseline manually.

# Committers: 1

- Wim Selles ([@wswebcreation](https://github.com/wswebcreation))
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ vi.mock('./images.js', async () => {
}
})

import { executeImageCompare } from './images.js'
import { executeImageCompare, checkBaselineImageExists } from './images.js'
import * as images from './images.js'

describe('executeImageCompare', () => {
Expand Down Expand Up @@ -1091,4 +1091,46 @@ describe('executeImageCompare', () => {
expect(images.saveBase64Image).not.toHaveBeenCalled()
expect(fsPromises.writeFile).not.toHaveBeenCalledWith('/mock/actual/test.png', expect.anything())
})

it('should not save actual image when baseline does not exist, alwaysSaveActualImage is false, and autoSaveBaseline is false', async () => {
// This test covers issue #1098: When both flags are false, we respect the user's choice
// and provide a helpful error message suggesting to adjust the arguments if needed
const base64Image = Buffer.from('base64-image').toString('base64')
const optionsWithoutAutoSave = {
...mockOptions,
folderOptions: {
...mockOptions.folderOptions,
alwaysSaveActualImage: false,
autoSaveBaseline: false,
}
}

vi.mocked(fsPromises.access).mockImplementation(async (path: any) => {
if (path === '/mock/baseline/test.png' || path === '/mock/actual/test.png') {
throw new Error('File not found')
}
return undefined
})

vi.mocked(images.checkBaselineImageExists).mockImplementation(checkBaselineImageExists)

vi.mocked(compareImages.default).mockResolvedValue({
rawMisMatchPercentage: 0,
misMatchPercentage: 0,
getBuffer: vi.fn().mockResolvedValue(Buffer.from('diff-image-data')),
diffBounds: { left: 0, top: 0, right: 0, bottom: 0 },
analysisTime: 10,
diffPixels: []
})

await expect(executeImageCompare({
isViewPortScreenshot: true,
isNativeContext: false,
options: optionsWithoutAutoSave,
testContext: mockTestContext,
actualBase64Image: base64Image,
})).rejects.toThrow(/If you need the actual image to create a baseline, please set alwaysSaveActualImage to true/)

expect(images.saveBase64Image).not.toHaveBeenCalled()
})
})
6 changes: 3 additions & 3 deletions packages/image-comparison-core/src/methods/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ export async function checkBaselineImageExists({
const actualFileExists = await checkIfImageExists(actualFilePath)
const filePathMessage = actualFileExists
? `The image can be found here:\n${actualFilePath}`
: 'The actual image was not saved to disk (alwaysSaveActualImage is false).'
: 'The actual image was not saved to disk (alwaysSaveActualImage is false).\nIf you need the actual image to create a baseline, please set alwaysSaveActualImage to true.'
throw new Error(
`
#####################################################################################
Baseline image not found, save the actual image manually to the baseline.
${filePathMessage}
Baseline image not found, save the actual image manually to the baseline.
${filePathMessage}
#####################################################################################`,
)
}
Expand Down