@blazediff/bun 0.0.0-fix-testing-matcher-performance-20260223113219

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,354 @@
1
+ # @blazediff/bun
2
+
3
+ <div align="center">
4
+
5
+ [![npm bundle size](https://img.shields.io/bundlephobia/min/%40blazediff%2Fbun)](https://www.npmjs.com/package/@blazediff/bun)
6
+ [![NPM Downloads](https://img.shields.io/npm/dy/%40blazediff%2Fbun)](https://www.npmjs.com/package/@blazediff/bun)
7
+
8
+ </div>
9
+
10
+ Bun test matcher for visual regression testing with blazediff. Powered by @blazediff/matcher with Bun-specific snapshot state integration.
11
+
12
+ ## Features
13
+
14
+ - **Native Bun matcher**: `toMatchImageSnapshot()` extends Bun's expect
15
+ - **Snapshot state tracking**: Bun reports accurate snapshot counts (when API available)
16
+ - **Multiple comparison algorithms**: `core`, `bin`, `ssim`, `msssim`, `hitchhikers-ssim`, `gmsd`
17
+ - **Auto-setup**: Imports and registers automatically
18
+ - **Update mode**: Works with Bun's `-u`/`--update` flag
19
+ - **TypeScript support**: Full type definitions included
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install --dev @blazediff/bun
25
+ ```
26
+
27
+ **Peer dependencies**: Bun >= 1.0.0
28
+
29
+ ## Quick Start
30
+
31
+ ```typescript
32
+ import { expect, it } from 'bun:test';
33
+ import '@blazediff/bun';
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
+ Bun test 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>'snapshot'</td>
100
+ <td>Identifier for the snapshot file (required for Bun)</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
+ <tr>
115
+ <td><code>runInWorker</code></td>
116
+ <td>boolean</td>
117
+ <td>true</td>
118
+ <td>Run comparison in worker thread for better performance</td>
119
+ </tr>
120
+ </table>
121
+
122
+ See [@blazediff/matcher](https://www.npmjs.com/package/@blazediff/matcher) for full options documentation.
123
+
124
+ ## Usage Patterns
125
+
126
+ ### Basic Snapshot Test
127
+
128
+ ```typescript
129
+ import { expect, it } from 'bun:test';
130
+ import '@blazediff/bun';
131
+
132
+ it('renders correctly', async () => {
133
+ const screenshot = await page.screenshot();
134
+
135
+ await expect(screenshot).toMatchImageSnapshot({
136
+ method: 'core',
137
+ snapshotIdentifier: 'homepage',
138
+ });
139
+ });
140
+ ```
141
+
142
+ ### Different Comparison Methods
143
+
144
+ ```typescript
145
+ // Fast Rust-native comparison (file paths only)
146
+ await expect('/path/to/image.png').toMatchImageSnapshot({
147
+ method: 'bin',
148
+ snapshotIdentifier: 'image-bin',
149
+ });
150
+
151
+ // Pure JavaScript comparison
152
+ await expect(imageBuffer).toMatchImageSnapshot({
153
+ method: 'core',
154
+ snapshotIdentifier: 'image-core',
155
+ });
156
+
157
+ // Perceptual similarity (SSIM)
158
+ await expect(imageBuffer).toMatchImageSnapshot({
159
+ method: 'ssim',
160
+ snapshotIdentifier: 'image-ssim',
161
+ });
162
+
163
+ // Gradient-based comparison
164
+ await expect(imageBuffer).toMatchImageSnapshot({
165
+ method: 'gmsd',
166
+ snapshotIdentifier: 'image-gmsd',
167
+ });
168
+ ```
169
+
170
+ ### Update Snapshots
171
+
172
+ ```bash
173
+ # Update all snapshots (recommended)
174
+ bun test --update-snapshots
175
+
176
+ # Or using environment variable
177
+ BUN_UPDATE_SNAPSHOTS=true bun test
178
+ ```
179
+
180
+ Or programmatically:
181
+
182
+ ```typescript
183
+ await expect(screenshot).toMatchImageSnapshot({
184
+ method: 'core',
185
+ snapshotIdentifier: 'homepage',
186
+ updateSnapshots: true,
187
+ });
188
+ ```
189
+
190
+ ### Custom Thresholds
191
+
192
+ ```typescript
193
+ // Allow up to 100 pixels difference
194
+ await expect(screenshot).toMatchImageSnapshot({
195
+ method: 'core',
196
+ snapshotIdentifier: 'homepage',
197
+ failureThreshold: 100,
198
+ failureThresholdType: 'pixel',
199
+ });
200
+
201
+ // Allow up to 0.1% difference
202
+ await expect(screenshot).toMatchImageSnapshot({
203
+ method: 'core',
204
+ snapshotIdentifier: 'homepage',
205
+ failureThreshold: 0.1,
206
+ failureThresholdType: 'percent',
207
+ });
208
+ ```
209
+
210
+ ### Custom Snapshot Directory
211
+
212
+ ```typescript
213
+ await expect(screenshot).toMatchImageSnapshot({
214
+ method: 'core',
215
+ snapshotIdentifier: 'homepage',
216
+ snapshotsDir: '__image_snapshots__',
217
+ });
218
+ ```
219
+
220
+ ### With Playwright
221
+
222
+ ```typescript
223
+ import { test, expect } from 'bun:test';
224
+ import '@blazediff/bun';
225
+ import { chromium } from 'playwright';
226
+
227
+ test('visual regression with Playwright', async () => {
228
+ const browser = await chromium.launch();
229
+ const page = await browser.newPage();
230
+ await page.goto('https://example.com');
231
+
232
+ const screenshot = await page.screenshot();
233
+ await expect(screenshot).toMatchImageSnapshot({
234
+ method: 'core',
235
+ snapshotIdentifier: 'homepage',
236
+ });
237
+
238
+ await browser.close();
239
+ });
240
+ ```
241
+
242
+ ### Negation
243
+
244
+ ```typescript
245
+ // Assert images are different
246
+ await expect(screenshot).not.toMatchImageSnapshot({
247
+ method: 'core',
248
+ snapshotIdentifier: 'different',
249
+ });
250
+ ```
251
+
252
+ ### Serial Tests
253
+
254
+ For tests that might interfere with each other (e.g., cleaning up snapshots), use serial execution:
255
+
256
+ ```typescript
257
+ import { describe, it } from 'bun:test';
258
+
259
+ describe.serial('Visual regression tests', () => {
260
+ it('test 1', async () => {
261
+ await expect(image1).toMatchImageSnapshot({
262
+ method: 'core',
263
+ snapshotIdentifier: 'test-1',
264
+ });
265
+ });
266
+
267
+ it('test 2', async () => {
268
+ await expect(image2).toMatchImageSnapshot({
269
+ method: 'core',
270
+ snapshotIdentifier: 'test-2',
271
+ });
272
+ });
273
+ });
274
+ ```
275
+
276
+ ## Snapshot State Tracking
277
+
278
+ This matcher attempts to integrate with Bun's snapshot state tracking system. If Bun exposes the snapshot state API to custom matchers, test summaries will show accurate counts:
279
+
280
+ ```
281
+ Snapshots 2 written | 1 updated | 5 passed
282
+ ```
283
+
284
+ **Note**: Bun's snapshot state API for custom matchers is limited. If snapshot state is unavailable, image snapshots will still work correctly but may not appear in Bun's test summary counts.
285
+
286
+ ## Bun-Specific Notes
287
+
288
+ ### Snapshot Identifier Required
289
+
290
+ Unlike Jest and Vitest which can auto-generate snapshot identifiers from test names, Bun has limited context exposure. It's recommended to always provide a `snapshotIdentifier`:
291
+
292
+ ```typescript
293
+ await expect(screenshot).toMatchImageSnapshot({
294
+ method: 'core',
295
+ snapshotIdentifier: 'my-component-state', // Recommended
296
+ });
297
+ ```
298
+
299
+ ### Test Path Detection
300
+
301
+ Bun provides `Bun.main` which is used to determine the test file path. In some edge cases, you may need to specify `snapshotsDir` as an absolute path:
302
+
303
+ ```typescript
304
+ await expect(screenshot).toMatchImageSnapshot({
305
+ method: 'core',
306
+ snapshotIdentifier: 'homepage',
307
+ snapshotsDir: '/absolute/path/to/snapshots',
308
+ });
309
+ ```
310
+
311
+ ## Setup
312
+
313
+ ### Auto-setup (Recommended)
314
+
315
+ Simply import the package in your test file:
316
+
317
+ ```typescript
318
+ import '@blazediff/bun';
319
+ ```
320
+
321
+ The matcher is automatically registered when imported.
322
+
323
+ ### Manual Setup
324
+
325
+ Alternatively, call the setup function explicitly:
326
+
327
+ ```typescript
328
+ import { setupBlazediffMatchers } from '@blazediff/bun';
329
+
330
+ setupBlazediffMatchers();
331
+ ```
332
+
333
+ ## TypeScript
334
+
335
+ TypeScript types are included. To use the matcher with TypeScript:
336
+
337
+ ```typescript
338
+ import '@blazediff/bun';
339
+
340
+ declare module 'bun:test' {
341
+ interface Matchers<T = unknown> {
342
+ toMatchImageSnapshot(options?: Partial<import('@blazediff/matcher').MatcherOptions>): Promise<MatcherResult>;
343
+ }
344
+ }
345
+ ```
346
+
347
+ The type augmentation is automatically included when you import the package.
348
+
349
+ ## Links
350
+
351
+ - [GitHub Repository](https://github.com/teimurjan/blazediff)
352
+ - [Documentation](https://blazediff.dev/docs/bun)
353
+ - [NPM Package](https://www.npmjs.com/package/@blazediff/bun)
354
+ - [@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 "bun:test" {
5
+ interface Matchers<T> {
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 Bun test
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // In your test file
48
+ * import { setupBlazediffMatchers } from '@blazediff/bun';
49
+ * setupBlazediffMatchers();
50
+ *
51
+ * // Or just import the package (auto-setup)
52
+ * import '@blazediff/bun';
53
+ * ```
54
+ */
55
+ declare function setupBlazediffMatchers(): void;
56
+
57
+ export { setupBlazediffMatchers as default, setupBlazediffMatchers };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});var bun_test=require('bun:test'),matcher=require('@blazediff/matcher');function r(){bun_test.expect.extend({toMatchImageSnapshot:async function(o,e){let a=h(),p=e?.snapshotIdentifier||"snapshot",c=e?.updateSnapshots||process.env.BUN_UPDATE_SNAPSHOTS==="true"||Bun.argv.includes("-u")||Bun.argv.includes("--update-snapshots"),s=await matcher.getOrCreateSnapshot(o,{method:"core",...e,updateSnapshots:c,updateCommand:"--update-snapshots or BUN_UPDATE_SNAPSHOTS=true"},{testPath:a,testName:p}),t=this.snapshotState;if(t&&s.snapshotStatus)switch(s.snapshotStatus){case "added":t.added=(t.added||0)+1;break;case "updated":t.updated=(t.updated||0)+1;break;case "matched":t.matched=(t.matched||0)+1;break;case "failed":t.unmatched=(t.unmatched||0)+1;break}return {pass:s.pass,message:()=>s.message}}});}function h(){if(typeof Bun<"u"&&Bun.main)return Bun.main;let n=new Error().stack;if(n){let o=n.split(`
2
+ `);for(let e of o){let a=e.match(/at\s+(.+\.test\.[tj]s)/);if(a)return a[1]}}return "unknown"}r();var f=r;exports.default=f;exports.setupBlazediffMatchers=r;
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import {expect}from'bun:test';import {getOrCreateSnapshot}from'@blazediff/matcher';function r(){expect.extend({toMatchImageSnapshot:async function(o,e){let a=h(),p=e?.snapshotIdentifier||"snapshot",c=e?.updateSnapshots||process.env.BUN_UPDATE_SNAPSHOTS==="true"||Bun.argv.includes("-u")||Bun.argv.includes("--update-snapshots"),s=await getOrCreateSnapshot(o,{method:"core",...e,updateSnapshots:c,updateCommand:"--update-snapshots or BUN_UPDATE_SNAPSHOTS=true"},{testPath:a,testName:p}),t=this.snapshotState;if(t&&s.snapshotStatus)switch(s.snapshotStatus){case "added":t.added=(t.added||0)+1;break;case "updated":t.updated=(t.updated||0)+1;break;case "matched":t.matched=(t.matched||0)+1;break;case "failed":t.unmatched=(t.unmatched||0)+1;break}return {pass:s.pass,message:()=>s.message}}});}function h(){if(typeof Bun<"u"&&Bun.main)return Bun.main;let n=new Error().stack;if(n){let o=n.split(`
2
+ `);for(let e of o){let a=e.match(/at\s+(.+\.test\.[tj]s)/);if(a)return a[1]}}return "unknown"}r();var f=r;export{f as default,r as setupBlazediffMatchers};
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@blazediff/bun",
3
+ "version": "0.0.0-fix-testing-matcher-performance-20260223113219",
4
+ "description": "Bun test 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
+ "bun",
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": "0.0.0-fix-testing-matcher-performance-20260223113219"
37
+ },
38
+ "devDependencies": {
39
+ "@types/bun": "^1.2.10",
40
+ "@types/node": "^24.3.0",
41
+ "tsup": "8.5.0",
42
+ "typescript": "5.9.2"
43
+ },
44
+ "peerDependencies": {
45
+ "bun": ">=1.0.0"
46
+ },
47
+ "scripts": {
48
+ "typecheck": "tsc --noEmit",
49
+ "build": "tsup",
50
+ "dev": "tsup --watch",
51
+ "clean": "rm -rf dist",
52
+ "test": "bun test"
53
+ }
54
+ }