-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathjest-imagediff-reporter.js
50 lines (43 loc) · 1.59 KB
/
jest-imagediff-reporter.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
const fs = require("fs");
const path = require("path");
const DIFF_DIRECTORY = path.join(__dirname, "..", "jest-screenshot-report");
class ImageDiffReporter {
/* istanbul ignore next - test coverage in child process */
onRunStart() {
try {
fs.statSync(DIFF_DIRECTORY);
} catch (e) {
fs.mkdirSync(DIFF_DIRECTORY, { recursive: true });
}
// cleanup
const files = fs.readdirSync(DIFF_DIRECTORY);
if (files.length > 0) {
files.forEach(function (filename) {
if (fs.statSync(DIFF_DIRECTORY + "/" + filename).isDirectory()) {
this.removeDir(DIFF_DIRECTORY + "/" + filename);
} else {
fs.unlinkSync(DIFF_DIRECTORY + "/" + filename);
}
});
}
}
/* istanbul ignore next - test coverage in child process */
onRunComplete() {
const files = fs.readdirSync(DIFF_DIRECTORY);
const imagesHtml = files.map((file) => `<h3>${file.replace("-diff.png", "")}</h3><img src="./${file}" />`).join("\n");
// write index.html
const html = `
<html>
<head>
<title>Diff reporter</title>
</head>
<body>
<h1>Diff reporter</h1>
${imagesHtml}
</body>
</html>
`;
fs.writeFileSync(path.join(DIFF_DIRECTORY, "index.html"), html);
}
}
module.exports = ImageDiffReporter;