@blazediff/bun 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,348 @@
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
+ </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 'bun:test';
124
+ import '@blazediff/bun';
125
+
126
+ it('renders correctly', async () => {
127
+ const screenshot = await page.screenshot();
128
+
129
+ await expect(screenshot).toMatchImageSnapshot({
130
+ method: 'core',
131
+ snapshotIdentifier: 'homepage',
132
+ });
133
+ });
134
+ ```
135
+
136
+ ### Different Comparison Methods
137
+
138
+ ```typescript
139
+ // Fast Rust-native comparison (file paths only)
140
+ await expect('/path/to/image.png').toMatchImageSnapshot({
141
+ method: 'bin',
142
+ snapshotIdentifier: 'image-bin',
143
+ });
144
+
145
+ // Pure JavaScript comparison
146
+ await expect(imageBuffer).toMatchImageSnapshot({
147
+ method: 'core',
148
+ snapshotIdentifier: 'image-core',
149
+ });
150
+
151
+ // Perceptual similarity (SSIM)
152
+ await expect(imageBuffer).toMatchImageSnapshot({
153
+ method: 'ssim',
154
+ snapshotIdentifier: 'image-ssim',
155
+ });
156
+
157
+ // Gradient-based comparison
158
+ await expect(imageBuffer).toMatchImageSnapshot({
159
+ method: 'gmsd',
160
+ snapshotIdentifier: 'image-gmsd',
161
+ });
162
+ ```
163
+
164
+ ### Update Snapshots
165
+
166
+ ```bash
167
+ # Update all snapshots
168
+ bun test -u
169
+
170
+ # Or
171
+ bun test --update
172
+ ```
173
+
174
+ Or programmatically:
175
+
176
+ ```typescript
177
+ await expect(screenshot).toMatchImageSnapshot({
178
+ method: 'core',
179
+ snapshotIdentifier: 'homepage',
180
+ updateSnapshots: true,
181
+ });
182
+ ```
183
+
184
+ ### Custom Thresholds
185
+
186
+ ```typescript
187
+ // Allow up to 100 pixels difference
188
+ await expect(screenshot).toMatchImageSnapshot({
189
+ method: 'core',
190
+ snapshotIdentifier: 'homepage',
191
+ failureThreshold: 100,
192
+ failureThresholdType: 'pixel',
193
+ });
194
+
195
+ // Allow up to 0.1% difference
196
+ await expect(screenshot).toMatchImageSnapshot({
197
+ method: 'core',
198
+ snapshotIdentifier: 'homepage',
199
+ failureThreshold: 0.1,
200
+ failureThresholdType: 'percent',
201
+ });
202
+ ```
203
+
204
+ ### Custom Snapshot Directory
205
+
206
+ ```typescript
207
+ await expect(screenshot).toMatchImageSnapshot({
208
+ method: 'core',
209
+ snapshotIdentifier: 'homepage',
210
+ snapshotsDir: '__image_snapshots__',
211
+ });
212
+ ```
213
+
214
+ ### With Playwright
215
+
216
+ ```typescript
217
+ import { test, expect } from 'bun:test';
218
+ import '@blazediff/bun';
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
+ snapshotIdentifier: 'homepage',
230
+ });
231
+
232
+ await browser.close();
233
+ });
234
+ ```
235
+
236
+ ### Negation
237
+
238
+ ```typescript
239
+ // Assert images are different
240
+ await expect(screenshot).not.toMatchImageSnapshot({
241
+ method: 'core',
242
+ snapshotIdentifier: 'different',
243
+ });
244
+ ```
245
+
246
+ ### Serial Tests
247
+
248
+ For tests that might interfere with each other (e.g., cleaning up snapshots), use serial execution:
249
+
250
+ ```typescript
251
+ import { describe, it } from 'bun:test';
252
+
253
+ describe.serial('Visual regression tests', () => {
254
+ it('test 1', async () => {
255
+ await expect(image1).toMatchImageSnapshot({
256
+ method: 'core',
257
+ snapshotIdentifier: 'test-1',
258
+ });
259
+ });
260
+
261
+ it('test 2', async () => {
262
+ await expect(image2).toMatchImageSnapshot({
263
+ method: 'core',
264
+ snapshotIdentifier: 'test-2',
265
+ });
266
+ });
267
+ });
268
+ ```
269
+
270
+ ## Snapshot State Tracking
271
+
272
+ 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:
273
+
274
+ ```
275
+ Snapshots 2 written | 1 updated | 5 passed
276
+ ```
277
+
278
+ **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.
279
+
280
+ ## Bun-Specific Notes
281
+
282
+ ### Snapshot Identifier Required
283
+
284
+ 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`:
285
+
286
+ ```typescript
287
+ await expect(screenshot).toMatchImageSnapshot({
288
+ method: 'core',
289
+ snapshotIdentifier: 'my-component-state', // Recommended
290
+ });
291
+ ```
292
+
293
+ ### Test Path Detection
294
+
295
+ 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:
296
+
297
+ ```typescript
298
+ await expect(screenshot).toMatchImageSnapshot({
299
+ method: 'core',
300
+ snapshotIdentifier: 'homepage',
301
+ snapshotsDir: '/absolute/path/to/snapshots',
302
+ });
303
+ ```
304
+
305
+ ## Setup
306
+
307
+ ### Auto-setup (Recommended)
308
+
309
+ Simply import the package in your test file:
310
+
311
+ ```typescript
312
+ import '@blazediff/bun';
313
+ ```
314
+
315
+ The matcher is automatically registered when imported.
316
+
317
+ ### Manual Setup
318
+
319
+ Alternatively, call the setup function explicitly:
320
+
321
+ ```typescript
322
+ import { setupBlazediffMatchers } from '@blazediff/bun';
323
+
324
+ setupBlazediffMatchers();
325
+ ```
326
+
327
+ ## TypeScript
328
+
329
+ TypeScript types are included. To use the matcher with TypeScript:
330
+
331
+ ```typescript
332
+ import '@blazediff/bun';
333
+
334
+ declare module 'bun:test' {
335
+ interface Matchers<T = unknown> {
336
+ toMatchImageSnapshot(options?: Partial<import('@blazediff/matcher').MatcherOptions>): Promise<MatcherResult>;
337
+ }
338
+ }
339
+ ```
340
+
341
+ The type augmentation is automatically included when you import the package.
342
+
343
+ ## Links
344
+
345
+ - [GitHub Repository](https://github.com/teimurjan/blazediff)
346
+ - [Documentation](https://blazediff.dev/docs/bun)
347
+ - [NPM Package](https://www.npmjs.com/package/@blazediff/bun)
348
+ - [@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 };
@@ -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=i(),c=e?.snapshotIdentifier||"snapshot",p=e?.updateSnapshots||process.env.UPDATE_SNAPSHOTS==="true"||Bun.argv.includes("-u")||Bun.argv.includes("--update"),s=await matcher.getOrCreateSnapshot(o,{method:"core",...e,updateSnapshots:p},{testPath:a,testName:c}),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 i(){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=i(),c=e?.snapshotIdentifier||"snapshot",p=e?.updateSnapshots||process.env.UPDATE_SNAPSHOTS==="true"||Bun.argv.includes("-u")||Bun.argv.includes("--update"),s=await getOrCreateSnapshot(o,{method:"core",...e,updateSnapshots:p},{testPath:a,testName:c}),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 i(){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": "1.0.1",
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": "1.0.1"
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
+ }