@blazediff/vitest 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Teimur Gasanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,339 @@
1
+ # @blazediff/vitest
2
+
3
+ <div align="center">
4
+
5
+ [![npm bundle size](https://img.shields.io/bundlephobia/min/%40blazediff%2Fvitest)](https://www.npmjs.com/package/@blazediff/vitest)
6
+ [![NPM Downloads](https://img.shields.io/npm/dy/%40blazediff%2Fvitest)](https://www.npmjs.com/package/@blazediff/vitest)
7
+
8
+ </div>
9
+
10
+ Vitest matcher for visual regression testing with blazediff. Powered by @blazediff/matcher with Vitest-specific snapshot state integration.
11
+
12
+ ## Features
13
+
14
+ - **Native Vitest matcher**: `toMatchImageSnapshot()` extends Vitest's expect
15
+ - **Snapshot state tracking**: Vitest reports accurate snapshot counts (added/matched/updated/failed)
16
+ - **Multiple comparison algorithms**: `core`, `bin`, `ssim`, `msssim`, `hitchhikers-ssim`, `gmsd`
17
+ - **Auto-setup**: Imports and registers automatically
18
+ - **Update mode**: Works with Vitest's `-u` flag
19
+ - **TypeScript support**: Full type definitions included
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install --save-dev @blazediff/vitest
25
+ ```
26
+
27
+ **Peer dependencies**: Vitest >= 1.0.0
28
+
29
+ ## Quick Start
30
+
31
+ ```typescript
32
+ import { expect, it } from 'vitest';
33
+ import '@blazediff/vitest';
34
+
35
+ it('should match screenshot', async () => {
36
+ const screenshot = await takeScreenshot();
37
+
38
+ await expect(screenshot).toMatchImageSnapshot({
39
+ method: 'core',
40
+ });
41
+ });
42
+ ```
43
+
44
+ ## API Reference
45
+
46
+ ### toMatchImageSnapshot(options?)
47
+
48
+ Vitest matcher for image snapshot comparison.
49
+
50
+ <table>
51
+ <tr>
52
+ <th width="500">Parameter</th>
53
+ <th width="500">Type</th>
54
+ <th width="500">Description</th>
55
+ </tr>
56
+ <tr>
57
+ <td><code>options</code></td>
58
+ <td>Partial&lt;MatcherOptions&gt;</td>
59
+ <td>Optional comparison options (see below)</td>
60
+ </tr>
61
+ </table>
62
+
63
+ #### Options
64
+
65
+ <table>
66
+ <tr>
67
+ <th width="500">Option</th>
68
+ <th width="500">Type</th>
69
+ <th width="500">Default</th>
70
+ <th width="500">Description</th>
71
+ </tr>
72
+ <tr>
73
+ <td><code>method</code></td>
74
+ <td>'core' | 'bin' | 'ssim' | 'msssim' | 'hitchhikers-ssim' | 'gmsd'</td>
75
+ <td>'core'</td>
76
+ <td>Comparison algorithm to use</td>
77
+ </tr>
78
+ <tr>
79
+ <td><code>failureThreshold</code></td>
80
+ <td>number</td>
81
+ <td>0</td>
82
+ <td>Number of pixels or percentage difference allowed</td>
83
+ </tr>
84
+ <tr>
85
+ <td><code>failureThresholdType</code></td>
86
+ <td>'pixel' | 'percent'</td>
87
+ <td>'pixel'</td>
88
+ <td>How to interpret failureThreshold</td>
89
+ </tr>
90
+ <tr>
91
+ <td><code>snapshotsDir</code></td>
92
+ <td>string</td>
93
+ <td>'__snapshots__'</td>
94
+ <td>Directory to store snapshots relative to test file</td>
95
+ </tr>
96
+ <tr>
97
+ <td><code>snapshotIdentifier</code></td>
98
+ <td>string</td>
99
+ <td>auto-generated</td>
100
+ <td>Custom identifier for the snapshot file</td>
101
+ </tr>
102
+ <tr>
103
+ <td><code>updateSnapshots</code></td>
104
+ <td>boolean</td>
105
+ <td>false</td>
106
+ <td>Force update snapshots</td>
107
+ </tr>
108
+ <tr>
109
+ <td><code>threshold</code></td>
110
+ <td>number</td>
111
+ <td>0.1</td>
112
+ <td>Color difference threshold (0-1) for core/bin methods</td>
113
+ </tr>
114
+ </table>
115
+
116
+ See [@blazediff/matcher](https://www.npmjs.com/package/@blazediff/matcher) for full options documentation.
117
+
118
+ ## Usage Patterns
119
+
120
+ ### Basic Snapshot Test
121
+
122
+ ```typescript
123
+ import { expect, it } from 'vitest';
124
+ import '@blazediff/vitest';
125
+
126
+ it('renders correctly', async () => {
127
+ const screenshot = await page.screenshot();
128
+
129
+ await expect(screenshot).toMatchImageSnapshot({
130
+ method: 'core',
131
+ });
132
+ });
133
+ ```
134
+
135
+ ### Different Comparison Methods
136
+
137
+ ```typescript
138
+ // Fast Rust-native comparison (file paths only)
139
+ await expect('/path/to/image.png').toMatchImageSnapshot({
140
+ method: 'bin',
141
+ });
142
+
143
+ // Pure JavaScript comparison
144
+ await expect(imageBuffer).toMatchImageSnapshot({
145
+ method: 'core',
146
+ });
147
+
148
+ // Perceptual similarity (SSIM)
149
+ await expect(imageBuffer).toMatchImageSnapshot({
150
+ method: 'ssim',
151
+ });
152
+
153
+ // Gradient-based comparison
154
+ await expect(imageBuffer).toMatchImageSnapshot({
155
+ method: 'gmsd',
156
+ });
157
+ ```
158
+
159
+ ### Update Snapshots
160
+
161
+ ```bash
162
+ # Update all snapshots
163
+ vitest -u
164
+
165
+ # Update snapshots for specific test
166
+ vitest -u path/to/test.spec.ts
167
+ ```
168
+
169
+ Or programmatically:
170
+
171
+ ```typescript
172
+ await expect(screenshot).toMatchImageSnapshot({
173
+ method: 'core',
174
+ updateSnapshots: true,
175
+ });
176
+ ```
177
+
178
+ ### Custom Thresholds
179
+
180
+ ```typescript
181
+ // Allow up to 100 pixels difference
182
+ await expect(screenshot).toMatchImageSnapshot({
183
+ method: 'core',
184
+ failureThreshold: 100,
185
+ failureThresholdType: 'pixel',
186
+ });
187
+
188
+ // Allow up to 0.1% difference
189
+ await expect(screenshot).toMatchImageSnapshot({
190
+ method: 'core',
191
+ failureThreshold: 0.1,
192
+ failureThresholdType: 'percent',
193
+ });
194
+ ```
195
+
196
+ ### Custom Snapshot Directory
197
+
198
+ ```typescript
199
+ await expect(screenshot).toMatchImageSnapshot({
200
+ method: 'core',
201
+ snapshotsDir: '__image_snapshots__',
202
+ });
203
+ ```
204
+
205
+ ### Custom Snapshot Identifier
206
+
207
+ ```typescript
208
+ await expect(screenshot).toMatchImageSnapshot({
209
+ method: 'core',
210
+ snapshotIdentifier: 'homepage-desktop-chrome',
211
+ });
212
+ ```
213
+
214
+ ### With Playwright
215
+
216
+ ```typescript
217
+ import { test, expect } from 'vitest';
218
+ import '@blazediff/vitest';
219
+ import { chromium } from 'playwright';
220
+
221
+ test('visual regression with Playwright', async () => {
222
+ const browser = await chromium.launch();
223
+ const page = await browser.newPage();
224
+ await page.goto('https://example.com');
225
+
226
+ const screenshot = await page.screenshot();
227
+ await expect(screenshot).toMatchImageSnapshot({
228
+ method: 'core',
229
+ });
230
+
231
+ await browser.close();
232
+ });
233
+ ```
234
+
235
+ ### Negation
236
+
237
+ ```typescript
238
+ // Assert images are different
239
+ await expect(screenshot).not.toMatchImageSnapshot({
240
+ method: 'core',
241
+ });
242
+ ```
243
+
244
+ ### Sequential Tests
245
+
246
+ For tests that might interfere with each other (e.g., cleaning up snapshots), use sequential execution:
247
+
248
+ ```typescript
249
+ import { describe, it } from 'vitest';
250
+
251
+ describe.sequential('Visual regression tests', () => {
252
+ it('test 1', async () => {
253
+ await expect(image1).toMatchImageSnapshot({ method: 'core' });
254
+ });
255
+
256
+ it('test 2', async () => {
257
+ await expect(image2).toMatchImageSnapshot({ method: 'core' });
258
+ });
259
+ });
260
+ ```
261
+
262
+ ## Snapshot State Tracking
263
+
264
+ This matcher integrates with Vitest's snapshot state tracking system. Vitest will report accurate counts in test summaries:
265
+
266
+ ```
267
+ Snapshots 2 written | 1 updated | 5 passed
268
+ ```
269
+
270
+ The matcher updates Vitest's internal counters:
271
+ - **Written**: New snapshots created
272
+ - **Updated**: Snapshots updated with `-u` flag
273
+ - **Passed**: Existing snapshots matched
274
+ - **Failed**: Snapshot comparisons failed
275
+
276
+ ## Setup
277
+
278
+ ### Auto-setup (Recommended)
279
+
280
+ Simply import the package in your test file:
281
+
282
+ ```typescript
283
+ import '@blazediff/vitest';
284
+ ```
285
+
286
+ The matcher is automatically registered when imported.
287
+
288
+ ### Manual Setup
289
+
290
+ Alternatively, call the setup function explicitly:
291
+
292
+ ```typescript
293
+ import { setupBlazediffMatchers } from '@blazediff/vitest';
294
+
295
+ setupBlazediffMatchers();
296
+ ```
297
+
298
+ ### Global Setup
299
+
300
+ To avoid importing in every test file, add to your Vitest config:
301
+
302
+ ```typescript
303
+ // vitest.config.ts
304
+ import { defineConfig } from 'vitest/config';
305
+
306
+ export default defineConfig({
307
+ test: {
308
+ setupFiles: ['./vitest.setup.ts'],
309
+ },
310
+ });
311
+ ```
312
+
313
+ ```typescript
314
+ // vitest.setup.ts
315
+ import '@blazediff/vitest';
316
+ ```
317
+
318
+ ## TypeScript
319
+
320
+ TypeScript types are included. To use the matcher with TypeScript:
321
+
322
+ ```typescript
323
+ import '@blazediff/vitest';
324
+
325
+ declare module 'vitest' {
326
+ interface Assertion<T = any> {
327
+ toMatchImageSnapshot(options?: Partial<import('@blazediff/matcher').MatcherOptions>): Promise<void>;
328
+ }
329
+ }
330
+ ```
331
+
332
+ The type augmentation is automatically included when you import the package.
333
+
334
+ ## Links
335
+
336
+ - [GitHub Repository](https://github.com/teimurjan/blazediff)
337
+ - [Documentation](https://blazediff.dev/docs/vitest)
338
+ - [NPM Package](https://www.npmjs.com/package/@blazediff/vitest)
339
+ - [@blazediff/matcher](https://www.npmjs.com/package/@blazediff/matcher) - Core matcher logic
@@ -0,0 +1,57 @@
1
+ import { MatcherOptions } from '@blazediff/matcher';
2
+ export { ComparisonResult, ImageInput, MatcherOptions } from '@blazediff/matcher';
3
+
4
+ declare module "vitest" {
5
+ interface Matchers<T = any> {
6
+ /**
7
+ * Compare an image against a stored snapshot.
8
+ *
9
+ * On first run, creates a new snapshot.
10
+ * On subsequent runs, compares against the stored snapshot.
11
+ *
12
+ * @param options - Matcher options including comparison method and thresholds
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * // Compare file path
17
+ * await expect('/path/to/screenshot.png').toMatchImageSnapshot({
18
+ * method: 'bin',
19
+ * failureThreshold: 100,
20
+ * failureThresholdType: 'pixel',
21
+ * });
22
+ *
23
+ * // Compare image buffer
24
+ * await expect({
25
+ * data: imageBuffer,
26
+ * width: 800,
27
+ * height: 600
28
+ * }).toMatchImageSnapshot({
29
+ * method: 'ssim',
30
+ * failureThreshold: 0.01,
31
+ * failureThresholdType: 'percent',
32
+ * });
33
+ * ```
34
+ */
35
+ toMatchImageSnapshot(options?: Partial<MatcherOptions>): Promise<T>;
36
+ }
37
+ interface AsymmetricMatchers {
38
+ toMatchImageSnapshot(options?: Partial<MatcherOptions>): void;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Setup blazediff matchers for Vitest
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // In your vitest setup file or test file
48
+ * import { setupBlazediffMatchers } from '@blazediff/vitest';
49
+ * setupBlazediffMatchers();
50
+ *
51
+ * // Or just import the package (auto-setup)
52
+ * import '@blazediff/vitest';
53
+ * ```
54
+ */
55
+ declare function setupBlazediffMatchers(): void;
56
+
57
+ export { setupBlazediffMatchers as default, setupBlazediffMatchers };
@@ -0,0 +1,57 @@
1
+ import { MatcherOptions } from '@blazediff/matcher';
2
+ export { ComparisonResult, ImageInput, MatcherOptions } from '@blazediff/matcher';
3
+
4
+ declare module "vitest" {
5
+ interface Matchers<T = any> {
6
+ /**
7
+ * Compare an image against a stored snapshot.
8
+ *
9
+ * On first run, creates a new snapshot.
10
+ * On subsequent runs, compares against the stored snapshot.
11
+ *
12
+ * @param options - Matcher options including comparison method and thresholds
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * // Compare file path
17
+ * await expect('/path/to/screenshot.png').toMatchImageSnapshot({
18
+ * method: 'bin',
19
+ * failureThreshold: 100,
20
+ * failureThresholdType: 'pixel',
21
+ * });
22
+ *
23
+ * // Compare image buffer
24
+ * await expect({
25
+ * data: imageBuffer,
26
+ * width: 800,
27
+ * height: 600
28
+ * }).toMatchImageSnapshot({
29
+ * method: 'ssim',
30
+ * failureThreshold: 0.01,
31
+ * failureThresholdType: 'percent',
32
+ * });
33
+ * ```
34
+ */
35
+ toMatchImageSnapshot(options?: Partial<MatcherOptions>): Promise<T>;
36
+ }
37
+ interface AsymmetricMatchers {
38
+ toMatchImageSnapshot(options?: Partial<MatcherOptions>): void;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Setup blazediff matchers for Vitest
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // In your vitest setup file or test file
48
+ * import { setupBlazediffMatchers } from '@blazediff/vitest';
49
+ * setupBlazediffMatchers();
50
+ *
51
+ * // Or just import the package (auto-setup)
52
+ * import '@blazediff/vitest';
53
+ * ```
54
+ */
55
+ declare function setupBlazediffMatchers(): void;
56
+
57
+ export { setupBlazediffMatchers as default, setupBlazediffMatchers };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});var matcher=require('@blazediff/matcher'),vitest=require('vitest');function n(){vitest.expect.extend({async toMatchImageSnapshot(r,s){let p=this.testPath||"",a=this.currentTestName||"unknown",t=this.snapshotState,c=s?.updateSnapshots||t?._updateSnapshot==="all"||process.env.UPDATE_SNAPSHOTS==="true",e=await matcher.getOrCreateSnapshot(r,{method:"core",...s,updateSnapshots:c},{testPath:p,testName:a});if(t&&e.snapshotStatus)switch(e.snapshotStatus){case "added":t.added.increment(a),t._dirty=true;break;case "updated":t.updated.increment(a),t._dirty=true;break;case "matched":t.matched.increment(a);break;case "failed":t.unmatched.increment(a);break}return {pass:e.pass,message:()=>e.message,actual:e.receivedPath,expected:e.baselinePath}}});}n();var m=n;exports.default=m;exports.setupBlazediffMatchers=n;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import {getOrCreateSnapshot}from'@blazediff/matcher';import {expect}from'vitest';function n(){expect.extend({async toMatchImageSnapshot(r,s){let p=this.testPath||"",a=this.currentTestName||"unknown",t=this.snapshotState,c=s?.updateSnapshots||t?._updateSnapshot==="all"||process.env.UPDATE_SNAPSHOTS==="true",e=await getOrCreateSnapshot(r,{method:"core",...s,updateSnapshots:c},{testPath:p,testName:a});if(t&&e.snapshotStatus)switch(e.snapshotStatus){case "added":t.added.increment(a),t._dirty=true;break;case "updated":t.updated.increment(a),t._dirty=true;break;case "matched":t.matched.increment(a);break;case "failed":t.unmatched.increment(a);break}return {pass:e.pass,message:()=>e.message,actual:e.receivedPath,expected:e.baselinePath}}});}n();var m=n;export{m as default,n as setupBlazediffMatchers};
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@blazediff/vitest",
3
+ "version": "1.0.1",
4
+ "description": "Vitest matcher for visual regression testing with blazediff",
5
+ "private": false,
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "main": "dist/index.js",
10
+ "module": "dist/index.mjs",
11
+ "types": "dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "vitest",
24
+ "image",
25
+ "comparison",
26
+ "diff",
27
+ "visual-testing",
28
+ "matcher",
29
+ "snapshot"
30
+ ],
31
+ "author": "Teimur Gasanov <me@teimurjan.dev> (https://github.com/teimurjan)",
32
+ "repository": "https://github.com/teimurjan/blazediff",
33
+ "homepage": "https://blazediff.dev",
34
+ "license": "MIT",
35
+ "dependencies": {
36
+ "@blazediff/matcher": "1.0.1"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^24.3.0",
40
+ "@vitest/coverage-v8": "^3.2.4",
41
+ "tsup": "8.5.0",
42
+ "typescript": "5.9.2",
43
+ "vitest": "^3.2.4"
44
+ },
45
+ "peerDependencies": {
46
+ "vitest": ">=1.0.0"
47
+ },
48
+ "scripts": {
49
+ "typecheck": "tsc --noEmit",
50
+ "build": "tsup",
51
+ "dev": "tsup --watch",
52
+ "clean": "rm -rf dist",
53
+ "test": "vitest run",
54
+ "test:watch": "vitest",
55
+ "test:coverage": "vitest run --coverage"
56
+ }
57
+ }