-
Notifications
You must be signed in to change notification settings - Fork 21
/
reporter.ts
91 lines (86 loc) · 3.57 KB
/
reporter.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { config, JestScreenshotConfiguration } from "./config";
import { AggregatedResult } from "@jest/test-result";
import { Context } from "@jest/reporters";
import { readFileSync, writeFileSync, readdirSync, mkdirSync, existsSync, unlinkSync } from "fs";
import * as path from "path";
import { sync as rimrafSync } from "rimraf";
import { getReportPath, getReportDir } from "./filenames";
import { FailedSnapshotInfo, ReportMetadata, FileReport } from "./reporter-types";
const template = (testResults: ReportMetadata) => `<html>
<head>
<meta charset="utf-8" />
<title>Jest Screenshot Report</title>
<link href="dist/report-viewer.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="root"></div>
<script>
window.testResults = ${JSON.stringify(testResults)};
</script>
<script src="dist/report-viewer.js"></script>
</body>
</html>`;
export = class JestScreenshotReporter { // tslint:disable-line
private config = config();
constructor() {
const { reportDir: reportDirName } = this.config;
const reportDir = getReportDir(reportDirName);
if (existsSync(reportDir)) {
rimrafSync(reportDir);
}
}
public onRunComplete(contexts: Set<Context>, { testResults, numFailedTests }: AggregatedResult) {
const { reportDir: reportDirName, noReport } = this.config;
const reportDir = getReportDir(reportDirName);
const reportsDir = path.join(reportDir, "reports");
// if (numFailedTests === 0) { return; }
if (noReport) { return; }
if (!existsSync(reportsDir)) { return; }
const failedSnapshots = readdirSync(reportsDir).map(testPath => {
const infoFilePath = path.join(reportDir, "reports", testPath, "info.json");
const info: FailedSnapshotInfo = JSON.parse(readFileSync(infoFilePath, "utf8"));
return info;
});
if (failedSnapshots.length === 0) { return; }
const fileReports: FileReport[] = testResults.reduce((reports, testFile) => {
const testFilePath = path.relative(process.cwd(), testFile.testFilePath);
const failedTests = testFile.testResults.reduce((failed, test) => {
const { fullName } = test;
const matchingFailedSnapshots = failedSnapshots.filter(failedSnapshot => {
return failedSnapshot.testName === fullName && failedSnapshot.testFileName === testFilePath;
});
if (matchingFailedSnapshots.length === 0) { return failed; }
failed.push({
titles: [...test.ancestorTitles, test.title],
failedSnapshots: matchingFailedSnapshots,
});
return failed;
}, []);
if (failedTests.length === 0) { return reports; }
reports.push({
testFilePath,
failedTests,
});
return reports;
}, []);
writeFileSync(path.join(reportDir, "index.html"), template({
files: fileReports,
}));
try {
mkdirSync(path.join(reportDir, "dist"));
} catch (err) {
// tslint: disable-line
}
[
"report-viewer.js",
"report-viewer.js.map",
"report-viewer.css",
"report-viewer.css.map",
].forEach(fileName =>
writeFileSync(
path.join(reportDir, "dist", fileName),
readFileSync(path.join(__dirname, fileName)),
),
);
}
};