Skip to content

Commit

Permalink
Add simple test for compare
Browse files Browse the repository at this point in the history
The tests were initially super slow, as the PNG.sync.write took up to 20 seconds.
This seems to be related to jest [1][2]. I therefore added Math to the
sandboxInjectedGlobals to speed things up.

[1] pngjs/pngjs#96
[2] jestjs/jest#5163 (comment)
  • Loading branch information
raphiz committed May 4, 2023
1 parent 3124afc commit 259e56b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions pixelpact/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata
31 changes: 23 additions & 8 deletions pixelpact/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pixelpact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
"fastify": "^4.6.0",
"pixelmatch": "^5.3.0",
"playwright": "^1.26.1",
"pngjs": "^6.0.0"
"pngjs": "^7.0.0"
},
"devDependencies": {
"jest": "^29.5.0",
"prettier": "2.8.2",
"supervisor": "^0.12.0"
},
"jest": {
"sandboxInjectedGlobals": [
"Math"
],
"testEnvironment": "node"
}
}
8 changes: 5 additions & 3 deletions pixelpact/src/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ export async function compare(expected, actual, options = { threshold: 0.01 }) {
const expectedPng = PNG.sync.read(expected);
const actualPng = PNG.sync.read(actual);
const { width, height } = expectedPng;
const diff = new PNG({ width, height });
const diffPng = new PNG({ width, height });
const numDiffPixels = pixelmatch(
expectedPng.data,
actualPng.data,
diff.data,
diffPng.data,
width,
height,
options
);
const diff = PNG.sync.write(diffPng);

return {
numDiffPixels: numDiffPixels,
expected: expected,
actual: actual,
diff: PNG.sync.write(diff),
diff: diff,
};
}
17 changes: 17 additions & 0 deletions pixelpact/src/compare.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from "fs/promises";
import { compare } from "./compare.js";

describe("compare", () => {
it("returns the number of pixels that differ", async () => {
let expected = await fs.readFile("testdata/simple-expected.png");
let actual = await fs.readFile("testdata/simple-actual.png");
let expectedDiff = await fs.readFile("testdata/simple-diff.png");

let result = await compare(expected, actual);

expect(result.numDiffPixels).toBe(152);
expect(result.expected).toStrictEqual(expected);
expect(result.actual).toStrictEqual(actual);
expect(result.diff).toStrictEqual(expectedDiff);
});
});
Binary file added pixelpact/testdata/simple-actual.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pixelpact/testdata/simple-diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pixelpact/testdata/simple-expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 259e56b

Please sign in to comment.