question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] What does threshold mean in a method toMatchSnapshot

See original GitHub issue

Hi all, Now I am using jest as a test runner with jest-playwright-preset and jest-image-snapshot for image comparisons. Versions: jest: 27.0.4 jest-playwright-preset: 1.7.0 jest-image-snapshot: 4.5.1 playwright: 1.16.2

Screenshot comparisons looks like:

const customConfig = { threshold: 0.5 };
  expect(image).toMatchImageSnapshot({
    customDiffConfig: customConfig,
  });

Where threshold is the ratio of differing to the total number of pixels. Or the maximum value of differing pixels, in case if threshold is > 1.

When I started migrating to the native playwright runner, I faced with the image comparison issue. The expected and actual screenshots are not equals. But the difference is only a few pixels. For the image with resolution 1920x1080. I tried to set the threshold 0.5 or even more. Without any positive results.

I started looking the pixlematch source and find out that threshold determines how much each pixel can differ in color, and not the number of different pixels. https://github.com/mapbox/pixelmatch/blob/b9261a447515f5aff37a15cfab9f4a491868f720/index.js#L45

Further, the number of different pixels is simply compared with zeros in playwright matcher. https://github.com/microsoft/playwright/blob/2a0a44bc09e66ac4aaf03efb1b9d12ea27fd641f/packages/playwright-test/src/matchers/golden.ts#L69

In my case, this approach leads to failures even for several pixels with a fairly high threshold.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
dimkin-eucommented, Dec 23, 2021

old one ticket, where pics with big and small diffs are same after same threshold https://github.com/microsoft/playwright/issues/9444

maybe its worth to investigate usage of some alternative?

0reactions
turnkey-commercecommented, Mar 8, 2022

Everybody: We just landed two new options to toMatchSnapshot: pixelCount and pixelRatio. With these, you can have the following options to configure snapshot matching:

  • threshold <[float]> an acceptable percieved color difference in the YIQ color space between pixels in compared images, between zero (strict) and one (lax). Defaults to 0.2.
  • pixelCount <[int]> an acceptable amount of pixels that could be different, unset by default.
  • pixelRatio <[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between 0 and 1 , unset by default.

You can give them a try using npm i @playwright/test@next. Let us know how it goes!

@aslushnikov I gave them a try on a scenario and they work well for me, thanks very much. I found the options to actually be named as:

  • maxDiffPixelRatio
  • maxDiffPixels
Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >
Playwright Visual Comparisons | Playwright part - 28 - YouTube
The visual comparison is really a great feature.Playwright Test includes the ability to produce and visually compare screenshots using ...
Read more >
How to Perform Visual Regression Testing Using Playwright
Playwright toMatchSnapshot() accepts threshold, threshold ranges from 0 to 1, default value is 0.2. The threshold is tolerance of image ...
Read more >
Jest testing: Top features and how to use them - LogRocket Blog
One problem I found with this way of validating data structures is that the snapshot file is separated from the test code. So...
Read more >
How Snapshot testing works and what does toMatchSnapshot ...
I think this question has not been answered with enough details! Snapshot testing is based on history of your previous tests.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found