Skip to content

Commit 2cb6a0a

Browse files
committed
Try using pixelmatch for testing
1 parent a1244eb commit 2cb6a0a

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"i18next-browser-languagedetector": "^4.0.1",
5353
"lint-staged": "^15.1.0",
5454
"msw": "^2.6.3",
55+
"pixelmatch": "^6.0.0",
5556
"rollup": "^4.9.6",
5657
"rollup-plugin-string": "^3.0.0",
5758
"rollup-plugin-visualizer": "^5.12.0",

test/unit/visual/visualTest.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import p5 from '../../../src/app.js';
22
import { server } from '@vitest/browser/context'
3+
import pixelmatch from 'pixelmatch'
34
import { THRESHOLD, DIFFERENCE, ERODE } from '../../../src/core/constants.js';
45
const { readFile, writeFile } = server.commands
56

@@ -104,39 +105,22 @@ export async function checkMatch(actual, expected, p5) {
104105
);
105106
}
106107

107-
const expectedWithBg = p5.createGraphics(expected.width, expected.height);
108-
expectedWithBg.pixelDensity(1);
109-
expectedWithBg.background(BG);
110-
expectedWithBg.image(expected, 0, 0);
108+
const diffData = actual.drawingContext.createImageData(actual.width, actual.height);
109+
const diffCount = pixelmatch(
110+
actual.drawingContext.getImageData(0, 0, actual.width, actual.height).data,
111+
expected.drawingContext.getImageData(0, 0, actual.width, actual.height).data,
112+
diffData.data,
113+
actual.width,
114+
actual.height,
115+
{ threshold: 0.4 }
116+
);
111117

112118
const cnv = p5.createGraphics(actual.width, actual.height);
113-
cnv.pixelDensity(1);
114-
cnv.background(BG);
115-
cnv.image(actual, 0, 0);
116-
cnv.blendMode(DIFFERENCE);
117-
cnv.image(expectedWithBg, 0, 0);
118-
for (let i = 0; i < shiftThreshold; i++) {
119-
cnv.filter(ERODE, false);
120-
}
121-
const diff = cnv.get();
119+
cnv.drawingContext.putImageData(diffData, 0, 0)
120+
const diff = cnv.get()
122121
cnv.remove();
123-
diff.loadPixels();
124-
expectedWithBg.remove();
125-
126-
let ok = true;
127-
for (let i = 0; i < diff.pixels.length; i += 4) {
128-
let diffSum = 0;
129-
for (let off = 0; off < 3; off++) {
130-
diffSum += diff.pixels[i+off]
131-
}
132-
diffSum /= 3;
133-
if (diffSum > COLOR_THRESHOLD) {
134-
ok = false;
135-
break;
136-
}
137-
}
138122

139-
return { ok, diff };
123+
return { ok: diffCount === 0, diff };
140124
}
141125

142126
/**

0 commit comments

Comments
 (0)