diff options
author | Lukas Eipert <leipert@gitlab.com> | 2018-10-30 14:46:44 +0300 |
---|---|---|
committer | Lukas Eipert <leipert@gitlab.com> | 2019-01-21 22:48:55 +0300 |
commit | 91df6d9ef496bdab9fa3658b8dea131fb0a71c8c (patch) | |
tree | 2271ad0d4ad9e693b0c0728ed15b7edb2e8497ad /spec/javascripts/matchers.js | |
parent | d56b76a52dbb978982c3dedba333523e908b37db (diff) |
Make favicon tests more fault resistent
It seems like every new version of Chrome renders the favicon canvas
differently. This reduces the problems with it by creating a more fault
tolerant check, which compares the color values in the pixels and is
okay if they are within a certain threshold.
Diffstat (limited to 'spec/javascripts/matchers.js')
-rw-r--r-- | spec/javascripts/matchers.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/spec/javascripts/matchers.js b/spec/javascripts/matchers.js index 0d465510fd3..406527b08a3 100644 --- a/spec/javascripts/matchers.js +++ b/spec/javascripts/matchers.js @@ -1,3 +1,5 @@ +import pixelmatch from 'pixelmatch'; + export default { toContainText: () => ({ compare(vm, text) { @@ -54,4 +56,41 @@ export default { return result; }, }), + toImageDiffEqual: () => { + const getImageData = img => { + const canvas = document.createElement('canvas'); + canvas.width = img.width; + canvas.height = img.height; + canvas.getContext('2d').drawImage(img, 0, 0); + return canvas.getContext('2d').getImageData(0, 0, img.width, img.height).data; + }; + + return { + compare(actual, expected, threshold = 0.1) { + if (actual.height !== expected.height || actual.width !== expected.width) { + return { + pass: false, + message: `Expected image dimensions (h x w) of ${expected.height}x${expected.width}. + Received an image with ${actual.height}x${actual.width}`, + }; + } + + const { width, height } = actual; + const differentPixels = pixelmatch( + getImageData(actual), + getImageData(expected), + null, + width, + height, + { threshold }, + ); + + return { + pass: differentPixels < 20, + message: `${differentPixels} pixels differ more than ${threshold * + 100} percent between input and output.`, + }; + }, + }; + }, }; |