-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathcompareImages.test.js
56 lines (47 loc) · 2.36 KB
/
compareImages.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-env jest*/
const compareImages = require("../compareImages");
const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
describe("compareImages", () => {
test("Buffers data", async () => {
const readImg1 = readFile("./demoassets/People.jpg");
const readImg2 = readFile("./demoassets/People2.jpg");
const readComparison = readFile("./nodejs-tests/assets/PeopleComparedToPeople2.png");
const data = await compareImages(await readImg1, await readImg2);
const buffer = data.getBuffer();
const comparison = await readComparison;
expect(data.isSameDimensions).toBe(true);
expect(data.misMatchPercentage).toEqual("8.66");
expect(buffer).toBeInstanceOf(Buffer);
expect(buffer.length).toBe(91876);
expect(buffer.equals(comparison)).toBe(true);
});
test("Buffer data includeOriginal", async () => {
const readImg1 = readFile("./demoassets/People.jpg");
const readImg2 = readFile("./demoassets/People2.jpg");
const readComparison = readFile("./nodejs-tests/assets/PeopleComparedToPeople2WithOriginal.png");
const data = await compareImages(await readImg1, await readImg2);
const buffer = data.getBuffer(true);
const comparison = await readComparison;
expect(buffer.equals(comparison)).toBe(true);
});
test("throws when failed", async () => {
const promise = compareImages(fs.readFileSync("./demoassets/People.jpg"), "bogus data");
await expect(promise).rejects.toMatch("Failed to load image 'bogus data'. Error: ENOENT, No such file or directory 'bogus data'");
});
test("throws when invalid image format", async () => {
const invalidImageFormat = "data:,";
const promise = compareImages(invalidImageFormat, fs.readFileSync("./demoassets/People.jpg"));
await expect(promise).rejects.toMatch("Failed to load image 'data:,'. Error: Unsupported image type");
});
test("returns early", async () => {
const readImg1 = readFile("./demoassets/People.jpg");
const readImg2 = readFile("./demoassets/People2.jpg");
const options = {
returnEarlyThreshold: 5
};
const data = await compareImages(await readImg1, await readImg2, options);
expect(data.misMatchPercentage).toEqual("5.00");
});
});