chore: address pixelCount and pixelRatio review comments (#12501)

- documentation fixes
- rename pixelCount to maxDiffPixels
- rename pixelRatio to maxDiffPixelRatio
This commit is contained in:
Andrey Lushnikov 2022-03-04 00:17:31 -07:00 committed by GitHub
parent fc32ec9944
commit 279e579097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 90 additions and 90 deletions

View File

@ -1021,9 +1021,9 @@ await expect(locator).toHaveScreenshot();
### option: LocatorAssertions.toHaveScreenshot.mask = %%-screenshot-option-mask-%% ### option: LocatorAssertions.toHaveScreenshot.mask = %%-screenshot-option-mask-%%
### option: LocatorAssertions.toHaveScreenshot.pixelCount = %%-assertions-pixel-count-%% ### option: LocatorAssertions.toHaveScreenshot.maxDiffPixels = %%-assertions-max-diff-pixels-%%
### option: LocatorAssertions.toHaveScreenshot.pixelRatio = %%-assertions-pixel-ratio-%% ### option: LocatorAssertions.toHaveScreenshot.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
### option: LocatorAssertions.toHaveScreenshot.threshold = %%-assertions-threshold-%% ### option: LocatorAssertions.toHaveScreenshot.threshold = %%-assertions-threshold-%%

View File

@ -140,9 +140,9 @@ await expect(page).toHaveScreenshot();
### option: PageAssertions.toHaveScreenshot.mask = %%-screenshot-option-mask-%% ### option: PageAssertions.toHaveScreenshot.mask = %%-screenshot-option-mask-%%
### option: PageAssertions.toHaveScreenshot.pixelCount = %%-assertions-pixel-count-%% ### option: PageAssertions.toHaveScreenshot.maxDiffPixels = %%-assertions-max-diff-pixels-%%
### option: PageAssertions.toHaveScreenshot.pixelRatio = %%-assertions-pixel-ratio-%% ### option: PageAssertions.toHaveScreenshot.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
### option: PageAssertions.toHaveScreenshot.threshold = %%-assertions-threshold-%% ### option: PageAssertions.toHaveScreenshot.threshold = %%-assertions-threshold-%%

View File

@ -34,8 +34,8 @@ Learn more about [visual comparisons](./test-snapshots.md).
Snapshot name. Snapshot name.
### option: ScreenshotAssertions.toMatchSnapshot.pixelCount = %%-assertions-pixel-count-%% ### option: ScreenshotAssertions.toMatchSnapshot.maxDiffPixels = %%-assertions-max-diff-pixels-%%
### option: ScreenshotAssertions.toMatchSnapshot.pixelRatio = %%-assertions-pixel-ratio-%% ### option: ScreenshotAssertions.toMatchSnapshot.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
### option: ScreenshotAssertions.toMatchSnapshot.threshold = %%-assertions-threshold-%% ### option: ScreenshotAssertions.toMatchSnapshot.threshold = %%-assertions-threshold-%%

View File

@ -715,15 +715,15 @@ Time to retry the assertion for. Defaults to `timeout` in `TestConfig.expect`.
Time to retry the assertion for. Time to retry the assertion for.
## assertions-pixel-count ## assertions-max-diff-pixels
* langs: js * langs: js
- `pixelCount` <[int]> - `maxDiffPixels` <[int]>
An acceptable amount of pixels that could be different, unset by default. An acceptable amount of pixels that could be different, unset by default.
## assertions-pixel-ratio ## assertions-max-diff-pixel-ratio
* langs: js * langs: js
- `pixelRatio` <[float]> - `maxDiffPixelRatio` <[float]>
An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`, unset by default. An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`, unset by default.
@ -731,7 +731,7 @@ An acceptable ratio of pixels that are different to the total amount of pixels,
* langs: js * langs: js
- `threshold` <[float]> - `threshold` <[float]>
An acceptable percieved color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between pixels in compared images, between zero (strict) and one (lax), default is configurable with `TestConfig.expect`. Defaults to `0.2`. An acceptable percieved color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between the same pixel in compared images, between zero (strict) and one (lax), default is configurable with `TestConfig.expect`. Defaults to `0.2`.
## shared-context-params-list ## shared-context-params-list
- %%-context-option-acceptdownloads-%% - %%-context-option-acceptdownloads-%%

View File

@ -37,9 +37,9 @@ export default config;
- type: <[Object]> - type: <[Object]>
- `timeout` <[int]> Default timeout for async expect matchers in milliseconds, defaults to 5000ms. - `timeout` <[int]> Default timeout for async expect matchers in milliseconds, defaults to 5000ms.
- `toMatchSnapshot` <[Object]> - `toMatchSnapshot` <[Object]>
- `threshold` <[float]> an acceptable percieved color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between pixels in compared images, between zero (strict) and one (lax). Defaults to `0.2`. - `threshold` <[float]> an acceptable percieved color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between the same pixel 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. - `maxDiffPixels` <[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. - `maxDiffPixelRatio` <[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
Configuration for the `expect` assertion library. Learn more about [various timeouts](./test-timeouts.md). Configuration for the `expect` assertion library. Learn more about [various timeouts](./test-timeouts.md).

View File

@ -108,9 +108,9 @@ export default config;
- type: <[Object]> - type: <[Object]>
- `timeout` <[int]> Default timeout for async expect matchers in milliseconds, defaults to 5000ms. - `timeout` <[int]> Default timeout for async expect matchers in milliseconds, defaults to 5000ms.
- `toMatchSnapshot` <[Object]> - `toMatchSnapshot` <[Object]>
- `threshold` <[float]> an acceptable percieved color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between pixels in compared images, between zero (strict) and one (lax). Defaults to `0.2`. - `threshold` <[float]> an acceptable percieved color difference in the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) between the same pixel 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. - `maxDiffPixels` <[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. - `maxDiffPixelRatio` <[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
Configuration for the `expect` assertion library. Configuration for the `expect` assertion library.

View File

@ -75,7 +75,7 @@ const { test, expect } = require('@playwright/test');
test('example test', async ({ page }) => { test('example test', async ({ page }) => {
await page.goto('https://playwright.dev'); await page.goto('https://playwright.dev');
expect(await page.screenshot()).toMatchSnapshot('home.png', { pixelCount: 100 }); expect(await page.screenshot()).toMatchSnapshot('home.png', { maxDiffPixels: 100 });
}); });
``` ```
@ -85,7 +85,7 @@ import { test, expect } from '@playwright/test';
test('example test', async ({ page }) => { test('example test', async ({ page }) => {
await page.goto('https://playwright.dev'); await page.goto('https://playwright.dev');
expect(await page.screenshot()).toMatchSnapshot('home.png', { pixelCount: 100 }); expect(await page.screenshot()).toMatchSnapshot('home.png', { maxDiffPixels: 100 });
}); });
``` ```
@ -94,7 +94,7 @@ If you'd like to share the default value among all the tests in the project, you
```js js-flavor=js ```js js-flavor=js
module.exports = { module.exports = {
expect: { expect: {
toMatchSnapshot: { pixelCount: 100 }, toMatchSnapshot: { maxDiffPixels: 100 },
}, },
}; };
``` ```
@ -103,7 +103,7 @@ module.exports = {
import { PlaywrightTestConfig } from '@playwright/test'; import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = { const config: PlaywrightTestConfig = {
expect: { expect: {
toMatchSnapshot: { pixelCount: 100 }, toMatchSnapshot: { maxDiffPixels: 100 },
}, },
}; };
export default config; export default config;

View File

@ -1496,8 +1496,8 @@ export type PageExpectScreenshotParams = {
selector: string, selector: string,
}, },
comparatorOptions?: { comparatorOptions?: {
pixelCount?: number, maxDiffPixels?: number,
pixelRatio?: number, maxDiffPixelRatio?: number,
threshold?: number, threshold?: number,
}, },
screenshotOptions?: { screenshotOptions?: {
@ -1519,8 +1519,8 @@ export type PageExpectScreenshotOptions = {
selector: string, selector: string,
}, },
comparatorOptions?: { comparatorOptions?: {
pixelCount?: number, maxDiffPixels?: number,
pixelRatio?: number, maxDiffPixelRatio?: number,
threshold?: number, threshold?: number,
}, },
screenshotOptions?: { screenshotOptions?: {

View File

@ -1003,8 +1003,8 @@ Page:
comparatorOptions: comparatorOptions:
type: object? type: object?
properties: properties:
pixelCount: number? maxDiffPixels: number?
pixelRatio: number? maxDiffPixelRatio: number?
threshold: number? threshold: number?
screenshotOptions: screenshotOptions:
type: object? type: object?

View File

@ -549,8 +549,8 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
selector: tString, selector: tString,
})), })),
comparatorOptions: tOptional(tObject({ comparatorOptions: tOptional(tObject({
pixelCount: tOptional(tNumber), maxDiffPixels: tOptional(tNumber),
pixelRatio: tOptional(tNumber), maxDiffPixelRatio: tOptional(tNumber),
threshold: tOptional(tNumber), threshold: tOptional(tNumber),
})), })),
screenshotOptions: tOptional(tObject({ screenshotOptions: tOptional(tObject({

View File

@ -23,7 +23,7 @@ import { diff_match_patch, DIFF_INSERT, DIFF_DELETE, DIFF_EQUAL } from '../third
// Note: we require the pngjs version of pixelmatch to avoid version mismatches. // Note: we require the pngjs version of pixelmatch to avoid version mismatches.
const { PNG } = require(require.resolve('pngjs', { paths: [require.resolve('pixelmatch')] })) as typeof import('pngjs'); const { PNG } = require(require.resolve('pngjs', { paths: [require.resolve('pixelmatch')] })) as typeof import('pngjs');
export type ImageComparatorOptions = { threshold?: number, pixelCount?: number, pixelRatio?: number }; export type ImageComparatorOptions = { threshold?: number, maxDiffPixels?: number, maxDiffPixelRatio?: number };
export type ComparatorResult = { diff?: Buffer; errorMessage?: string; } | null; export type ComparatorResult = { diff?: Buffer; errorMessage?: string; } | null;
export type Comparator = (actualBuffer: Buffer | string, expectedBuffer: Buffer, options?: any) => ComparatorResult; export type Comparator = (actualBuffer: Buffer | string, expectedBuffer: Buffer, options?: any) => ComparatorResult;
export const mimeTypeToComparator: { [key: string]: Comparator } = { export const mimeTypeToComparator: { [key: string]: Comparator } = {
@ -59,14 +59,14 @@ function compareImages(mimeType: string, actualBuffer: Buffer | string, expected
threshold: options.threshold ?? 0.2, threshold: options.threshold ?? 0.2,
}); });
const pixelCount1 = options.pixelCount; const maxDiffPixels1 = options.maxDiffPixels;
const pixelCount2 = options.pixelRatio !== undefined ? expected.width * expected.height * options.pixelRatio : undefined; const maxDiffPixels2 = options.maxDiffPixelRatio !== undefined ? expected.width * expected.height * options.maxDiffPixelRatio : undefined;
let pixelCount; let maxDiffPixels;
if (pixelCount1 !== undefined && pixelCount2 !== undefined) if (maxDiffPixels1 !== undefined && maxDiffPixels2 !== undefined)
pixelCount = Math.min(pixelCount1, pixelCount2); maxDiffPixels = Math.min(maxDiffPixels1, maxDiffPixels2);
else else
pixelCount = pixelCount1 ?? pixelCount2 ?? 0; maxDiffPixels = maxDiffPixels1 ?? maxDiffPixels2 ?? 0;
return count > pixelCount ? { diff: PNG.sync.write(diff) } : null; return count > maxDiffPixels ? { diff: PNG.sync.write(diff) } : null;
} }
function compareText(actual: Buffer | string, expectedBuffer: Buffer): ComparatorResult { function compareText(actual: Buffer | string, expectedBuffer: Buffer): ComparatorResult {

View File

@ -80,11 +80,11 @@ class SnapshotHelper<T extends ImageComparatorOptions> {
...options, ...options,
}; };
if (options.pixelCount !== undefined && options.pixelCount < 0) if (options.maxDiffPixels !== undefined && options.maxDiffPixels < 0)
throw new Error('`pixelCount` option value must be non-negative integer'); throw new Error('`maxDiffPixels` option value must be non-negative integer');
if (options.pixelRatio !== undefined && (options.pixelRatio < 0 || options.pixelRatio > 1)) if (options.maxDiffPixelRatio !== undefined && (options.maxDiffPixelRatio < 0 || options.maxDiffPixelRatio > 1))
throw new Error('`pixelRatio` option value must be between 0 and 1'); throw new Error('`maxDiffPixelRatio` option value must be between 0 and 1');
// sanitizes path if string // sanitizes path if string
const pathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, '', undefined, true)]; const pathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, '', undefined, true)];
@ -111,8 +111,8 @@ class SnapshotHelper<T extends ImageComparatorOptions> {
this.updateSnapshots = updateSnapshots; this.updateSnapshots = updateSnapshots;
this.allOptions = options; this.allOptions = options;
this.comparatorOptions = { this.comparatorOptions = {
pixelCount: options.pixelCount, maxDiffPixels: options.maxDiffPixels,
pixelRatio: options.pixelRatio, maxDiffPixelRatio: options.maxDiffPixelRatio,
threshold: options.threshold, threshold: options.threshold,
}; };
this.kind = this.mimeType.startsWith('image/') ? 'Screenshot' : 'Snapshot'; this.kind = this.mimeType.startsWith('image/') ? 'Screenshot' : 'Snapshot';
@ -262,8 +262,8 @@ export async function toHaveScreenshot(
mask: (helper.allOptions.mask || []) as LocatorEx[], mask: (helper.allOptions.mask || []) as LocatorEx[],
name: undefined, name: undefined,
threshold: undefined, threshold: undefined,
pixelCount: undefined, maxDiffPixels: undefined,
pixelRatio: undefined, maxDiffPixelRatio: undefined,
}; };
const hasSnapshot = fs.existsSync(helper.snapshotPath); const hasSnapshot = fs.existsSync(helper.snapshotPath);

View File

@ -50,11 +50,11 @@ type ExpectSettings = {
/** /**
* An acceptable amount of pixels that could be different, unset by default. * An acceptable amount of pixels that could be different, unset by default.
*/ */
pixelCount?: number, maxDiffPixels?: number,
/** /**
* An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default. * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
*/ */
pixelRatio?: number, maxDiffPixelRatio?: number,
} }
}; };

View File

@ -50,8 +50,8 @@ export declare type Expect = {
type ImageComparatorOptions = { type ImageComparatorOptions = {
threshold?: number, threshold?: number,
pixelCount?: number, maxDiffPixels?: number,
pixelRatio?: number, maxDiffPixelRatio?: number,
}; };
type Awaited<T> = T extends PromiseLike<infer U> ? U : T; type Awaited<T> = T extends PromiseLike<infer U> ? U : T;

View File

@ -79,8 +79,8 @@ test('should compile with different option combinations', async ({ runTSC }) =>
test('is a test', async ({ page }) => { test('is a test', async ({ page }) => {
expect('foo').toMatchSnapshot(); expect('foo').toMatchSnapshot();
expect('foo').toMatchSnapshot({ threshold: 0.2 }); expect('foo').toMatchSnapshot({ threshold: 0.2 });
expect('foo').toMatchSnapshot({ pixelRatio: 0.2 }); expect('foo').toMatchSnapshot({ maxDiffPixelRatio: 0.2 });
expect('foo').toMatchSnapshot({ pixelCount: 0.2 }); expect('foo').toMatchSnapshot({ maxDiffPixels: 0.2 });
}); });
` `
}); });
@ -396,35 +396,35 @@ test('should compare binary', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0); expect(result.exitCode).toBe(0);
}); });
test('should throw for invalid pixelCount values', async ({ runInlineTest }) => { test('should throw for invalid maxDiffPixels values', async ({ runInlineTest }) => {
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
'a.spec.js': ` 'a.spec.js': `
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from([1,2,3,4])).toMatchSnapshot({ expect(Buffer.from([1,2,3,4])).toMatchSnapshot({
pixelCount: -1, maxDiffPixels: -1,
}); });
}); });
` `
})).exitCode).toBe(1); })).exitCode).toBe(1);
}); });
test('should throw for invalid pixelRatio values', async ({ runInlineTest }) => { test('should throw for invalid maxDiffPixelRatio values', async ({ runInlineTest }) => {
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
'a.spec.js': ` 'a.spec.js': `
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from([1,2,3,4])).toMatchSnapshot({ expect(Buffer.from([1,2,3,4])).toMatchSnapshot({
pixelRatio: 12, maxDiffPixelRatio: 12,
}); });
}); });
` `
})).exitCode).toBe(1); })).exitCode).toBe(1);
}); });
test('should respect pixelCount option', async ({ runInlineTest }) => { test('should respect maxDiffPixels option', async ({ runInlineTest }) => {
const width = 20, height = 20; const width = 20, height = 20;
const BAD_PIXELS = 120; const BAD_PIXELS = 120;
const image1 = createWhiteImage(width, height); const image1 = createWhiteImage(width, height);
@ -448,17 +448,17 @@ test('should respect pixelCount option', async ({ runInlineTest }) => {
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', { expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', {
pixelCount: ${BAD_PIXELS} maxDiffPixels: ${BAD_PIXELS}
}); });
}); });
` `
})).exitCode, 'make sure pixelCount option is respected').toBe(0); })).exitCode, 'make sure maxDiffPixels option is respected').toBe(0);
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
'playwright.config.ts': ` 'playwright.config.ts': `
module.exports = { projects: [ module.exports = { projects: [
{ expect: { toMatchSnapshot: { pixelCount: ${BAD_PIXELS} } } }, { expect: { toMatchSnapshot: { maxDiffPixels: ${BAD_PIXELS} } } },
]}; ]};
`, `,
'a.spec.js-snapshots/snapshot.png': image1, 'a.spec.js-snapshots/snapshot.png': image1,
@ -468,10 +468,10 @@ test('should respect pixelCount option', async ({ runInlineTest }) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png'); expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png');
}); });
` `
})).exitCode, 'make sure pixelCount option in project config is respected').toBe(0); })).exitCode, 'make sure maxDiffPixels option in project config is respected').toBe(0);
}); });
test('should respect pixelRatio option', async ({ runInlineTest }) => { test('should respect maxDiffPixelRatio option', async ({ runInlineTest }) => {
const width = 20, height = 20; const width = 20, height = 20;
const BAD_RATIO = 0.25; const BAD_RATIO = 0.25;
const BAD_PIXELS = Math.floor(width * height * BAD_RATIO); const BAD_PIXELS = Math.floor(width * height * BAD_RATIO);
@ -496,17 +496,17 @@ test('should respect pixelRatio option', async ({ runInlineTest }) => {
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', { expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', {
pixelRatio: ${BAD_RATIO} maxDiffPixelRatio: ${BAD_RATIO}
}); });
}); });
` `
})).exitCode, 'make sure pixelRatio option is respected').toBe(0); })).exitCode, 'make sure maxDiffPixelRatio option is respected').toBe(0);
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
'playwright.config.ts': ` 'playwright.config.ts': `
module.exports = { projects: [ module.exports = { projects: [
{ expect: { toMatchSnapshot: { pixelRatio: ${BAD_RATIO} } } }, { expect: { toMatchSnapshot: { maxDiffPixelRatio: ${BAD_RATIO} } } },
]}; ]};
`, `,
'a.spec.js-snapshots/snapshot.png': image1, 'a.spec.js-snapshots/snapshot.png': image1,
@ -516,10 +516,10 @@ test('should respect pixelRatio option', async ({ runInlineTest }) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png'); expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png');
}); });
` `
})).exitCode, 'make sure pixelCount option in project config is respected').toBe(0); })).exitCode, 'make sure maxDiffPixels option in project config is respected').toBe(0);
}); });
test('should satisfy both pixelRatio and pixelCount', async ({ runInlineTest }) => { test('should satisfy both maxDiffPixelRatio and maxDiffPixels', async ({ runInlineTest }) => {
const width = 20, height = 20; const width = 20, height = 20;
const BAD_RATIO = 0.25; const BAD_RATIO = 0.25;
const BAD_COUNT = Math.floor(width * height * BAD_RATIO); const BAD_COUNT = Math.floor(width * height * BAD_RATIO);
@ -544,12 +544,12 @@ test('should satisfy both pixelRatio and pixelCount', async ({ runInlineTest })
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', { expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', {
pixelCount: ${Math.floor(BAD_COUNT / 2)}, maxDiffPixels: ${Math.floor(BAD_COUNT / 2)},
pixelRatio: ${BAD_RATIO}, maxDiffPixelRatio: ${BAD_RATIO},
}); });
}); });
` `
})).exitCode, 'make sure it fails when pixelCount < actualBadPixels < pixelRatio').toBe(1); })).exitCode, 'make sure it fails when maxDiffPixels < actualBadPixels < maxDiffPixelRatio').toBe(1);
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
@ -558,12 +558,12 @@ test('should satisfy both pixelRatio and pixelCount', async ({ runInlineTest })
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', { expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', {
pixelCount: ${BAD_COUNT}, maxDiffPixels: ${BAD_COUNT},
pixelRatio: ${BAD_RATIO / 2}, maxDiffPixelRatio: ${BAD_RATIO / 2},
}); });
}); });
` `
})).exitCode, 'make sure it fails when pixelRatio < actualBadPixels < pixelCount').toBe(1); })).exitCode, 'make sure it fails when maxDiffPixelRatio < actualBadPixels < maxDiffPixels').toBe(1);
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
@ -572,12 +572,12 @@ test('should satisfy both pixelRatio and pixelCount', async ({ runInlineTest })
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', ({}) => { test('is a test', ({}) => {
expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', { expect(Buffer.from('${image2.toString('base64')}', 'base64')).toMatchSnapshot('snapshot.png', {
pixelCount: ${BAD_COUNT}, maxDiffPixels: ${BAD_COUNT},
pixelRatio: ${BAD_RATIO}, maxDiffPixelRatio: ${BAD_RATIO},
}); });
}); });
` `
})).exitCode, 'make sure it passes when actualBadPixels < pixelRatio && actualBadPixels < pixelCount').toBe(0); })).exitCode, 'make sure it passes when actualBadPixels < maxDiffPixelRatio && actualBadPixels < maxDiffPixels').toBe(0);
}); });
test('should compare PNG images', async ({ runInlineTest }) => { test('should compare PNG images', async ({ runInlineTest }) => {

View File

@ -212,11 +212,11 @@ test('should compile with different option combinations', async ({ runTSC }) =>
test('is a test', async ({ page }) => { test('is a test', async ({ page }) => {
await expect(page).toHaveScreenshot(); await expect(page).toHaveScreenshot();
await expect(page.locator('body')).toHaveScreenshot({ threshold: 0.2 }); await expect(page.locator('body')).toHaveScreenshot({ threshold: 0.2 });
await expect(page).toHaveScreenshot({ pixelRatio: 0.2 }); await expect(page).toHaveScreenshot({ maxDiffPixelRatio: 0.2 });
await expect(page).toHaveScreenshot({ await expect(page).toHaveScreenshot({
threshold: 0.2, threshold: 0.2,
pixelCount: 10, maxDiffPixels: 10,
pixelRatio: 0.2, maxDiffPixelRatio: 0.2,
disableAnimations: true, disableAnimations: true,
omitBackground: true, omitBackground: true,
timeout: 1000, timeout: 1000,
@ -473,7 +473,7 @@ test('should use provided name via options', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0); expect(result.exitCode).toBe(0);
}); });
test('should respect pixelCount option', async ({ runInlineTest }) => { test('should respect maxDiffPixels option', async ({ runInlineTest }) => {
const BAD_PIXELS = 120; const BAD_PIXELS = 120;
const EXPECTED_SNAPSHOT = paintBlackPixels(whiteImage, BAD_PIXELS); const EXPECTED_SNAPSHOT = paintBlackPixels(whiteImage, BAD_PIXELS);
@ -495,17 +495,17 @@ test('should respect pixelCount option', async ({ runInlineTest }) => {
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', async ({ page }) => { test('is a test', async ({ page }) => {
await expect(page).toHaveScreenshot('snapshot.png', { await expect(page).toHaveScreenshot('snapshot.png', {
pixelCount: ${BAD_PIXELS} maxDiffPixels: ${BAD_PIXELS}
}); });
}); });
` `
})).exitCode, 'make sure pixelCount option is respected').toBe(0); })).exitCode, 'make sure maxDiffPixels option is respected').toBe(0);
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
'playwright.config.ts': ` 'playwright.config.ts': `
module.exports = { projects: [ module.exports = { projects: [
{ expect: { toMatchSnapshot: { pixelCount: ${BAD_PIXELS} } } }, { expect: { toMatchSnapshot: { maxDiffPixels: ${BAD_PIXELS} } } },
]}; ]};
`, `,
'a.spec.js-snapshots/snapshot.png': EXPECTED_SNAPSHOT, 'a.spec.js-snapshots/snapshot.png': EXPECTED_SNAPSHOT,
@ -515,10 +515,10 @@ test('should respect pixelCount option', async ({ runInlineTest }) => {
await expect(page).toHaveScreenshot('snapshot.png'); await expect(page).toHaveScreenshot('snapshot.png');
}); });
` `
})).exitCode, 'make sure pixelCount option in project config is respected').toBe(0); })).exitCode, 'make sure maxDiffPixels option in project config is respected').toBe(0);
}); });
test('should respect pixelRatio option', async ({ runInlineTest }) => { test('should respect maxDiffPixelRatio option', async ({ runInlineTest }) => {
const BAD_RATIO = 0.25; const BAD_RATIO = 0.25;
const BAD_PIXELS = IMG_WIDTH * IMG_HEIGHT * BAD_RATIO; const BAD_PIXELS = IMG_WIDTH * IMG_HEIGHT * BAD_RATIO;
const EXPECTED_SNAPSHOT = paintBlackPixels(whiteImage, BAD_PIXELS); const EXPECTED_SNAPSHOT = paintBlackPixels(whiteImage, BAD_PIXELS);
@ -541,17 +541,17 @@ test('should respect pixelRatio option', async ({ runInlineTest }) => {
const { test } = require('./helper'); const { test } = require('./helper');
test('is a test', async ({ page }) => { test('is a test', async ({ page }) => {
await expect(page).toHaveScreenshot('snapshot.png', { await expect(page).toHaveScreenshot('snapshot.png', {
pixelRatio: ${BAD_RATIO} maxDiffPixelRatio: ${BAD_RATIO}
}); });
}); });
` `
})).exitCode, 'make sure pixelRatio option is respected').toBe(0); })).exitCode, 'make sure maxDiffPixelRatio option is respected').toBe(0);
expect((await runInlineTest({ expect((await runInlineTest({
...files, ...files,
'playwright.config.ts': ` 'playwright.config.ts': `
module.exports = { projects: [ module.exports = { projects: [
{ expect: { toMatchSnapshot: { pixelRatio: ${BAD_RATIO} } } }, { expect: { toMatchSnapshot: { maxDiffPixelRatio: ${BAD_RATIO} } } },
]}; ]};
`, `,
'a.spec.js-snapshots/snapshot.png': EXPECTED_SNAPSHOT, 'a.spec.js-snapshots/snapshot.png': EXPECTED_SNAPSHOT,
@ -561,7 +561,7 @@ test('should respect pixelRatio option', async ({ runInlineTest }) => {
await expect(page).toHaveScreenshot('snapshot.png'); await expect(page).toHaveScreenshot('snapshot.png');
}); });
` `
})).exitCode, 'make sure pixelCount option in project config is respected').toBe(0); })).exitCode, 'make sure maxDiffPixels option in project config is respected').toBe(0);
}); });
test('should attach expected/actual and no diff when sizes are different', async ({ runInlineTest }, testInfo) => { test('should attach expected/actual and no diff when sizes are different', async ({ runInlineTest }, testInfo) => {

View File

@ -49,11 +49,11 @@ type ExpectSettings = {
/** /**
* An acceptable amount of pixels that could be different, unset by default. * An acceptable amount of pixels that could be different, unset by default.
*/ */
pixelCount?: number, maxDiffPixels?: number,
/** /**
* An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default. * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
*/ */
pixelRatio?: number, maxDiffPixelRatio?: number,
} }
}; };