Skip to content

Commit

Permalink
Expose resemble options in config file, so it can be tweaked as needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldalsanto committed May 9, 2023
1 parent 9e510d3 commit 4393037
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
11 changes: 2 additions & 9 deletions compare/compareImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ const compareImages = (config, fileName, viewport) => {
const result = await resembleCompare(
referenceImage,
testImage,
{
ignore: 'nothing',
scaleToSameSize: false,
output: {
largeImageThreshold: 0,
transparency: 0.3
}
}
config.resembleOptions
)

if (result.rawMisMatchPercentage > config.threshold) {
await createFolder(`${config.paths.diffImages}/${fileName}`)
const diffImagePath = getFilePath(config.paths.diffImages, fileName, viewport)
fs.writeFileSync(diffImagePath, result.getBuffer())
reject(new Error(`Images are not the same (${result.rawMisMatchPercentage}). See difference at ${diffImagePath}.`))
reject(new Error(`Images are not the same (${result.rawMisMatchPercentage}%). See difference at ${diffImagePath}.`))
} else {
resolve()
}
Expand Down
26 changes: 24 additions & 2 deletions core/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getConfig = () => {
const args = minimist(process.argv)
const configFile = fs.readFileSync(process.cwd() + '/robot-eyes.json')
const userConfig = JSON.parse(configFile)
const config = Object.assign({
const config = mergeDeep({
baseURL: null,
paths: {
testImages: './images/test_images',
Expand All @@ -22,7 +22,14 @@ const getConfig = () => {
timeout: 40000,
headless: true,
threshold: 0.01,
waitForResourceTimeout: 60000
waitForResourceTimeout: 60000,
resembleOptions: {
ignore: 'nothing',
output: {
largeImageThreshold: 0,
transparency: 0.3
}
}
}, userConfig)
Object.keys(config.paths)
.forEach(v => {
Expand All @@ -36,4 +43,19 @@ const getConfig = () => {
return config
}

const mergeDeep = (obj1, obj2) => {
const result = {};
Object.assign(result, obj1);

for (const prop of Object.keys(obj2)) {
if (result.hasOwnProperty(prop) && typeof obj2[prop] === 'object' && !Array.isArray(obj2[prop])) {
result[prop] = mergeDeep(result[prop], obj2[prop]);
} else {
result[prop] = obj2[prop];
}
}

return result;
}

module.exports = getConfig
42 changes: 42 additions & 0 deletions core/tests/getConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,48 @@ describe('getConfig', function () {
compareResolvedAndUnresolvedPath(config.paths.referenceImages, paths.referenceImages)
})

it('default resemble options', function () {
createEmptyConfigFile()

const config = getConfig()

expect(config.resembleOptions.ignore).to.eql('nothing')
expect(config.resembleOptions.output.largeImageThreshold).to.eql(0)
expect(config.resembleOptions.output.transparency).to.eql(0.3)
})

it('default resemble options should be overrided by config file', function () {
const resembleOptions = {
ignore: 'antialiasing',
output: {
largeImageThreshold: 1000,
transparency: 0.5
}
}
createConfigFile({ resembleOptions })

const config = getConfig()

expect(config.resembleOptions).to.eql(resembleOptions)
})

it('should override only the specified resemble options preserving defaults', function () {
const resembleOptions = {
output: {
transparency: 0.5,
errorType: "movement"
}
}
createConfigFile({ resembleOptions })

const config = getConfig()

expect(config.resembleOptions.ignore).to.eql('nothing')
expect(config.resembleOptions.output.largeImageThreshold).to.eql(0)
expect(config.resembleOptions.output.transparency).to.eql(0.5)
expect(config.resembleOptions.output.errorType).to.eql("movement")
})

const compareResolvedAndUnresolvedPath = (resolved, unresolved) => {
expect(resolved).to.satisfy(path => path.endsWith(unresolved.replace('.', '')))
}
Expand Down
4 changes: 4 additions & 0 deletions integrationTests/robot-eyes.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"testImages": "./images/test",
"diffImages": "./images/diff",
"referenceImages": "./images/reference"
},
"threshold": 0.02,
"resembleOptions": {
"ignore": ["antialiasing"]
}
}

0 comments on commit 4393037

Please sign in to comment.