-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use odiff-bin to compare screenshots
- Loading branch information
1 parent
948bca0
commit 168a8bc
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
node_modules | ||
coverage | ||
dist | ||
tmp | ||
analysis.json | ||
yarn-error.log | ||
.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* eslint-env node */ | ||
const path = require('path'); | ||
const { compare } = require('odiff-bin'); | ||
const mkdirp = require('mkdirp'); | ||
const fs = require('fs'); | ||
const { nanoid } = require('nanoid'); | ||
const { promisify } = require('util'); | ||
|
||
const readFile = promisify(fs.readFile); | ||
const writeFile = promisify(fs.writeFile); | ||
const removeFile = promisify(fs.unlink); | ||
|
||
async function saveImage({ filePath, content }) { | ||
await mkdirp(path.dirname(filePath)); | ||
await writeFile(filePath, content); | ||
} | ||
|
||
async function imageDiff({ baselineImage, image, options }) { | ||
const diffOptions = { | ||
threshold: options.threshold, | ||
antialiasing: true | ||
}; | ||
|
||
const id = nanoid(); | ||
|
||
// TODO: https://github.com/dmtrKovalenko/odiff/issues/53 | ||
const tmpOldImagePath = path.resolve(`tmp/${id}-baseline.png`); | ||
const tmpNewImagePath = path.resolve(`tmp/${id}-actual.png`); | ||
const tmpDiffPath = path.resolve(`tmp/${id}-diff.png`); | ||
|
||
await saveImage({ filePath: tmpOldImagePath, content: baselineImage }); | ||
await saveImage({ filePath: tmpNewImagePath, content: image }); | ||
|
||
const { match, reason, diffPercentage } = await compare(tmpOldImagePath, tmpNewImagePath, tmpDiffPath, diffOptions); | ||
|
||
const doNotMatch = match == false; | ||
|
||
const result = { | ||
error: doNotMatch ? reason : false, | ||
diffPercentage: diffPercentage || 0 | ||
}; | ||
|
||
if (doNotMatch) { | ||
result.diffImage = await readFile(tmpDiffPath); | ||
await removeFile(tmpDiffPath); | ||
} | ||
|
||
await removeFile(tmpOldImagePath); | ||
await removeFile(tmpNewImagePath); | ||
|
||
return result; | ||
} | ||
|
||
module.exports = { | ||
imageDiff | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters