Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) pixelmatch threshold control #236

Open
jehoshua02 opened this issue Aug 18, 2023 · 1 comment
Open

(feat) pixelmatch threshold control #236

jehoshua02 opened this issue Aug 18, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@jehoshua02
Copy link

jehoshua02 commented Aug 18, 2023

Is your feature request related to a problem? Please describe.

I want to be able to control pixelmatch threshold so I can control how different a pixel must be before pixelmatch considers it different.

Describe the solution you'd like

I'd like to be able to control pixelmatch threshold directly, instead of cypress-plugin-snapshots passing a hard coded 0.01.

Describe alternatives you've considered

Forking this repo. Using a different package. Adjusting threshold in cypress-plugin-snapshots config. Living with flaky tests and accepting new snapshots every day, which is annoying and hinders adoption of visual testing in my team.

Additional context

A snapshot that has imperceptible changes failed a test. I used pixelmatch directly to dial in a threshold that would tolerate the difference:

  • npx pixelmatch imageA.png imageB.actual.png 0.01 reports 354 different pixels
  • npx pixelmatch imageA.png imageB.actual.png 0.011 reports 0 different pixels (sweet spot, at least currently with this snapshot)

I like keeping the threshold for cypress-plugin-snapshots at 0 pixels, because I don't want to allow any room for visual regressions.

Here's the code where threshold is utilized to determine if tests pass or not. Just above this, you can see where the hard coded pixelmatch threshold is passed.
https://github.com/meinaart/cypress-plugin-snapshots/blob/6b95c7a930364b99f2496922159a6d693ad684a2/src/utils/tasks/imageSnapshots.js#L130C1-L138C6

@ibrahimelement
Copy link

ibrahimelement commented Jan 19, 2024

Thanks for spotting this, also running into the same issue - due to color distance between two different renders. Adjusting the threshold seems to solve this problem.

Edit: Upon looking closer, it seems that you're trying to control the difference threshold passed to pixelmatch, rather than the hardcoded 0.01 threshold difference. From my tests, the difference in pixels reported by changing the threshold from 0.01 to 0.00 is 4.7x - which shows that it's significantly more sensitive in what it reports. This ratio is also highly variable. So what you're looking for by setting it to 0.00 is a pixel perfection, then adjusting the difference ratio.

Edit: While using this, I also ran into issues with threshold control and weird bugs for just checking images. I solved this by implementing my own image snapshot logic using pixelmatch - essentially the same thing this package is doing, but gives us full control over image comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants