vitest-screenshot 0.1.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ben Houston
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,32 @@
1
+ # vitest-screenshot
2
+
3
+ `expect(image).toMatchScreenshot(name)`: pixel-diff an RGBA image against a PNG baseline committed
4
+ next to your tests. Backend-agnostic: feed it `canvas.getImageData()` from
5
+ [`vitest-environment-webgl-node`](../vitest-environment-webgl-node), a WebGPU texture readback, or
6
+ pixels from a real browser.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ pnpm add -D vitest vitest-screenshot
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```ts
17
+ import { extendMatchers } from 'vitest-screenshot';
18
+ extendMatchers();
19
+
20
+ it('renders the knot', () => {
21
+ renderer.render(scene, camera);
22
+ expect(canvas.getImageData()).toMatchScreenshot('knot', { maxDiffRatio: 0.01 });
23
+ });
24
+ ```
25
+
26
+ - Baselines live in `__screenshots__/<name>.png` beside the test file (override with `baselineDir`).
27
+ - A missing baseline is written on first run, except when `CI` is set, where it fails.
28
+ - `UPDATE_SCREENSHOTS=1 vitest` (or `update: true`) rewrites baselines.
29
+ - On failure `<name>.actual.png` and `<name>.diff.png` are written beside the baseline. Gitignore them.
30
+ - `threshold` is pixelmatch's per-pixel colour tolerance (default 0.1); `maxDiffRatio` is the fraction
31
+ of pixels allowed to differ (default 0.001). GPUs and drivers rasterize slightly differently, so loosen
32
+ `maxDiffRatio` for baselines shared across platforms.
@@ -0,0 +1,23 @@
1
+ /** Anything with RGBA8 pixels, top row first: `ImageData`, node-webgl's `canvas.getImageData()`, or a WebGPU readback. */
2
+ export type RgbaImage = {
3
+ width: number;
4
+ height: number;
5
+ data: Uint8Array | Uint8ClampedArray;
6
+ };
7
+ export type ScreenshotOptions = {
8
+ /** Directory holding baseline PNGs. Default: `__screenshots__` beside the test file. */
9
+ baselineDir?: string;
10
+ /** Per-pixel colour distance (0..1) tolerated by pixelmatch. Default 0.1. */
11
+ threshold?: number;
12
+ /** Fraction (0..1) of pixels allowed to differ. Default 0.001. */
13
+ maxDiffRatio?: number;
14
+ /** Overwrite the baseline. Default: `UPDATE_SCREENSHOTS` env var is set. */
15
+ update?: boolean;
16
+ };
17
+ declare module 'vitest' {
18
+ interface Assertion {
19
+ toMatchScreenshot(name: string, options?: ScreenshotOptions): void;
20
+ }
21
+ }
22
+ export declare function extendMatchers(): void;
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,0HAA0H;AAC1H,MAAM,MAAM,SAAS,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,GAAG,iBAAiB,CAAA;CAAE,CAAC;AAEhG,MAAM,MAAM,iBAAiB,GAAG;IAC9B,wFAAwF;IACxF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6EAA6E;IAC7E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kEAAkE;IAClE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,4EAA4E;IAC5E,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,OAAO,QAAQ,QAAQ,CAAC;IACtB,UAAU,SAAS;QACjB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,IAAI,CAAC;KACpE;CACF;AAUD,wBAAgB,cAAc,IAAI,IAAI,CAoCrC"}
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { basename, dirname, join } from 'node:path';
3
+ import pixelmatch from 'pixelmatch';
4
+ import { PNG } from 'pngjs';
5
+ import { expect } from 'vitest';
6
+ const result = (pass, message) => ({ pass, message: () => message });
7
+ const toPng = ({ width, height, data }) => {
8
+ const png = new PNG({ width, height });
9
+ png.data = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
10
+ return png;
11
+ };
12
+ export function extendMatchers() {
13
+ expect.extend({
14
+ toMatchScreenshot(received, name, options = {}) {
15
+ const dir = options.baselineDir ?? join(dirname(this.testPath ?? ''), '__screenshots__');
16
+ const file = join(dir, `${name}.png`);
17
+ const actual = toPng(received);
18
+ const update = options.update ?? Boolean(process.env.UPDATE_SCREENSHOTS);
19
+ if (update || (!existsSync(file) && !process.env.CI)) {
20
+ mkdirSync(dir, { recursive: true });
21
+ writeFileSync(file, PNG.sync.write(actual));
22
+ return result(true, `wrote baseline ${file}`);
23
+ }
24
+ if (!existsSync(file))
25
+ return result(false, `missing baseline ${file}; run with UPDATE_SCREENSHOTS=1`);
26
+ const baseline = PNG.sync.read(readFileSync(file));
27
+ if (baseline.width !== actual.width || baseline.height !== actual.height) {
28
+ return result(false, `size mismatch: baseline ${baseline.width}x${baseline.height}, actual ${actual.width}x${actual.height}`);
29
+ }
30
+ const diff = new PNG({ width: actual.width, height: actual.height });
31
+ const count = pixelmatch(baseline.data, actual.data, diff.data, actual.width, actual.height, {
32
+ threshold: options.threshold ?? 0.1,
33
+ });
34
+ const ratio = count / (actual.width * actual.height);
35
+ const pass = ratio <= (options.maxDiffRatio ?? 0.001);
36
+ if (!pass) {
37
+ writeFileSync(join(dir, `${name}.actual.png`), PNG.sync.write(actual));
38
+ writeFileSync(join(dir, `${name}.diff.png`), PNG.sync.write(diff));
39
+ }
40
+ return result(pass, `${basename(file)}: ${count} pixels (${(ratio * 100).toFixed(3)}%) differ; see ${name}.diff.png`);
41
+ },
42
+ });
43
+ }
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAsBhC,MAAM,MAAM,GAAG,CAAC,IAAa,EAAE,OAAe,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;AAEtF,MAAM,KAAK,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAa,EAAO,EAAE;IACxD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,KAAK,EAAE,MAA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C,KAAK,KAAK,YAAY,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAkB,IAAI,WAAW,CACjG,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "vitest-screenshot",
3
+ "version": "0.1.0",
4
+ "description": "toMatchScreenshot(): pixel-diff an RGBA image against a committed PNG baseline in Vitest",
5
+ "keywords": [
6
+ "matcher",
7
+ "pixelmatch",
8
+ "screenshot",
9
+ "visual-regression",
10
+ "vitest",
11
+ "webgl",
12
+ "webgpu"
13
+ ],
14
+ "homepage": "https://github.com/bhouston/vitest-gpu#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/bhouston/vitest-gpu/issues"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Ben Houston <ben@ben3d.ca> (https://ben3d.ca)",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/bhouston/vitest-gpu.git",
23
+ "directory": "packages/vitest-screenshot"
24
+ },
25
+ "files": [
26
+ "dist/**/*.js",
27
+ "dist/**/*.js.map",
28
+ "dist/**/*.d.ts",
29
+ "dist/**/*.d.ts.map",
30
+ "CHANGELOG.md"
31
+ ],
32
+ "type": "module",
33
+ "main": "./dist/index.js",
34
+ "types": "./dist/index.d.ts",
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.js"
39
+ }
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc -b",
46
+ "tsc": "tsc -b"
47
+ },
48
+ "dependencies": {
49
+ "pixelmatch": "^7.1.0",
50
+ "pngjs": "^7.0.0"
51
+ },
52
+ "devDependencies": {
53
+ "@types/pngjs": "^6.0.5"
54
+ },
55
+ "peerDependencies": {
56
+ "vitest": ">=3 <5"
57
+ },
58
+ "engines": {
59
+ "node": ">=20.0.0"
60
+ }
61
+ }