Skip to content

Commit 340fbe6

Browse files
fix: improve error message when baseline is missing and both flags are false (#1099)
* fix: improve error message when baseline is missing and both flags are false - Improve error message in checkBaselineImageExists to provide guidance when alwaysSaveActualImage is false and baseline doesn't exist - Add helpful message suggesting users set alwaysSaveActualImage to true if they need the actual image to create a baseline - Add test to verify the improved error message - Respect user's explicit configuration choice (both flags set to false) Fixes #1098 * chore: add changeset for improved error message
1 parent e4e5b5c commit 340fbe6

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@wdio/image-comparison-core": patch
3+
"@wdio/visual-service": patch
4+
---
5+
6+
# 🐛 Bugfixes
7+
8+
## #1098 Improve error message when baseline is missing and both flags are false
9+
10+
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.
11+
12+
# Committers: 1
13+
14+
- Wim Selles ([@wswebcreation](https://github.com/wswebcreation))

packages/image-comparison-core/src/methods/images.executeImageCompare.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ vi.mock('./images.js', async () => {
9999
}
100100
})
101101

102-
import { executeImageCompare } from './images.js'
102+
import { executeImageCompare, checkBaselineImageExists } from './images.js'
103103
import * as images from './images.js'
104104

105105
describe('executeImageCompare', () => {
@@ -1091,4 +1091,46 @@ describe('executeImageCompare', () => {
10911091
expect(images.saveBase64Image).not.toHaveBeenCalled()
10921092
expect(fsPromises.writeFile).not.toHaveBeenCalledWith('/mock/actual/test.png', expect.anything())
10931093
})
1094+
1095+
it('should not save actual image when baseline does not exist, alwaysSaveActualImage is false, and autoSaveBaseline is false', async () => {
1096+
// This test covers issue #1098: When both flags are false, we respect the user's choice
1097+
// and provide a helpful error message suggesting to adjust the arguments if needed
1098+
const base64Image = Buffer.from('base64-image').toString('base64')
1099+
const optionsWithoutAutoSave = {
1100+
...mockOptions,
1101+
folderOptions: {
1102+
...mockOptions.folderOptions,
1103+
alwaysSaveActualImage: false,
1104+
autoSaveBaseline: false,
1105+
}
1106+
}
1107+
1108+
vi.mocked(fsPromises.access).mockImplementation(async (path: any) => {
1109+
if (path === '/mock/baseline/test.png' || path === '/mock/actual/test.png') {
1110+
throw new Error('File not found')
1111+
}
1112+
return undefined
1113+
})
1114+
1115+
vi.mocked(images.checkBaselineImageExists).mockImplementation(checkBaselineImageExists)
1116+
1117+
vi.mocked(compareImages.default).mockResolvedValue({
1118+
rawMisMatchPercentage: 0,
1119+
misMatchPercentage: 0,
1120+
getBuffer: vi.fn().mockResolvedValue(Buffer.from('diff-image-data')),
1121+
diffBounds: { left: 0, top: 0, right: 0, bottom: 0 },
1122+
analysisTime: 10,
1123+
diffPixels: []
1124+
})
1125+
1126+
await expect(executeImageCompare({
1127+
isViewPortScreenshot: true,
1128+
isNativeContext: false,
1129+
options: optionsWithoutAutoSave,
1130+
testContext: mockTestContext,
1131+
actualBase64Image: base64Image,
1132+
})).rejects.toThrow(/If you need the actual image to create a baseline, please set alwaysSaveActualImage to true/)
1133+
1134+
expect(images.saveBase64Image).not.toHaveBeenCalled()
1135+
})
10941136
})

packages/image-comparison-core/src/methods/images.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ export async function checkBaselineImageExists({
107107
const actualFileExists = await checkIfImageExists(actualFilePath)
108108
const filePathMessage = actualFileExists
109109
? `The image can be found here:\n${actualFilePath}`
110-
: 'The actual image was not saved to disk (alwaysSaveActualImage is false).'
110+
: '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.'
111111
throw new Error(
112112
`
113113
#####################################################################################
114-
Baseline image not found, save the actual image manually to the baseline.
115-
${filePathMessage}
114+
Baseline image not found, save the actual image manually to the baseline.
115+
${filePathMessage}
116116
#####################################################################################`,
117117
)
118118
}

0 commit comments

Comments
 (0)