Skip to content

Commit 84d98d7

Browse files
authored
Merge pull request #159 from Hargne/bugfix/append-issue
Bugfix: Append test result issue
2 parents 346cc7e + 59575ad commit 84d98d7

File tree

6 files changed

+1080
-1124
lines changed

6 files changed

+1080
-1124
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-html-reporter",
3-
"version": "3.7.1",
3+
"version": "3.8.0",
44
"description": "Jest test results processor for generating a summary in HTML",
55
"main": "dist/index.js",
66
"unpkg": "dist/index.js",
@@ -57,7 +57,7 @@
5757
"@rollup/plugin-babel": "^5.3.0",
5858
"@rollup/plugin-node-resolve": "^7.1.1",
5959
"@types/dateformat": "^3.0.X",
60-
"@types/jest": "29.0.0",
60+
"@types/jest": "^29.0.0",
6161
"@types/mkdirp": "1.0.2",
6262
"@types/node": "12.20.12",
6363
"@types/sinon": "9.0.11",

src/htmlreporter.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,49 @@ class HTMLReporter {
3636
this.jestConfig ? this.jestConfig.rootDir : "",
3737
this.getConfigValue("outputPath") as string
3838
);
39-
4039
await mkdirp(path.dirname(outputPath));
40+
41+
let writeFullReport = true;
4142
if (this.getConfigValue("append") as boolean) {
42-
await this.appendToFile(outputPath, report.toString());
43-
} else {
44-
fs.writeFileSync(outputPath, report.toString());
43+
const fileExists = fs.existsSync(outputPath);
44+
if (fileExists) {
45+
await this.appendToFile(outputPath, report.content.toString());
46+
writeFullReport = false;
47+
}
48+
}
49+
if (writeFullReport) {
50+
fs.writeFileSync(outputPath, report.fullHtml.toString());
4551
}
4652

4753
this.logMessage("success", `Report generated (${outputPath})`);
48-
return report;
54+
return report.fullHtml;
4955
} catch (e) {
5056
this.logMessage("error", e);
5157
}
5258
}
5359

54-
public async renderTestReport() {
55-
// Generate the content of the test report
60+
public async renderTestReport(): Promise<{
61+
fullHtml: string;
62+
content: string;
63+
}> {
5664
const reportContent = await this.renderTestReportContent();
5765

58-
// --
59-
6066
// Boilerplate Option
61-
if (!!this.getConfigValue("boilerplate")) {
67+
if (this.getConfigValue("boilerplate")) {
6268
const boilerplatePath = this.replaceRootDirInPath(
6369
this.jestConfig ? this.jestConfig.rootDir : "",
6470
this.getConfigValue("boilerplate") as string
6571
);
66-
6772
const boilerplateContent = fs.readFileSync(boilerplatePath, "utf8");
68-
return boilerplateContent.replace(
69-
"{jesthtmlreporter-content}",
70-
reportContent && reportContent.toString()
71-
);
73+
return {
74+
content: reportContent.toString(),
75+
fullHtml: boilerplateContent.replace(
76+
"{jesthtmlreporter-content}",
77+
reportContent && reportContent.toString()
78+
),
79+
};
7280
}
7381

74-
// --
75-
7682
// Create HTML and apply reporter content
7783
const report = xmlbuilder.create({ html: {} });
7884
const headTag = report.ele("head");
@@ -115,7 +121,10 @@ class HTMLReporter {
115121
`<script src="${this.getConfigValue("customScriptPath")}"></script>`
116122
);
117123
}
118-
return report;
124+
return {
125+
fullHtml: report.toString(),
126+
content: reportContent.toString(),
127+
};
119128
}
120129

121130
public renderTestSuiteInfo(parent: XMLElement, suite: TestResult) {
@@ -178,7 +187,7 @@ class HTMLReporter {
178187

179188
// HTML Body
180189
const reportBody: XMLElement = xmlbuilder.begin().element("div", {
181-
id: "jesthtml-content",
190+
class: "jesthtml-content",
182191
});
183192

184193
/**
@@ -668,32 +677,26 @@ class HTMLReporter {
668677
* @param filePath
669678
* @param content
670679
*/
671-
public async appendToFile(filePath: string, content: any) {
680+
public async appendToFile(filePath: string, content: string) {
672681
let parsedContent = content;
673-
// Check if the file exists or not
674-
const fileExists = fs.existsSync(filePath);
675-
// The file exists - we need to strip all unnecessary html
676-
if (fileExists) {
677-
const fileToAppend = fs.readFileSync(filePath, "utf8");
678-
const contentSearch = /<body>(.*?)<\/body>/gm.exec(content);
679-
if (contentSearch) {
680-
const [strippedContent] = contentSearch;
681-
parsedContent = strippedContent;
682-
}
683-
// Then we need to add the stripped content just before the </body> tag
684-
let newContent = fileToAppend;
685-
const closingBodyTag = /<\/body>/gm.exec(fileToAppend);
686-
const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag.index : 0;
687-
688-
newContent = [
689-
fileToAppend.slice(0, indexOfClosingBodyTag),
690-
parsedContent,
691-
fileToAppend.slice(indexOfClosingBodyTag),
692-
].join("");
693-
694-
return fs.writeFileSync(filePath, newContent);
682+
const fileToAppend = fs.readFileSync(filePath, "utf8");
683+
const contentSearch = /<body>(.*?)<\/body>/gm.exec(content);
684+
if (contentSearch) {
685+
const [strippedContent] = contentSearch;
686+
parsedContent = strippedContent;
695687
}
696-
return fs.appendFileSync(filePath, parsedContent);
688+
// Then we need to add the stripped content just before the </body> tag
689+
let newContent = fileToAppend;
690+
const closingBodyTag = /<\/body>/gm.exec(fileToAppend);
691+
const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag.index : 0;
692+
693+
newContent = [
694+
fileToAppend.slice(0, indexOfClosingBodyTag),
695+
parsedContent,
696+
fileToAppend.slice(indexOfClosingBodyTag),
697+
].join("");
698+
699+
return fs.writeFileSync(filePath, newContent);
697700
}
698701

699702
/**
@@ -703,8 +706,8 @@ class HTMLReporter {
703706
* @param filePath
704707
*/
705708
public replaceRootDirInPath(
706-
rootDir: Config.GlobalConfig['rootDir'],
707-
filePath: Config.GlobalConfig['testPathPattern']
709+
rootDir: Config.GlobalConfig["rootDir"],
710+
filePath: Config.GlobalConfig["testPathPattern"]
708711
): string {
709712
if (!/^<rootDir>/.test(filePath)) {
710713
return filePath;

style/defaultTheme.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ body {
1010
padding: 2rem 1rem;
1111
font-size: 0.85rem;
1212
}
13-
#jesthtml-content {
13+
.jesthtml-content {
1414
margin: 0 auto;
1515
max-width: 70rem;
1616
}

0 commit comments

Comments
 (0)