Skip to content

Commit

Permalink
Merge pull request #159 from Hargne/bugfix/append-issue
Browse files Browse the repository at this point in the history
Bugfix: Append test result issue
  • Loading branch information
Hargne committed Apr 20, 2023
2 parents 346cc7e + 59575ad commit 84d98d7
Show file tree
Hide file tree
Showing 6 changed files with 1,080 additions and 1,124 deletions.
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "jest-html-reporter",
"version": "3.7.1",
"version": "3.8.0",
"description": "Jest test results processor for generating a summary in HTML",
"main": "dist/index.js",
"unpkg": "dist/index.js",
Expand Down Expand Up @@ -57,7 +57,7 @@
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@types/dateformat": "^3.0.X",
"@types/jest": "29.0.0",
"@types/jest": "^29.0.0",
"@types/mkdirp": "1.0.2",
"@types/node": "12.20.12",
"@types/sinon": "9.0.11",
Expand Down
93 changes: 48 additions & 45 deletions src/htmlreporter.ts
Expand Up @@ -36,43 +36,49 @@ class HTMLReporter {
this.jestConfig ? this.jestConfig.rootDir : "",
this.getConfigValue("outputPath") as string
);

await mkdirp(path.dirname(outputPath));

let writeFullReport = true;
if (this.getConfigValue("append") as boolean) {
await this.appendToFile(outputPath, report.toString());
} else {
fs.writeFileSync(outputPath, report.toString());
const fileExists = fs.existsSync(outputPath);
if (fileExists) {
await this.appendToFile(outputPath, report.content.toString());
writeFullReport = false;
}
}
if (writeFullReport) {
fs.writeFileSync(outputPath, report.fullHtml.toString());
}

this.logMessage("success", `Report generated (${outputPath})`);
return report;
return report.fullHtml;
} catch (e) {
this.logMessage("error", e);
}
}

public async renderTestReport() {
// Generate the content of the test report
public async renderTestReport(): Promise<{
fullHtml: string;
content: string;
}> {
const reportContent = await this.renderTestReportContent();

// --

// Boilerplate Option
if (!!this.getConfigValue("boilerplate")) {
if (this.getConfigValue("boilerplate")) {
const boilerplatePath = this.replaceRootDirInPath(
this.jestConfig ? this.jestConfig.rootDir : "",
this.getConfigValue("boilerplate") as string
);

const boilerplateContent = fs.readFileSync(boilerplatePath, "utf8");
return boilerplateContent.replace(
"{jesthtmlreporter-content}",
reportContent && reportContent.toString()
);
return {
content: reportContent.toString(),
fullHtml: boilerplateContent.replace(
"{jesthtmlreporter-content}",
reportContent && reportContent.toString()
),
};
}

// --

// Create HTML and apply reporter content
const report = xmlbuilder.create({ html: {} });
const headTag = report.ele("head");
Expand Down Expand Up @@ -115,7 +121,10 @@ class HTMLReporter {
`<script src="${this.getConfigValue("customScriptPath")}"></script>`
);
}
return report;
return {
fullHtml: report.toString(),
content: reportContent.toString(),
};
}

public renderTestSuiteInfo(parent: XMLElement, suite: TestResult) {
Expand Down Expand Up @@ -178,7 +187,7 @@ class HTMLReporter {

// HTML Body
const reportBody: XMLElement = xmlbuilder.begin().element("div", {
id: "jesthtml-content",
class: "jesthtml-content",
});

/**
Expand Down Expand Up @@ -668,32 +677,26 @@ class HTMLReporter {
* @param filePath
* @param content
*/
public async appendToFile(filePath: string, content: any) {
public async appendToFile(filePath: string, content: string) {
let parsedContent = content;
// Check if the file exists or not
const fileExists = fs.existsSync(filePath);
// The file exists - we need to strip all unnecessary html
if (fileExists) {
const fileToAppend = fs.readFileSync(filePath, "utf8");
const contentSearch = /<body>(.*?)<\/body>/gm.exec(content);
if (contentSearch) {
const [strippedContent] = contentSearch;
parsedContent = strippedContent;
}
// Then we need to add the stripped content just before the </body> tag
let newContent = fileToAppend;
const closingBodyTag = /<\/body>/gm.exec(fileToAppend);
const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag.index : 0;

newContent = [
fileToAppend.slice(0, indexOfClosingBodyTag),
parsedContent,
fileToAppend.slice(indexOfClosingBodyTag),
].join("");

return fs.writeFileSync(filePath, newContent);
const fileToAppend = fs.readFileSync(filePath, "utf8");
const contentSearch = /<body>(.*?)<\/body>/gm.exec(content);
if (contentSearch) {
const [strippedContent] = contentSearch;
parsedContent = strippedContent;
}
return fs.appendFileSync(filePath, parsedContent);
// Then we need to add the stripped content just before the </body> tag
let newContent = fileToAppend;
const closingBodyTag = /<\/body>/gm.exec(fileToAppend);
const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag.index : 0;

newContent = [
fileToAppend.slice(0, indexOfClosingBodyTag),
parsedContent,
fileToAppend.slice(indexOfClosingBodyTag),
].join("");

return fs.writeFileSync(filePath, newContent);
}

/**
Expand All @@ -703,8 +706,8 @@ class HTMLReporter {
* @param filePath
*/
public replaceRootDirInPath(
rootDir: Config.GlobalConfig['rootDir'],
filePath: Config.GlobalConfig['testPathPattern']
rootDir: Config.GlobalConfig["rootDir"],
filePath: Config.GlobalConfig["testPathPattern"]
): string {
if (!/^<rootDir>/.test(filePath)) {
return filePath;
Expand Down
2 changes: 1 addition & 1 deletion style/defaultTheme.css
Expand Up @@ -10,7 +10,7 @@ body {
padding: 2rem 1rem;
font-size: 0.85rem;
}
#jesthtml-content {
.jesthtml-content {
margin: 0 auto;
max-width: 70rem;
}
Expand Down

0 comments on commit 84d98d7

Please sign in to comment.