Skip to content

Commit 867d761

Browse files
authored
use resemble's more accurate rawMisMatchPercentage value (#1337)
1 parent 4a3cf1d commit 867d761

File tree

7 files changed

+28
-10
lines changed

7 files changed

+28
-10
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ By specifying `resembleOutputOptions` in your backstop.json file you can modify
817817

818818
Instead of calling resemble`s ignoreAntialiasing(), you may set it as a property in the config. (See [example](examples/simpleReactApp/backstop.json))
819819

820+
Per default, Backstop uses Resemble's misMatchPercentage value. However, this value only detects mismatches above 0.01%. If you need more precision and want to use a `misMatchThreshold` below `0.01` (e.g. for large screenshots or small changes), you can set `usePreciseMatching` in the `resembleOutputOptions`. (See [example](examples/simpleReactApp/backstop.json))
821+
820822
```json
821823
"resembleOutputOptions": {
822824
"errorColor": {

core/util/compare/compare-resemble.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ var resemble = require('@mirzazeyrek/node-resemble-js');
22

33
module.exports = function (referencePath, testPath, misMatchThreshold, resembleOutputSettings, requireSameDimensions) {
44
return new Promise(function (resolve, reject) {
5-
resemble.outputSettings(resembleOutputSettings || {});
6-
var comparison = resemble(referencePath).compareTo(testPath);
5+
const resembleSettings = resembleOutputSettings || {};
6+
resemble.outputSettings(resembleSettings);
7+
const comparison = resemble(referencePath).compareTo(testPath);
78

8-
if (resembleOutputSettings && resembleOutputSettings.ignoreAntialiasing) {
9+
if (resembleSettings.ignoreAntialiasing) {
910
comparison.ignoreAntialiasing();
1011
}
1112

1213
comparison.onComplete(data => {
13-
if ((requireSameDimensions === false || data.isSameDimensions === true) && data.misMatchPercentage <= misMatchThreshold) {
14+
const misMatchPercentage = resembleSettings.usePreciseMatching ? data.rawMisMatchPercentage : data.misMatchPercentage;
15+
if ((requireSameDimensions === false || data.isSameDimensions === true) && misMatchPercentage <= misMatchThreshold) {
1416
return resolve(data);
1517
}
1618
reject(data);

core/util/compare/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function comparePair (pair, report, config, compareConfig) {
5353
}
5454
}
5555

56-
var resembleOutputSettings = config.resembleOutputOptions;
56+
const resembleOutputSettings = config.resembleOutputOptions;
5757
return compareImages(referencePath, testPath, pair, resembleOutputSettings, Test);
5858
}
5959

core/util/engineTools.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function genHash (str) {
3030
return hash.toString().replace(/^-/, 0);
3131
}
3232

33-
function getRequireSameDimentions (scenario, config) {
33+
function getRequireSameDimensions (scenario, config) {
3434
if (scenario.requireSameDimensions !== undefined) {
3535
return scenario.requireSameDimensions;
3636
} else if (config.requireSameDimensions !== undefined) {
@@ -114,7 +114,7 @@ function generateTestPair (config, scenario, viewport, variantOrScenarioLabelSaf
114114
selector: selector,
115115
fileName: fileName,
116116
label: scenario.label,
117-
requireSameDimensions: getRequireSameDimentions(scenario, config),
117+
requireSameDimensions: getRequireSameDimensions(scenario, config),
118118
misMatchThreshold: getMisMatchThreshHold(scenario, config),
119119
url: scenario.url,
120120
referenceUrl: scenario.referenceUrl,
@@ -126,7 +126,7 @@ function generateTestPair (config, scenario, viewport, variantOrScenarioLabelSaf
126126
module.exports = {
127127
generateTestPair: generateTestPair,
128128
getMisMatchThreshHold: getMisMatchThreshHold,
129-
getRequireSameDimentions: getRequireSameDimentions,
129+
getRequireSameDimensions: getRequireSameDimensions,
130130
ensureFileSuffix: ensureFileSuffix,
131131
glueStringsWithSlash: glueStringsWithSlash,
132132
genHash: genHash,

examples/simpleReactApp/backstop.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
},
4141
"report": ["browser"],
4242
"resembleOutputOptions": {
43-
"ignoreAntialiasing": true
43+
"ignoreAntialiasing": true,
44+
"usePreciseMatching": true
4445
},
4546
"debug": false
4647
}

test/core/util/compare/compare-resemble_spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const REF_IMG1 = path.join(__dirname, 'refImage-1.png');
55
const REF_IMG1_OPTIMIZED = path.join(__dirname, 'refImage-1-optimized.png');
66
const REF_IMG2 = path.join(__dirname, 'refImage-2.png');
7+
const REF_IMG3 = path.join(__dirname, 'refImage-3.png');
78

89
describe('compare-resemble', function () {
910
it('should resolve if the same image is compared', function () {
@@ -13,6 +14,18 @@ describe('compare-resemble', function () {
1314
return compareResemble(REF_IMG1, REF_IMG1_OPTIMIZED, 0, {});
1415
});
1516
it('should reject if two images exceed the mismatchThreshold', function (cb) {
16-
compareResemble(REF_IMG1, REF_IMG2, 0, {}).catch(() => cb());
17+
compareResemble(REF_IMG1, REF_IMG2, 0, {}).then(
18+
() => cb(new Error('should have rejected')),
19+
() => cb()
20+
);
21+
});
22+
it('should use resemble\'s rounded misMatchPercentage value per default', function () {
23+
return compareResemble(REF_IMG1, REF_IMG3, 0, {});
24+
});
25+
it('should use resemble\'s more precise rawMisMatchPercentage value if specified', function (cb) {
26+
compareResemble(REF_IMG1, REF_IMG3, 0, { usePreciseMatching: true }, true).then(
27+
() => cb(new Error('should have rejected')),
28+
() => cb()
29+
);
1730
});
1831
});

test/core/util/compare/refImage-3.png

5.56 KB
Loading

0 commit comments

Comments
 (0)