Skip to content

Commit

Permalink
Merge pull request #82 from Hargne/release/2.8.0
Browse files Browse the repository at this point in the history
v2.8.0
  • Loading branch information
Hargne authored Dec 10, 2019
2 parents 61e47ca + e090795 commit 80a5b06
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Please note that all configuration properties are optional.
| `sort` | `STRING` | Sorts the test results using the given method. Available sorting methods can be found in the [documentation](https://github.com/Hargne/jest-html-reporter/wiki/Sorting-Methods). | `"default"`
| `statusIgnoreFilter` | `STRING` | A comma-separated string of the test result statuses that should be ignored when rendering the report. Available statuses are: `"passed"`, `"pending"`, `"failed"` | `null`
| `boilerplate` | `STRING` | The path to a boilerplate file that should be used to render the body of the test results into. `{jesthtmlreporter-content}` within the boilerplate will be replaced with the test results | `null`
| `append` | `BOOLEAN` | If set to true, new test results will be appended to the existing test report | `false`

> *The plugin will search for the *styleOverridePath* from the root directory, therefore there is no need to prepend the string with `./` or `../` - You can read more about the themes in the [documentation](https://github.com/Hargne/jest-html-reporter/wiki/Test-Report-Themes).
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-html-reporter",
"version": "2.7.0",
"version": "2.8.0",
"description": "Jest test results processor for generating a summary in HTML",
"main": "dist/main",
"unpkg": "dist/main.min.js",
Expand Down
8 changes: 8 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ const getSort = () =>
const getStatusIgnoreFilter = () =>
process.env.JEST_HTML_REPORTER_STATUS_FILTER || config.statusIgnoreFilter || null;

/**
* Returns whether or not new reports should be Appended to existing report
* @return {Boolean}
*/
const getAppend = () =>
process.env.JEST_HTML_REPORTER_APPEND || config.append || false;

module.exports = {
config,
setup,
Expand All @@ -160,4 +167,5 @@ module.exports = {
getDateFormat,
getSort,
getStatusIgnoreFilter,
getAppend,
};
8 changes: 6 additions & 2 deletions src/reportGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ReportGenerator {
const fileDestination = this.config.getOutputFilepath();
const useCssFile = this.config.shouldUseCssFile();
const shouldGetStylesheetContent = this.config.shouldGetStylesheetContent();
const append = this.config.getAppend();
let stylesheetPath = null;
let stylesheetContent = null;

Expand All @@ -39,10 +40,13 @@ class ReportGenerator {
stylesheet,
stylesheetPath,
}))
.then(xmlBuilderOutput => utils.writeFile({
.then(xmlBuilderOutput => (append ? utils.appendFile({
filePath: fileDestination,
content: xmlBuilderOutput,
}))
}) : utils.writeFile({
filePath: fileDestination,
content: xmlBuilderOutput,
})))
.then(() => utils.logMessage({
type: 'success',
msg: `Report generated (${fileDestination})`,
Expand Down
50 changes: 50 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,55 @@ const writeFile = ({ filePath, content }) => new Promise((resolve, reject) => {
});
});

/**
* Appends a file at the given destination
* @param {String} filePath
* @param {Any} content
*/
const appendFile = ({ filePath, content }) => new Promise((resolve, reject) => {
mkdirp(path.dirname(filePath), (mkdirpError) => {
if (mkdirpError) {
return reject(new Error(`Something went wrong when creating the folder: ${mkdirpError}`));
}

// Check if the file exists or not
return fs.readFile(filePath, 'utf8', (err, existingContent) => {
let parsedContent = content;
// The file exists - we need to strip all unecessary html
if (!err) {
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
if (existingContent) {
let newContent = existingContent;
const closingBodyTag = /<\/body>/gm.exec(existingContent);
const indexOfClosingBodyTag = closingBodyTag ? closingBodyTag.index : 0;

newContent = [existingContent.slice(0, indexOfClosingBodyTag), parsedContent, existingContent.slice(indexOfClosingBodyTag)]
.join('');

return fs.writeFile(filePath, newContent, (writeFileError) => {
if (writeFileError) {
return reject(new Error(`Something went wrong when creating the file: ${writeFileError}`));
}
return resolve(filePath);
});
}
}

return fs.appendFile(filePath, parsedContent, (writeFileError) => {
if (writeFileError) {
return reject(new Error(`Something went wrong when appending the file: ${writeFileError}`));
}
return resolve(filePath);
});
});
});
});

/**
* Reads and returns the content of a given file
* @param {String} filePath
Expand Down Expand Up @@ -90,6 +139,7 @@ const sortAlphabetically = ({ a, b, reversed }) => {
module.exports = {
logMessage,
writeFile,
appendFile,
getFileContent,
createHtmlBase,
sortAlphabetically,
Expand Down
3 changes: 3 additions & 0 deletions style/darkTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ body {
padding: 3rem;
font-size: 0.85rem;
}
#jesthtml-content {
margin-bottom: 2rem;
}
header {
display: flex;
align-items: center;
Expand Down
3 changes: 3 additions & 0 deletions style/defaultTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ body {
padding: 1rem;
font-size: 0.85rem;
}
#jesthtml-content {
margin-bottom: 2rem;
}
header {
display: flex;
align-items: center;
Expand Down
3 changes: 3 additions & 0 deletions style/lightTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ body {
padding: 3rem;
font-size: 0.85rem;
}
#jesthtml-content {
margin-bottom: 2rem;
}
header {
display: flex;
align-items: center;
Expand Down

0 comments on commit 80a5b06

Please sign in to comment.