Skip to content

Commit 93da8f0

Browse files
committed
feat(browser): use @blazediff/core as the default screenshot comparator
1 parent 20e00ef commit 93da8f0

11 files changed

Lines changed: 42 additions & 39 deletions

File tree

docs/api/browser/assertions.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,15 +1133,15 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button')
11331133
11341134
// with options
11351135
await expect.element(getByTestId('button')).toMatchScreenshot({
1136-
comparatorName: 'pixelmatch',
1136+
comparatorName: '@blazediff/core',
11371137
comparatorOptions: {
11381138
allowedMismatchedPixelRatio: 0.01,
11391139
},
11401140
})
11411141
11421142
// with both name and options
11431143
await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
1144-
comparatorName: 'pixelmatch',
1144+
comparatorName: '@blazediff/core',
11451145
comparatorOptions: {
11461146
allowedMismatchedPixelRatio: 0.01,
11471147
},
@@ -1150,12 +1150,13 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
11501150

11511151
### Options
11521152

1153-
- `comparatorName: "pixelmatch" = "pixelmatch"`
1153+
- `comparatorName: "@blazediff/core" | "pixelmatch" = "@blazediff/core"`
11541154

1155-
The name of the algorithm/library used for comparing images.
1155+
The algorithm/library used for comparing images.
11561156

1157-
Currently, [`"pixelmatch"`](https://github.com/mapbox/pixelmatch) is the only
1158-
supported comparator.
1157+
[`"@blazediff/core"`](https://blazediff.dev/docs/core) is the only built-in comparator, but you can use custom ones by [registering them in the config file](/config/browser#browser-expect-toMatchScreenshot-comparators).
1158+
1159+
Since v4.1, `"pixelmatch"` is a legacy alias for `"@blazediff/core"` and will be removed in the next major version.
11591160

11601161
- `comparatorOptions: object`
11611162

@@ -1164,7 +1165,7 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
11641165

11651166
Vitest has set default values out of the box, but they can be overridden.
11661167

1167-
- [`"pixelmatch"` options](#pixelmatch-comparator-options)
1168+
- [`"@blazediff/core"` options](#blazediff-core-comparator-options)
11681169

11691170
::: warning
11701171
**Always explicitly set `comparatorName` to get proper type inference for
@@ -1181,9 +1182,9 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
11811182
},
11821183
})
11831184
1184-
// ✅ TypeScript knows these are pixelmatch options
1185+
// ✅ TypeScript knows these are `"@blazediff/core"` options
11851186
await expect.element(button).toMatchScreenshot({
1186-
comparatorName: 'pixelmatch',
1187+
comparatorName: '@blazediff/core',
11871188
comparatorOptions: {
11881189
allowedMismatchedPixelRatio: 0.01,
11891190
},
@@ -1208,9 +1209,9 @@ await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
12081209
Setting this value to `0` disables the timeout, but if a stable screenshot
12091210
can't be determined the process will not end.
12101211

1211-
#### `"pixelmatch"` comparator options
1212+
#### `"@blazediff/core"` comparator options
12121213

1213-
The following options are available when using the `"pixelmatch"` comparator:
1214+
The following options are available when using the `"@blazediff/core"` comparator:
12141215

12151216
- `allowedMismatchedPixelRatio: number | undefined = undefined`
12161217

docs/config/browser/expect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default defineConfig({
2828
enabled: true,
2929
expect: {
3030
toMatchScreenshot: {
31-
comparatorName: 'pixelmatch',
31+
comparatorName: '@blazediff/core',
3232
comparatorOptions: {
3333
threshold: 0.2,
3434
allowedMismatchedPixels: 100,

docs/guide/browser/visual-regression-testing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export default defineConfig({
154154
browser: {
155155
expect: {
156156
toMatchScreenshot: {
157-
comparatorName: 'pixelmatch',
157+
comparatorName: '@blazediff/core',
158158
comparatorOptions: {
159159
// 0-1, how different can colors be?
160160
threshold: 0.2,
@@ -174,7 +174,7 @@ Override global settings for specific tests:
174174

175175
```ts
176176
await expect(element).toMatchScreenshot('button-hover', {
177-
comparatorName: 'pixelmatch',
177+
comparatorName: '@blazediff/core',
178178
comparatorOptions: {
179179
// more lax comparison for text-heavy elements
180180
allowedMismatchedPixelRatio: 0.1,
@@ -339,7 +339,7 @@ possible solutions might be to:
339339

340340
```ts
341341
await expect(page.getByTestId('article-summary')).toMatchScreenshot({
342-
comparatorName: 'pixelmatch',
342+
comparatorName: '@blazediff/core',
343343
comparatorOptions: {
344344
// 10% of the pixels are allowed to change
345345
allowedMismatchedPixelRatio: 0.1,

packages/browser/context.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface ScreenshotOptions {
4141
}
4242

4343
interface StandardScreenshotComparators {
44-
pixelmatch: {
44+
'@blazediff/core': {
4545
/**
4646
* The maximum number of pixels that are allowed to differ between the captured
4747
* screenshot and the stored reference image.
@@ -134,6 +134,10 @@ interface StandardScreenshotComparators {
134134
*/
135135
diffMask?: boolean | undefined
136136
}
137+
/**
138+
* @deprecated Since v4.1, `"pixelmatch"` is a legacy alias for `"@blazediff/core"` and will be removed in the next major version.
139+
*/
140+
pixelmatch: StandardScreenshotComparators['@blazediff/core']
137141
}
138142

139143
export interface ScreenshotComparatorRegistry extends StandardScreenshotComparators {}
@@ -151,7 +155,7 @@ export interface ScreenshotMatcherOptions<
151155
*
152156
* Must be one of the keys from {@linkcode ScreenshotComparatorRegistry}.
153157
*
154-
* @defaultValue `'pixelmatch'`
158+
* @defaultValue `'@blazediff/core'`
155159
*/
156160
comparatorName?: ComparatorName
157161
comparatorOptions?: ScreenshotComparatorRegistry[ComparatorName]

packages/browser/jest-dom.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,15 @@ export interface TestingLibraryMatchers<E, R> {
698698
*
699699
* // with options
700700
* await expect.element(getByTestId('button')).toMatchScreenshot({
701-
* comparatorName: 'pixelmatch',
701+
* comparatorName: '@blazediff/core',
702702
* comparatorOptions: {
703703
* allowedMismatchedPixelRatio: 0.01,
704704
* },
705705
* })
706706
*
707707
* // with both name and options
708708
* await expect.element(getByTestId('button')).toMatchScreenshot('fancy-button', {
709-
* comparatorName: 'pixelmatch',
709+
* comparatorName: '@blazediff/core',
710710
* comparatorOptions: {
711711
* allowedMismatchedPixelRatio: 0.01,
712712
* },

packages/browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
"vitest": "workspace:*"
6565
},
6666
"dependencies": {
67+
"@blazediff/core": "1.9.1",
6768
"@vitest/mocker": "workspace:*",
6869
"@vitest/utils": "workspace:*",
6970
"magic-string": "catalog:",
70-
"pixelmatch": "7.1.0",
7171
"pngjs": "^7.0.0",
7272
"sirv": "catalog:",
7373
"tinyrainbow": "catalog:",

packages/browser/src/node/commands/screenshotMatcher/comparators/pixelmatch.ts renamed to packages/browser/src/node/commands/screenshotMatcher/comparators/blazediff-core.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ScreenshotComparatorRegistry } from '../../../../../context'
22
import type { Comparator } from '../types'
3-
import pm from 'pixelmatch'
3+
import { diff } from '@blazediff/core'
44

55
const defaultOptions = {
66
allowedMismatchedPixelRatio: undefined,
@@ -12,9 +12,9 @@ const defaultOptions = {
1212
diffColor: [255, 0, 0],
1313
diffColorAlt: undefined,
1414
diffMask: false,
15-
} satisfies ScreenshotComparatorRegistry['pixelmatch']
15+
} satisfies ScreenshotComparatorRegistry['@blazediff/core']
1616

17-
export const pixelmatch: Comparator<ScreenshotComparatorRegistry['pixelmatch']> = (
17+
export const blazediffCore: Comparator<ScreenshotComparatorRegistry['@blazediff/core']> = (
1818
reference,
1919
actual,
2020
{ createDiff, ...options },
@@ -36,7 +36,7 @@ export const pixelmatch: Comparator<ScreenshotComparatorRegistry['pixelmatch']>
3636
? new Uint8Array(reference.data.length)
3737
: undefined
3838

39-
const mismatchedPixels = pm(
39+
const mismatchedPixels = diff(
4040
reference.data,
4141
actual.data,
4242
diffBuffer,

packages/browser/src/node/commands/screenshotMatcher/comparators/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { BrowserCommandContext } from 'vitest/node'
22
import type { ScreenshotComparatorRegistry } from '../../../../../context'
33
import type { Comparator } from '../types'
4-
import { pixelmatch } from './pixelmatch'
4+
import { blazediffCore } from './blazediff-core'
55

66
const comparators: {
77
[ComparatorName in keyof ScreenshotComparatorRegistry]: Comparator<
88
ScreenshotComparatorRegistry[ComparatorName]
99
>
1010
} = {
11-
pixelmatch,
11+
'@blazediff/core': blazediffCore,
12+
'pixelmatch': blazediffCore,
1213
}
1314

1415
export function getComparator<ComparatorName extends keyof ScreenshotComparatorRegistry>(

packages/browser/src/node/commands/screenshotMatcher/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type GlobalOptions = Required<Omit<
1717
>>
1818

1919
const defaultOptions = {
20-
comparatorName: 'pixelmatch',
20+
comparatorName: '@blazediff/core',
2121
// these are handled by each comparator on its own
2222
comparatorOptions: {},
2323
screenshotOptions: {

packages/vitest/src/node/types/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export interface BrowserConfigOptions {
277277
/**
278278
* The name of the comparator to use for visual diffing.
279279
*
280-
* @defaultValue `'pixelmatch'`
280+
* @defaultValue `'@blazediff/core'`
281281
*/
282282
comparatorName?: ComparatorName
283283
comparatorOptions?: ToMatchScreenshotComparators[ComparatorName]

0 commit comments

Comments
 (0)