Skip to content

Commit 44bd691

Browse files
committed
fix: fix issue 1085
- added tests
1 parent 56990d4 commit 44bd691

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,14 @@ describe('executeImageCompare', () => {
665665
autoSaveBaseline: true,
666666
}
667667
}
668+
669+
vi.mocked(fsPromises.access).mockImplementation(async (path: any) => {
670+
if (path === '/mock/baseline/test.png') {
671+
throw new Error('File not found')
672+
}
673+
return undefined
674+
})
675+
668676
vi.mocked(compareImages.default).mockResolvedValue({
669677
rawMisMatchPercentage: 0,
670678
misMatchPercentage: 0,
@@ -1043,4 +1051,44 @@ describe('executeImageCompare', () => {
10431051
expect(images.saveBase64Image).not.toHaveBeenCalled()
10441052
expect(log.warn).not.toHaveBeenCalled()
10451053
})
1054+
1055+
it('should not save actual image when autoSaveBaseline is true, alwaysSaveActualImage is false, baseline exists, and comparison passes', async () => {
1056+
// This test covers issue #1085: autoSaveBaseline collides with alwaysSaveActualImage
1057+
// When baseline exists and comparison passes, actual image should NOT be saved
1058+
const base64Image = Buffer.from('base64-image').toString('base64')
1059+
const optionsWithAutoSave = {
1060+
...mockOptions,
1061+
folderOptions: {
1062+
...mockOptions.folderOptions,
1063+
alwaysSaveActualImage: false,
1064+
autoSaveBaseline: true,
1065+
}
1066+
}
1067+
1068+
vi.mocked(fsPromises.access).mockResolvedValue(undefined)
1069+
1070+
vi.mocked(images.checkBaselineImageExists).mockImplementation(async () => {
1071+
return Promise.resolve()
1072+
})
1073+
1074+
vi.mocked(compareImages.default).mockResolvedValue({
1075+
rawMisMatchPercentage: 0,
1076+
misMatchPercentage: 0,
1077+
getBuffer: vi.fn().mockResolvedValue(Buffer.from('diff-image-data')),
1078+
diffBounds: { left: 0, top: 0, right: 0, bottom: 0 },
1079+
analysisTime: 10,
1080+
diffPixels: []
1081+
})
1082+
1083+
await executeImageCompare({
1084+
isViewPortScreenshot: true,
1085+
isNativeContext: false,
1086+
options: optionsWithAutoSave,
1087+
testContext: mockTestContext,
1088+
actualBase64Image: base64Image,
1089+
})
1090+
1091+
expect(images.saveBase64Image).not.toHaveBeenCalled()
1092+
expect(fsPromises.writeFile).not.toHaveBeenCalledWith('/mock/actual/test.png', expect.anything())
1093+
})
10461094
})

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ export async function executeImageCompare(
384384
if (useBase64Image) {
385385
// Convert base64 to buffer for comparison
386386
actualImageBuffer = Buffer.from(actualBase64Image, 'base64')
387-
// Only save if autoSaveBaseline is true (needed to copy to baseline)
388-
if (autoSaveBaseline) {
387+
// Only save actual image if baseline doesn't exist and autoSaveBaseline is true
388+
if (autoSaveBaseline && !(await checkIfImageExists(baselineFilePath))) {
389389
await saveBase64Image(actualBase64Image, actualFilePath)
390390
}
391391
} else {

0 commit comments

Comments
 (0)